Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS container div not getting height

I want my container div to get the height of max of its children's height. without knowing what height the child divs are going to have. I was trying out on JSFiddle. The container div is on red. which is not showing up. Why?

like image 302
Neel Basu Avatar asked Oct 19 '11 06:10

Neel Basu


People also ask

Why does my div have no height?

The reason why the height or the containers is 0 is because you are floating all the content inside them and floated elements don't expand the height (or width) of their parents.

How do you find the height of a container in CSS?

$(divToResize). css('height',$(container). innerHeight()); $(divToResize) is the selector for the DIV you wish to match the height of it's container and $(container) is logically the container whose height you want to get.

How do I stretch a div to fit a container?

Method 2: We can make the display attribute of the child container to table-row and display attribute of parent container to table, that will take all the height available from the parent div element. To cover all the width, we can make the width of parent div to 100%.


1 Answers

Add the following property:

.c{     ...     overflow: hidden; } 

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.

.c:after{     clear: both;     content: "";     display: block; } 

http://jsfiddle.net/gtdfY/368/

like image 85
Nightfirecat Avatar answered Oct 21 '22 07:10

Nightfirecat