Article Introduction: There are many ways to clean up floats, like using the clear genus with the BR tag, using the element's overflow, using an empty tag to set the Clear:both, and so on. However, given the compatibility and semantic issues, we generally use the following code to clean up the float. |
There are many ways to clean up floats, like using the clear genus with the BR tag, using the element's overflow, using an empty tag to set the Clear:both, and so on. However, given the compatibility and semantic issues, we generally use the following code to clean up the float.
* * Clean FLOAT * *
. clearfix:after {
Visibility:hidden;
Display:block;
font-size:0;
Content: "";
Clear:both;
height:0;
}
. clearfix {
Zoom:1;
}
The rationale is, in the Advanced browser, the after pseudo class adds a display:none, invisible block content to the floating block, and sets it to clear:both to clear the float. Add Haslayout to the floating block in IE6 and 7 to keep the floating block high and affect the document flow normally.
The above code should be the current mainstream cleanup floating mode. Alipay is now used in this way. Now, Nicolas Gallagher has given a more concise solution:
. Cf:before,. Cf:after {
Content: "";
display:table;
}
. cf:after {
Clear:both;
}
. CF {
Zoom:1;
}
The principle is still the same. Use: After pseudo class to provide clear:both after floating block. The difference is that hiding this space is using display:table. rather than setting visibility:hidden;height:0;font-size:0; Such a hack.
It is noteworthy here: the Before pseudo class. In fact, he is to deal with the top-margin edge folding, with the clean float is not much of a relationship. However, because floats create the block formatting context, you should let them not fold (although this is not common) if there is a margin-bottom on another element on the floating element and the floating element is just margin-top.