I have a little peculiar problem that I currently solve using a table
, see below. Basically, I want to have two divs take up 100% of the available width, but only take up as much vertical space as needed (which isn't really that obvious from the picture). The two should at all times have the exact same height with a little line between them, as shown.
alt text http://pici.se/pictures/qJRMtoLPv.png
This is all very easy to do using table
, which I'm currently doing. However, I'm not too keen on the solution, as semantically this is not actually a table.
Using JS, use
data-same-height="group_name"
in all the elements you want to have the same height.The example: https://jsfiddle.net/eoom2b82/
The code:
I had similar problem and in my opinion best option is to use just a little bit of javascript or jquery.
You can get wanted divs to be same height by getting highest div value and applying that value to all other divs. If you have many divs and many solutions i suggest to write little advance js code to find out which of all divs is the highest and then use it's value.
With jquery and 2 divs it's very simple, here is example code:
$('.smaller-div').css('height',$('.higher-div').css('height'));
And for the end, there is 1 last thing. Their padding (top and bottom) must be the same ! If one have larger padding you need to eliminate padding difference.
This works for me in IE 7, FF 3.5, Chrome 3b, Safari 4 (Windows).
Also works in IE 6 if you uncomment the clearer div at the bottom. Edit: as Natalie Downe said, you can simply add
width: 100%;
to#container
instead.I don't know a CSS way to vertically center the text in the right div if the div isn't of fixed height. If it is, you can set the
line-height
to the same value as the div height and put an inner div containing your text withdisplay: inline; line-height: 110%
.you can get this working with js:
You should use flexbox to achieve this. It's not supported in IE8 and IE9 and only with the -ms prefix in IE10, but all other browsers support it. For vendor prefixes, you should also use autoprefixer.
You can get equal height columns in CSS by applying bottom padding of a large amount, bottom negative margin of the same amount and surrounding the columns with a div that has overflow hidden. Vertically centering the text is a little trickier but this should help you on the way.
I think it worth mentioning that
the previous answer by streetpc has invalid html, the doctype is XHTML and there are single quotes around the attributes. Also worth noting is thatyou dont need an extra element withclear
on in order to clear the internal floats of the container. If you use overflow hidden this clears the floats in all non-IE browsers and then just adding something to give hasLayout such as width or zoom:1 will cause IE to clear its internal floats.I have tested this in all modern browsers FF3+ Opera9+ Chrome Safari 3+ and IE6/7/8. It may seem like an ugly trick but it works well and I use it in production a lot.
I hope this helps.