The principle and method of clearing floating, and the method of clearing floating principle

Source: Internet
Author: User

The principle and method of clearing floating, and the method of clearing floating principle

Cause:

In the CSS specification, the floating position is separated from the normal stream of elements. Therefore, as long as the parent container containing floating elements is displayed, the positions of child elements are not considered, and they do not exist. This results in display. The parent container is like an empty container.

For example, the following code:

1 <div class="box">2     <div style="float:left;width:100px;height:100px;"></div>3     <div style="float:right;width:100px;height:100px"></div>4 </div>

When running in a browser, the actual view displays child elements outside the parent container.

Solution 1: add an empty Element

Add a non-floating element under the floating Element

The Code is as follows:

 1 <div class="box"> 2   <div style="float:left;width:100px;height:100px;"></div> 3   <div style="float:right;width:100px;height:100px;"></div> 4   <div class="clearfix"></div> 5 </div> 6  7 <style> 8 .clearfix{ 9     clear:both;10 }11 </style>

Solution 2: Floating parent container

Change the parent container to floating location, so that it can be floating with child elements.

The Code is as follows:

 1 <div class="box"> 2     <div style="float:left;width:100px;height:100px;"></div> 3     <div style="float:right;width:100px;height:100px;"></div> 4 </div> 5  6 <style> 7 .box{ 8     float:left; 9 }10 </style>

Solution 3: automatic clearing of floating Elements

The parent container can automatically "clear" the floating of child elements to identify the position of floating child elements without any display errors.

The Code is as follows:

 1 <div class="box"> 2   <div style="float:left;width:100px;height:100px;"></div> 3   <div style="float:right;width:100px;height:100px;"></div> 4 </div> 5  6 <style> 7 .box{ 8     overflow:hidden; 9 }10 </style>

Solution 4: Add sub-elements using CSS statements so that you do not need to modify the HTML code.

Is to use the after pseudo element method to automatically create an element at the end of the container.

The Code is as follows:

 1 <div class="box"> 2   <div style="float:left;width:100px;height:100px;"></div> 3   <div style="float:right;width:100px;height:100px;"></div> 4 </div> 5  6 <style> 7 .box:after { 8     content: "\0020"; 9     display: block;10     height: 0;11     clear: both;12     zoom:1;13 }14 </style>

 

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.