CSS Clear floating Daquan total 8 ways

Source: Internet
Author: User

CSS Clear floating all 8 ways to display the effect in various browsers may not be the same, so that the removal of floating more difficult, the following summary of 8 ways to clear floating, testing has passed IE chrome Firefox opera, the need for friends can refer to the next

Clear float is a function that every web foreground designer must master. CSS Clear Floating Encyclopedia, a total of 8 methods.

Floating causes the current label to float, affecting both the front and back labels, the position of the parent label, and the width height property. And the same code, in a variety of browsers display the effect may also be different, so that the removal of floating more difficult. There are several ways to solve problems caused by floating, but there are some methods that have problems with browser compatibility.

Here is a summary of 8 ways to clear the float (test has been via IE Chrome Firefox opera, the next three ways only to understand it):

1, parent div defines height

 The code is as follows:
<style type= "Text/css" >
. Div1{background: #000080; border:1px solid red;/* Solution code */height:200px;}
. Div2{background: #800080; border:1px solid red;height:100px;margin-top:10px}
. Left{float:left;width:20%;height:200px;background: #DDD}
. Right{float:right;width:30%;height:80px;background: #DDD}
</style>
<div class= "Div1" >
<div class= "left" >Left</div>
<div class= "right" >Right</div>
</div>
<div class= "Div2" >
Div2
</div>


Principle: The parent div manually defines the height, which solves the problem that the parent Div cannot automatically get to the heights.

Advantages: Simple, less code, easy to master

Cons: only suitable for highly fixed layout, to give precise height, if the height and the parent div is not the same, the problem arises

Recommendation: Not recommended, only recommended for highly fixed layouts when used

2, add empty div tag at the end Clear:both

 The code is as follows:
<style type= "Text/css" >
. Div1{background: #000080; border:1px solid Red}
. Div2{background: #800080; border:1px solid red;height:100px;margin-top:10px}
. Left{float:left;width:20%;height:200px;background: #DDD}
. Right{float:right;width:30%;height:80px;background: #DDD}
/* Clear Floating code */
. Clearfloat{clear:both}
</style>
<div class= "Div1" >
<div class= "left" >Left</div>
<div class= "right" >Right</div>
<div class= "Clearfloat" ></div>
</div>
<div class= "Div2" >
Div2
</div>


Principle: Add an empty div, use CSS to improve the clear:both clear floating, so that the parent div can automatically get to the height

Advantages: Simple, less code, good browser support, not prone to strange problems

Disadvantages: Many beginners do not understand the principle; if the page floating layout more, it is necessary to add a lot of empty Div, people feel very bad

Recommendation: Deprecated, but this method is a major use of the previously used to clear the floating method

3, Parent div definition Pseudo-class: After and zoom

The code is as follows:
<style type= "Text/css" >
. Div1{background: #000080; border:1px solid Red;}
. Div2{background: #800080; border:1px solid red;height:100px;margin-top:10px}
. Left{float:left;width:20%;height:200px;background: #DDD}
. Right{float:right;width:30%;height:80px;background: #DDD}
/* Clear Floating code */
. Clearfloat:after{display:block;clear:both;content: ""; visibility:hidden;height:0}
. clearfloat{zoom:1}
</style>
<div class= "Div1 clearfloat" >
<div class= "left" >Left</div>
<div class= "right" >Right</div>
</div>
<div class= "Div2" >
Div2
</div>


Principle: IE8 above and non-IE browser support: After, principle and method 2 somewhat similar, zoom (ie turn to have attribute) can solve ie6,ie7 floating problem

Advantages: Browser support is good, not prone to strange problems (currently: large sites are used, such as: Tencent, NetEase, Sina, etc.)

Disadvantages: Many code, many beginners do not understand the principle, to use two code together to let the mainstream browser support.

Recommendation: recommended, we recommend defining public classes to reduce CSS code.

4, Parent div definition Overflow:hidden

 The code is as follows:
<style type= "Text/css" >
. Div1{background: #000080; border:1px solid red;/* Solution Code */width:98%;overflow:hidden}
. Div2{background: #800080; border:1px solid red;height:100px;margin-top:10px;width:98%}
. Left{float:left;width:20%;height:200px;background: #DDD}
. Right{float:right;width:30%;height:80px;background: #DDD}
</style>
<div class= "Div1" >
<div class= "left" >Left</div>
<div class= "right" >Right</div>
</div>
<div class= "Div2" >
Div2
</div>


Principle: Width or zoom:1 must be defined and height cannot be defined, and when using Overflow:hidden, the browser automatically checks the altitude of the floating area

Pros: Simple, less code, good browser support

Disadvantage: cannot be used in conjunction with position, because the size of the excess will be hidden.

Recommendation: It is recommended to use only those friends who are not using position or who understand more deeply about Overflow:hidden.

5, Parent div definition Overflow:auto

 The code is as follows:
<style type= "Text/css" >
. Div1{background: #000080; border:1px solid red;/* Solution Code */width:98%;overflow:auto}
. Div2{background: #800080; border:1px solid red;height:100px;margin-top:10px;width:98%}
. Left{float:left;width:20%;height:200px;background: #DDD}
. Right{float:right;width:30%;height:80px;background: #DDD}
</style>
<div class= "Div1" >
<div class= "left" >Left</div>
<div class= "right" >Right</div>
</div>
<div class= "Div2" >
Div2
</div>


Principle: Width or zoom:1 must be defined and height cannot be defined, and when using Overflow:auto, the browser automatically checks the altitude of the floating area

Pros: Simple, less code, good browser support

Disadvantage: Scroll bars appear when the internal width is higher than the parent Div.

Recommendation: Not recommended, if you need to scroll bar or make sure your code does not appear scroll bar to use it.

6, the parent Div also floats together

 The code is as follows:
<style type= "Text/css" >
. Div1{background: #000080; border:1px solid red;/* Solution Code */width:98%;margin-bottom:10px;float:left}
. Div2{background: #800080; border:1px solid red;height:100px;width:98%;/* Solution Code */clear:both}
. Left{float:left;width:20%;height:200px;background: #DDD}
. Right{float:right;width:30%;height:80px;background: #DDD}
</style>
<div class= "Div1" >
<div class= "left" >Left</div>
<div class= "right" >Right</div>
</div>
<div class= "Div2" >
Div2
</div>


Principle: All the code floats together, it becomes a whole

Pros: No advantages

Cons: New floating issues are created.

Recommendation: Not recommended, only for understanding.

7, Parent div definition display:table

 The code is as follows:
<style type= "Text/css" >
. Div1{background: #000080; border:1px solid red;/* Solution Code */width:98%;d isplay:table;margin-bottom:10px;}
. Div2{background: #800080; border:1px solid red;height:100px;width:98%;}
. Left{float:left;width:20%;height:200px;background: #DDD}
. Right{float:right;width:30%;height:80px;background: #DDD}
</style>
<div class= "Div1" >
<div class= "left" >Left</div>
<div class= "right" >Right</div>
</div>
<div class= "Div2" >
Div2
</div>


Principle: Turning div attributes into tables

Pros: No advantages

Cons: New and unknown issues arise.

Recommendation: Not recommended, only for understanding.

8, add BR tag at the end Clear:both

 The code is as follows:
<style type= "Text/css" >
. Div1{background: #000080; border:1px solid Red;margin-bottom:10px;zoom:1}
. Div2{background: #800080; border:1px solid red;height:100px}
. Left{float:left;width:20%;height:200px;background: #DDD}
. Right{float:right;width:30%;height:80px;background: #DDD}
. Clearfloat{clear:both}
</style>
<div class= "Div1" >
<div class= "left" >Left</div>
<div class= "right" >Right</div>
&LT;BR class= "Clearfloat"/>
</div>
<div class= "Div2" >
Div2
</div>


Principle: Parent div definition zoom:1 to solve IE floating problem, add BR tag at the end Clear:both

Recommendation: Not recommended, only for understanding.

CSS Clear floating Daquan total 8 ways

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.