I have three divs in a container: http://jsfiddle.net/fBe9y/
One div has a lot of content. How do I get the other two div
s, with less content, to match the height of the longest div
?
I tried adding height: 100%
to all the div
s, but it doesn't work because that would need a height
on div.container
, which I don't know before rendering.
Basically what you do is make both divs/columns very tall by adding a padding-bottom: 100% and then "trick the browser" into thinking they aren't that tall using margin-bottom: -100% .
Answer: Use the CSS3 flexbox With CSS3 flex layout model you can very easily create the equal height columns or <div> elements that are aligned side by side. Just apply the display property with the value flex on the container element and the flex property with the value 1 on child elements.
You can use 2 properties, clientHeight and offsetHeight to get the height of the div. clientHeight includes padding of the div. offsetHeight includes padding, scrollBar, and borders of the div.
height() It returns the current height of the element. It does not include the padding, border or margin. It always returns a unit-less pixel value. Note: The height() will always return the content height, regardless of the value of CSS box-sizing property.
I recommend using display: table-row;
and display: table-cell;
for this. In short, what you do is make a table layout, but using <div>
tags, and then style them to behave like a table.
This is better than just using a table for semantic and accessibility reasons.
But generally speaking, CSS does not give you many ways to refer to an element's siblings this way. The <table>
tag does, but then it confuses screen readers and things.
If you wanted more rows, you would have more .container
<div>
s, and then create another <div>
wrapping them all, and give it display: table;
.
So with the same HTML you had, this CSS does what you want:
.container { display: table-row; } .tile { display: table-cell; width: 100px; background: #eee; border: 1px solid black; }
See Fiddle.
Of note: while display: table;
et al. are widely supported, IE did not add support until version 8. If you plan on supporting this for IE 7 or lower, you'll be forced to use a more complicated approach, like @Hristo's.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With