Design Pattern: Self-clearing floats
January 8, 2009Originally published on the TOPP Design Blog
Problem Summary
An element needs to be expanded to contain all child elements, one or more of which may be floated (and thus removed from the normal document flow).
Use When
The page contains floated elements which should not extend beyond their parent element.
Solution
Use self-clearing floats.
Rationale
While the “use a float to contain a float” approach will ensure that all elements are appropriately contained, it can trigger other renderbugs which this approach avoids.
Example
CSS
.selfclear:after {
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
.selfclear {
display: inline-block; /* IE 7 */
}
.selfclear {
display: block;
}
* html .selfclear {
height: 1px; /* IE < 7 */
}
Note: this method builds upon the approach documented at http://positioniseverything.net/easyclearing.html.

This was invaluable for something I was just struggling with. Thank you!