More concise CSS clean floating Way

Source: Internet
Author: User
Tags visibility

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.