4

I have a single-line div with content on the left and on the right:

---------------------------------------------------------------------------
|Single line of text                          Icon and single line of text|                                                
---------------------------------------------------------------------------

If there is no enough space I want the right content to take the width it needs while the left content should take the rest of the available width (with overflow hidden to keep a single line).

The problem is that the content (left & right) is dynamic, so I do not know its width in advance.

Any hint? Thanks

2 Answers 2

2

Give this a whirl:

http://jsfiddle.net/thirtydot/fjJMX/191/

CSS:

#lineContainer {
    overflow: hidden; /* clear floats */
    outline: 1px solid red;
    width: 300px /* just to make demo easier */
}
#lineLeft {
    overflow: hidden; /* hide extra text */
    white-space: nowrap;
    background: #f0f

}
#lineRight {
    float: right;
    background: #ccc
}

HTML:

<div id="lineContainer">
    <div id="lineRight">right right right right right</div>
    <div id="lineLeft">left left left</div>
</div>
Sign up to request clarification or add additional context in comments.

4 Comments

It works perfectly. I had assumed that floated elements needed to have a defined width. Thanks!
Maybe there was a bug in that version of Firefox? It works for me now in Firefox 40.0.2.
Too bad you have to put the right content above the left in the HTML. Can you come up with a solution where you keep your html in the (for me) more natural order with the left content first, and the right after? I'm afraid screen readers will read the content in the wrong order otherwise
@Tobbe: You can use a display: table-cell based solution instead, example here.
0

Float only one of the columns should work.

.left {
   float: left;
}
.right {
   overflow: hidden;
   padding-left: 20px;
}

See updated fiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.