tags:

views:

426

answers:

5
------------- --------------------------------------------------
| some text | | some more text, sometimes more, sometimes less |
------------- --------------------------------------------------

|<------------------------- 100% width ----------------------->|

So I have the above layout. The left box should always be as small as possible, while the right one should take up the remaining space. That would normally be fine with a float: left.

The problem is that the right box can grow quite a lot, while the left one is pretty much guaranteed to be very small (yet varies in size, so needs to be flexible). If the right box grows, I need it to behave like this:

------------- --------------------------------------------------
| some text | | quite a lot of text, actually, really quite a  |
------------- | bunch of it, you could say this is really      |
              | quite a heck of a lot of text                  |
              --------------------------------------------------

If I use a float:left, the right box will line-break under the left box if it contains a lot of text:

-------------
| some text |                                         (not good)
-------------
----------------------------------------------------------------
| quite a lot of text, actually, really quite a bunch of it,   |
| you could say this is really quite a heck of a lot of text   |
----------------------------------------------------------------

If I use a table for both instead, the left box may grow unnecessarily if both contain very little text:

                                                      (not good)
-------------------------------- -------------------------------
| some text                    | | not that much text          |
-------------------------------- -------------------------------

Furthermore, the left box is not supposed to line-break. But since it contains a few HTML elements, not just pure text, a no-wrap doesn't seem to work in all browsers.

What's a good solution to this problem?

EDIT

It's not actually terribly important that the right box takes up the remaining width, just that it always stays attached to the right side of the left box.

A: 

Try this,

<div style="border:solid 1px black; width:20%; height:200px;float:left">
  Hello
</div>
<div style="border:solid 1px black; width:78%; height:200px;float:right;">
  Hello
</div>
adatapost
No, as I said, the left one needs to be flexible, so no fixed widths. :(
deceze
A: 

Define a fixed width to the left column as well as float:left; put it after the main column in the HTML

Two divs for the right column; the outer one also floated left and with 100% width, the inner with a left margin of the same width as the left column.

Steve Gilham
Sorry, no fixed widths, the left column needs to be flexible.
deceze
A: 

As Stobor suggested: Try putting the left box inside the right box

<div id="rightBox">
   <div id="leftBox">This text will expand the left box</div>
   <div style="float:left">
      Content of right box goes here<br>
      Breaking even works...
   </div>
</div>

CSS:

#rightBox {
   width: 100%;
   background: red;
}
#leftBox {
   width: auto;
   float: left;
   background: blue;
}

Works for me.

peirix
So basically put both in a wrapper and float them left? Doesn't work for me, if the right box contains a lot of text (no line breaks!), it wraps below the left box.
deceze
Ah. I see. Hmm.. I'll have to try out a couple of other things then.
peirix
A: 

If you're using a pure css solution I think you're going to have to give one of the columns some sort of width constraint. Can it definitely not be a percentage as suggested above?

The problem being 2 fold. If you float one column and give the other a width of 100%, because the floated column is removed from the document flow the 100% width of the other is the full viewport width (it ignores the floated div). If you float both without setting the width of the second div the div will(should) shrink to the width of it's widest child element. the w3c spec maybe of some help, though it's not the easiest read.

You could always try using javascript to read the viewport width and then setting the column widths, otherwise I think you're going to struggle.

paulb
I'll play around with it some more, but it seems indeed there's hardly a solution without Javascript. Right now I'm thinking about using a table, as that gets me pretty close already, and adjusting the width of the left column via JS. :(
deceze
+3  A: 

For the right box use overflow: hidden; width: auto; instead of floating. That should take the remaining space without dropping below no matter how much content there is. Continue floating the left box.

Eric Meyer
Hey, I think you're onto something there. I didn't know `overflow: hidden` works that way. I need to do some more tests, but this seems to be the answer! :)
deceze
I learned it from Nicole Sullivan over at stubbornella.org - she has some more detail on it there:http://www.stubbornella.org/content/2009/07/23/overflow-a-secret-benefit/Very useful.
Eric Meyer
Awesome, it works. Ze Frentch 'ave manie a secröt trick. (Forgive my bad imitation. ;))
deceze