CSS floats: container does not stretch to fit floated contents
This article was formerly titled “Alternative to the PIE clearfix hack” but has since been rewritten.
Getting this problem?

Example:
Left
Left
Left
Right
Right
Right
<div style="background: red; padding: 2%; border: 1px solid #000;">
<div style="border: 1px solid #000000; background: #e2e2e2; float: left; width: 45%;">Left<br />Left<br />Left<br />Left</div>
<div style="border: 1px solid #000000; background: #e2e2e2; float: right; width: 45%;">Right<br />Right<br />Right<br /></div>
</div>
There are generally two ways of dealing with this and you shouldn’t usually need to hack it.
Hack-free approach
Normally, you can apply something like the following to the outer div:
height: 1%; overflow: hidden;
In most modern browsers, the container will now stretch to fit the floated contents.
<div style="background: red; padding: 2%; border: 1px solid #000; height: 1%; overflow: hidden;">
<div style="border: 1px solid #000000; background: #e2e2e2; float: left; width: 45%;">Left<br />Left<br />Left<br />Left</div>
<div style="border: 1px solid #000000; background: #e2e2e2; float: right; width: 45%;">Right<br />Right<br />Right<br /></div>
</div>
PIE clearfix hack
In some circumstances, especially when support with ancient browsers is required, there’s also a generally trouble-free hack.
PIE Clearfix works like this:
.clearfix:after {content: "."; display: block; height: 0; clear: both; visibility: hidden;}
.clearfix {display: inline-block;}
/* Hides from IE-mac \*/
* html .clearfix {height: 1%;}
.clearfix {display: block;}
/* End hide from IE-mac */
Now simply add:
class="clearfix"
… to the outer div or apply it to the actual elements/classes you’ve already written.
I wrote this short article because it took me a painfully long time to find the original PIE article. The reason being that searching for this particular CSS problem with keywords is difficult: how do you describe it? I’ve seen “nested div float problems”, “clearing nested floats” and simply “container div won’t float” and all of these mean the same thing!
Comments
3 responses so far to CSS floats: container does not stretch to fit floated contents
Why not give me your comments?
See also:
Solution to nested DIV float problems
This item is now out of date. See the updated version. I recently discovered a CSS hack that has come in very handy indeed since. The problem arises in both Firefox and ...
- Originally published: 12 Sep 2005 in Technical
IE7 only: float bug on elements with italic, background properties
Setting font-style:italic in your CSS for elements in the vicinity of a floated element can actually trigger a float bug in Internet Explorer 7. This is only a problem, so far ...
- Originally published: 13 Jul 2009 in Technical
Testing CSS for print media: we need a Print Preview for developers!
Source: Wikimedia Commons The virtues of @media print have been explained elsewhere, best of all by Eric Meyer in his seminal A List Apart article Going to Print, whose original publish ...
- Originally published: 15 Jun 2009 in Technical
Am I a DIV or what?
Little by little, I’m being dragged into the 21st century (no coincidence that my boss’ name is Little) of CSS. Bronwen Hodgkinson’s beautiful “Kelmscott” design at csszengarden.com I’ve been doing CSS layouts ...
- Originally published: 28 Jul 2005 in Technical
Out of touch
About eight years ago when I was still a student, I visited New York and Washington DC. Washington is home to many of the United States’ government agencies including the ...
- Originally published: 6 Feb 2007 in Technology
Who is that guy?
Hello you. I'm Mike Padgett and I work in the technology sector as an Information Designer.
I also enjoy travel, concerts, films and walking.
I'm based in Brussels, Belgium. My current favourite Belgian beer is St Feuillien Brune.



June 29th, 2009 at 20:46
Thanks for this post! I could not think of how to describe the problem, but your article was one of the first search results.
August 30th, 2009 at 17:59
Thanks a lot
September 11th, 2009 at 2:09
First time using css, divs and floats, I spent 2 days figuring out the problem… MAN!!!! what a way to learn!
I would-of given up on the whole thing if it weren’t for you!
Thanks a lot!!!!