CSS floating cleaning, clear: Both label is not used

Source: Internet
Author: User

For example:
<Div style = "Background: #666;"> <! -- Float container -->
<Div style = "float: Left; width: 30%; Height: 40px; Background: # Eee;"> some content </div>
</Div>
Preview this Code The parent element float container of the outermost layer is not displayed. This is because the child element is floating and is out of the Document Stream, resulting in the parent element's height being zero.
If you change the code:
<Div style = "Background: #666;"> <! -- Float container -->
<Div style = "float: Left; width: 30%; Height: 40px; Background: # Eee;"> some content </div>
<Div style = "clear: both"> </div>
</Div>
Note that there is an additional code to clear the floating. This is a good CSS code habit, but this method adds useless elements. Here is a better way to change the HTML code:
<Div class = "Clearfix" style = "Background: #666;"> <! -- Float container -->
<Div style = "float: Left; width: 30%; Height: 40px; Background: # Eee;"> some content </div>
</Div>
Define the CSS class for "floating cleaning" control: Copy code The Code is as follows:. Clearfix: After {}{
Content :".";
Clear: both;
Height: 0;
Visibility: hidden;
Display: block;
}/* This is the processing of Firefox, because Firefox supports generating elements, while all IE versions do not support generating elements */
. Clearfix {}{
Display: inline-block;
}/* This is the processing of IE browser on Mac */
/** // * Hides from IE-Mac \*/
* Html. Clearfix {} {Height: 1%;}/* This is the processing of IE browser on win */
. Clearfix {} {display: block;}/* This is the modification to display: inline-block;, reset to block element */
/** // * End hide from IE-Mac */

In this case, preview the above Code (delete this annotation) and you will find that even if the child element is floating, the parent element float container will still be surrounded and highly adaptive.

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.