I want my container div to get the height of max of its children's height. without knowing what height the child div
s are going to have. I was trying out on JSFiddle. The container div
is on red. which is not showing up. Why?
CSS container div not getting height
108.3k Views Asked by Neel Basu AtThere are 7 best solutions below

I ran into this same issue, and I have come up with four total viable solutions:
- Make the container
display: flex;
(this is my favorite solution) - Add
overflow: auto;
oroverflow: hidden;
to the container - Add the following CSS for the container:
.c:after {
clear: both;
content: "";
display: block;
}
- Make the following the last item inside the container:
<div style="clear: both;"></div>

Try inserting this clearing div before the last </div>
<div style="clear: both; line-height: 0;"> </div>

You are floating the children which means they "float" in front of the container. In order to take the correct height, you must "clear" the float
The div style="clear: both" clears the floating an gives the correct height to the container. see http://css.maxdesign.com.au/floatutorial/clear.htm for more info on floats.
eg.
<div class="c">
<div class="l">
</div>
<div class="m">
World
</div>
<div style="clear: both" />
</div>

The best and the most bulletproof solution is to add ::before
and ::after
pseudoelements to the container. So if you have for example a list like:
<ul class="clearfix">
<li></li>
<li></li>
<li></li>
</ul>
And every elements in the list has float:left
property, then you should add to your css:
.clearfix::after, .clearfix::before {
content: '';
clear: both;
display: table;
}
Or you could try display:inline-block;
property, then you don't need to add any clearfix.

I just ran into the same issue and after some research I belive using display: flow-root
on the containing element is the correct approach.
From MDN on the clear
property:
If an element contains only floated elements, its height collapses to nothing. If you want it to always be able to resize, so that it contains floating elements inside it, set the value of the element's display property to flow-root.
So while clear: both
works (as other answers suggest), it is actually designed to correctly position other elements inside the same container respecting the height of the floating elements. display: flow-root
should be used to make the containing element respect the heights of all it's children so other elements (e.g. siblings to the containing element) can be positioned properly.
Add the following property:
This will force the container to respect the height of all elements within it, regardless of floating elements.
http://jsfiddle.net/gtdfY/3/
UPDATE
Recently, I was working on a project that required this trick, but needed to allow overflow to show, so instead, you can use a pseudo-element to clear your floats, effectively achieving the same effect while allowing overflow on all elements.
http://jsfiddle.net/gtdfY/368/