This question already has an answer here:
- What methods of ‘clearfix’ can I use? 28 answers
Although elements like <div>
s normally grow to fit their contents, using the float
property can cause a startling problem for CSS newbies: if floated elements have non-floated parent elements, the parent will collapse.
For example:
<div>
<div style="float: left;">Div 1</div>
<div style="float: left;">Div 2</div>
</div>
The parent div in this example will not expand to contain its floated children - it will appear to have height: 0
.
How do you solve this problem?
I would like to create an exhaustive list of solutions here. If you're aware of cross-browser compatibility issues, please point them out.
Solution 1
Float the parent.
<div style="float: left;">
<div style="float: left;">Div 1</div>
<div style="float: left;">Div 2</div>
</div>
Pros: Semantic code.
Cons: You may not always want the parent floated. Even if you do, do you float the parents' parent, and so on? Must you float every ancestor element?
Solution 2
Give the parent an explicit height.
<div style="height: 300px;">
<div style="float: left;">Div 1</div>
<div style="float: left;">Div 2</div>
</div>
Pros: Semantic code.
Cons: Not flexible - if the content changes or the browser is resized, the layout will break.
Solution 3
Append a "spacer" element inside the parent element, like this:
<div>
<div style="float: left;">Div 1</div>
<div style="float: left;">Div 2</div>
<div class="spacer" style="clear: both;"></div>
</div>
Pros: Straightforward to code.
Cons: Not semantic; the spacer div exists only as a layout hack.
Solution 4
Set parent to overflow: auto
.
<div style="overflow: auto;">
<div style="float: left;">Div 1</div>
<div style="float: left;">Div 2</div>
</div>
Pros: Doesn't require extra div.
Cons: Seems like a hack - that's not the overflow
property's stated purpose.
add this in the parent div at the bottom
I usually use the
overflow: auto
trick; although that's not, strictly speaking, the intended use for overflow, it is kinda related - enough to make it easy to remember, certainly. The meaning offloat: left
itself has been extended for various uses more significantly than overflow is in this example, IMO.Although the code isn't perfectly semantic, I think it's more straightforward to have what I call a "clearing div" at the bottom of every container with floats in it. In fact, I've included the following style rule in my reset block for every project:
If you're styling for IE6 (god help you), you might want to give this rule a 0px line-height and height as well.
My favourite method is using a clearfix class for parent element
One of the most well known solutions is a variation of your solution number 3 that uses a pseudo element instead of a non-semantic html element.
It goes something like this...
You place that in your stylesheet, and all you need is to add the class 'cf' to the element containing the floats.
What I use is another variation which comes from Nicolas Gallagher.
It does the same thing, but it's shorter, looks neater, and maybe used to accomplish another thing that's pretty useful - preventing the child elements' margins from collapsing with it's parents' (but for that you do need something else - read more about it here http://nicolasgallagher.com/micro-clearfix-hack/ ).
Solution 1:
The most reliable and unobtrusive method appears to be this:
Demo: http://jsfiddle.net/SO_AMK/wXaEH/
HTML:
CSS:
With a little CSS targeting you don't even need to add a class to the parent
DIV
.This solution is backwards compatible to IE8 so you don't need to worry about older browsers failing.
Solution 2:
An adaptation on solution 1 has been suggested and is as follows:
Demo: http://jsfiddle.net/wXaEH/162/
HTML:
CSS:
This solution appears to be backwards compatible to IE5.5 but is untested.
Solution 3:
It's also possible to set
display: inline-block;
andwidth: 100%;
to emulate a normal block element while not collapsing.Demo: http://jsfiddle.net/SO_AMK/ae5ey/
CSS:
This solution should be backwards compatible to IE5.5 but has only been tested in IE6.