Let's say I have 2 divs within a wrapper side by side.
<div id="wrapper">
<div id="primary"></div>
<div id="secondary"></div>
</div>
#primary {
width:50%;
float: left;
}
#secondary {
width: 50%;
}
How can I make sure div secondary always has the same height as div primary
If you specify the height value for their container let say
#wrapper {height:300px;}
, you can just set the the#primary
and the#secondary
height value to100%
. But if you don't want to specify any height value then you can usedisplay:table
option like in the example here http://jsfiddle.net/qiqiabaziz/LFEF5/just make sure the parent div (div wrapper) has a width in pixel
this will work, unless div primary has margin and/or padding
Make the two divs of equal height (either by declaring their heights in px, em or %) and declare their
overflow : auto
, so if content in any or both divs increases, scroll is provided automatically and their heights do not get disturbed.try using javascript taking the value of the primary div an assignment at the second div.
The other way is trying the use pixel px or em, this way you ensure always has the same height both
Unfortunately there is no perfect method to do this without using Javascript as realistically the two divs know nothing about one another.
What your options are depends on what exactly you were looking to achieve visually.
A quick google search brought this up which looks quite promising: http://www.vanseodesign.com/css/equal-height-columns/
If you can focus on more modern browsers you may be able to get away with using flexbox. See this post for examples etc: http://css-tricks.com/fluid-width-equal-height-columns/