Weird Float/Height Problems and How To Solve Them!
By Seb Kay |
February 20, 2010 |
4 Comments

Ever made a site or just part of a site that never looked like the design because of a rendering problem where the height of parent element would cease to exist if you floated the child elements, so you have to give it a fixed height? Well fear no more, there is a fix! Continue Reading...
Please Note: This has only been tested in FireFox because I work with FireFox the most. However I’ve had the same problem with Safari and Opera. I know this trick works, I’ve used it many times.
I’d like to take this chance to explain to the younger-in-mind CSS coders out there, (It means your at the beginner/intermediate level), how to solve the strange no-height issue when floating child elements. I’ve seen this problem affect many advanced coders as well as the newbies, and this is it:
I set my Child Divs’ to Float and now the Parent Div has lost it’s height…WTF?!
Or
I set my Child Divs’ to Float and now the Parent Div’s height is only as high as the Child elements Paragraph tags…WTF?!
Yes, We’ve all been there. It’s a problem where the Parent element doesn’t recognise the Child elements, only the Child element’s Paragraph tags. Your code will look something like this:
The Problem
(x)HTML
<div id="parent_element">
<div class="child_element">
<p>Left</p>
</div>
<div class="child_element2">
<p>Right</p>
</div>
</div>
CSS
#parent_element {
width:600px;
padding:20px;
border:1px solid #ccc;
background:#e6e6e6;
}
.child_element {
width:260px;
height:50px;
float:left;
background:#666;
}
.child_element2 {
width:260px;
height:50px;
float:right;
background:#666;
}
The Result:

Not Pretty, Is It?
Solving The Problem With Display:Inline-Block;
We only need to add one thing. Take a look, I think I’ve made it pretty obvious
:
CSS
#parent_element {
width:600px;
padding:20px;
/***Right Here***/ display:inline-block; /***That's Right!***/
border:1px solid #ccc;
background:#e6e6e6;
}
The Magic At Work:

Final Thoughts & Wise Words
All I can say is that it works perfectly. However you can’t center the Div using margin:0px auto; so just add another wrapper. I also realize that there are other ways to fix this issue. However this one works best for me because I’m used to using it. If you enjoyed this or think I’ve failed epically, leave a comment below!
Hello!, i'm KayRose, (as you probably guessed it's not my real name ), i'm one of the editors here at BeCreative Magazine, i also coded the entire thing for my friend and fellow web enthusiast Patrick Larsson, hqmStudios, who designed it.
I love all things Wordpress, jQuery, HTML and CSS.
You can also catch me on twitter @KayRoseDesign.
4 Comments
“overflow: hidden” to the parent elements works too, and it is the standard solution these days, as far I know.
Good tip anyway.
From what I understand you mean to hide the child elements outside of the parent right?
For example on my site http://kayrosedesign.com/blog/ I used a seperator background image to keep the seperator between the left and right columns the entire height of the page, (from the bottom of the featured area to the top of the footer). Sorry if that was confusing
Also I see your point beacuse my example doesn’t work in FireFox 2.0. Then again it’s not to much of a worry.
Clearing the floated divs inside the container would also fix it, and it’s good practice to always clear floated containers anyway.
Yeah, clearing the float is the best way to fix this issue. The overflow:auto fix also works, though I prefer to clear the floats.