CSS clear use of floating clearfix:after

Source: Internet
Author: User
Tags visibility

If there is a div container outside, and its internal div container sets the float style, the outer container div cannot be stretched because there is no clear inside.
Workaround:

CSS code:

Copy Code code as follows:


. 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 * *





//clearfix CSS uses the pseudo object after this


//Applies the content in the end of the Clearfix element


//Transfer character "", Mac IE browser will ignore this paragraph hack, but Windows IE will not

Here is a detailed explanation:

The first thing to clear the floating principle is that the element leaves enough vertical margins to float the element.

Copy Code code as follows:


<div >


<div style= "Float:left" ></div>


</div>

The effect is that the outer div will not contain the inline div. Because the floating element is detached from the original document stream.
Take this as an example: Generally, there are three ways to clear floating
。 First: Let the outer element also float, for example:

Copy Code code as follows:


<div style= "Float:left" >


<div style= "Float:left" ></div>


</div>

Then add the Clear:both effect to the element other than this element.
Second: Add an element to the end of the outer div and use this element to float clearly.
Specific usage:

Copy Code code as follows:


<div style= "Clearfix" >


<div style= "Float:left" ></div>


</div>


. clearfix:atfer{


content: ".";


Display:block;


Visibility:hidden;


height:0;


Clear:both;


}


. Clearfix{display:inline-block}
. Clearfix{display:block}
The latter two are set for compatibility with other browsers. Because: After not all browsers are supported.
Third: Using the overflow attribute

Copy CodeThe code is as follows:


<div style= "Overflow:hidden" >


<div style= "Float:left" ></div>


</div>
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.