tags:

views:

54

answers:

3

Hey peeps,

I have an issue (about the third time I've ran into it actually) where I have a container which holds a left and right div.

If they are of different lengths (right being longer than left or the other way around) how can I get the other one to stretch [in height] without using Javascript.

I don't want to have to result in table and I thought inherit would do it (but it just inherits auto). There doesn't seem to be an easy answer. Or should I just use tables!? Anything wrong with that?

This problem is blowing my mind for how stupidly simple it should be...

Thanks in advance!

edit: I'll give an example:

<body style="background: #000000 url(????.com);">
    <div id="container" style="margin:0 auto; width:996px; height: auto; background: transparent; overflow:hidden;">
        <div id="left" style="float:left; width:690px">
             <div style="background-color:#FFFFFF;">Content</div>
             <div style="background-color:#FFFFFF;">content</div>
        </div>
        <div id="right" style="float:left; width:306px">
             <div style="background: transparent;">Content<br />Content<br />content<br/>Last line</div>
             <div style="background-color:#FFFFFF;">Content</div>
        </div>
    </div>
</body>

As you would know, the left is much smaller than the right. Height:100% seems to do nothing.

Result: it's impossible without JS support. Cheers all.

+1  A: 

http://www.alistapart.com/articles/fauxcolumns/

For anything relatively simple faux columns will do the job, you just repeat an image vertically mimicking the simple solid bg colors of the columns. If it's anything more complex you'll need to do a combination of the sort.

See my reply here for more techniques: http://stackoverflow.com/questions/1184515/2-column-css-div-with-stretchable-height/1184522#1184522

meder
Thanks, I'll look at the faux technique.
Dorjan
THe problem here is that it wouldn't work with transparency
Dorjan
Do you have a link to the design?
meder
+2  A: 

If you need support for IE6 and IE7 I´m afraid you can´t do that in just css without tables or javascript.

For other browsers you can go the display: table-cell and display: table-row way.

Of course faking it using background images works if you don´t really need the columns to be equal height but just appear to be like that.

jeroen
As I thought. SO for my second question of: is there any reason why I "shouldn't" use tables? Or is that enough a 2nd offical question/thread
Dorjan
I think jeroen is right... my answer that i posted earlier only works if you have a height set on the container.If you do it with javascript look for a property on the div called scrollHeight or overflowHeight. That will give you the height of the div and it can be used to set the height of the other divs.
Zoidberg
There are many reasons not to use tables, just look around at SO (rendering time, screenreaders, etc). However there are also valid reasons to use tables but I would definitely not recommend wrapping your whole page in one. I guess you have to decide what´s worse, tables or javascript :-)
jeroen
hmm, I just wanted my site to be fully displayed without or with Javascript... :(
Dorjan
@Zoidberg: I don´t know about the cross-browser compatibility of your solution, I normally use jQuery already and then calculating and setting height is pretty trivial.
jeroen
@Dorjan: In my experience you can solve *almost* all problems using the fauxcolumns method (faking it).
jeroen
@jeroen: I have used it on a few applications, usually if offsetHeight isn't there, then scrollHeight is, but if JQuery provides this functionality then I would absolutely use that instead, let them worry about the cross browser issues I say.
Zoidberg
The problem here jeroen is that the right col top needs to be transparent...
Dorjan
A: 

If you just want it to appear that the columns are the same length, you can use this ugly hack:

.rightDiv, .leftDiv
{
    margin-bottom: -1000px;
    padding-bottom: 1000px; /*1000 + real padding*/
}
.wrapper
{
    overflow: hidden;
}

Basically, it causes both columns to extend 1000px further than they should, with the unwanted bits hidden. Won't work with bottom borders though.

Obviously, this doesn't actually make them the same length.

Eric
Interesting hack thanks but I'd rather not :)
Dorjan