Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Div height:0px does not work?

Tags:

html

css

<div style="height:0px;max-height:0px"> </div> 

Setting a div height to 0px does not seem to work.

The div expands to show its contents, how do we prevent this from happening?

like image 870
coder_bro Avatar asked Jun 04 '09 14:06

coder_bro


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.

Why is HTML height zero?

Height = 0 because the parent of the elements have 0 height, so make sure your parent, which is body here have 100% height or else.


2 Answers

If you really want to be sure it's gonna be have no height you could use something like this:

display: block; line-height:0; height: 0; overflow: hidden; 

If you're still having problems on IE, you could also add

zoom: 1; 

to it in a stylesheet targeted at IE with a conditional comment. That'll trigger the hasLayout property in IE.

And display:none isn't the same as setting it to zero height. Just look at the various clearfix solutions for a case where not removing it from the flow is crucial.

like image 54
Gabriel Hurley Avatar answered Sep 21 '22 14:09

Gabriel Hurley


Set overflow:hidden. Otherwise the content will expand the wrapping element.

like image 35
Gumbo Avatar answered Sep 21 '22 14:09

Gumbo