A total of 8 methods to clear floating CSS_html/css_WEB-ITnose

WBOY
Release: 2016-06-24 11:40:12
Original
1467 people have browsed it

The display effect in various browsers may also be different, which makes clearing floats more difficult. Here are 8 ways to clear floats. The test has passed ie chrome firefox opera. Friends who need it You can refer to the following

Clearing floats is a function that every web front-end designer must master. css clear float encyclopedia, a total of 8 methods.

Floating will cause the current label to float upward, and will also affect the position of the front and rear labels, the parent label and the width height attribute. Moreover, the same code may display differently in various browsers, which makes clearing floats more difficult. There are many ways to solve problems caused by floats, but some have issues with browser compatibility.

The following summarizes 8 methods of clearing floats (the test has passed ie chrome firefox opera, the next three methods are just for understanding):

1, parent div defines height

<style type="text/css"> .div1{background:#000080;border:1px solid red;/*解决代码*/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>
Copy after login

 

Principle: Manually defining the height of the parent div solves the problem that the parent div cannot automatically obtain the height.

Advantages: simple, less code, easy to master

Disadvantages: only suitable for fixed-height layouts. Accurate height must be given. If the height is different from the parent div, problems will occur.

Recommendation: Not recommended, only recommended for fixed-height layouts

2, add an empty div tag at the end clear:both

<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} /*清除浮动代码*/ .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> 
Copy after login

 

Principle: Add an empty div, use the clear:both improved by css to clear the float, so that the parent div can automatically obtain the height

Advantages: simple, Less code, good browser support, and less prone to strange problems

Disadvantages: Many beginners do not understand the principle; if the page has a lot of floating layouts, a lot of empty divs will be added, which makes people feel bad

Recommendation: Not recommended, but this method is a method of clearing floats that was mainly used in the past

3. The parent div defines pseudo-classes: after and zoom

<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} /*清除浮动代码*/ .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> 
Copy after login

 

Principle: Only IE8 and above and non-IE browsers support: after. The principle is somewhat similar to method 2. Zoom (IE conversion has attributes) can solve the floating problem of ie6 and ie7

Advantages: good browser support, not prone to strange problems (currently: used by large websites, such as: Tencent, NetEase, Sina, etc.)

Disadvantages: a lot of code, many beginners I don’t understand the principle. I need to use two lines of code together to make it supported by mainstream browsers.

Recommendation: Recommended to use, it is recommended to define public classes to reduce CSS code.

4, the parent div defines overflow:hidden

<style type="text/css"> .div1{background:#000080;border:1px solid red;/*解决代码*/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> 
Copy after login

 

Principle: width or zoom:1 must be defined, and height cannot be defined at the same time , when using overflow:hidden, the browser will automatically check the height of the floating area

Advantages: simple, less code, good browser support

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

Recommendation: Only recommended for friends who have not used position or have a deep understanding of overflow:hidden.

5, the parent div defines overflow:auto

<style type="text/css"> .div1{background:#000080;border:1px solid red;/*解决代码*/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> 
Copy after login

 

Principle: width or zoom:1 must be defined, and height cannot be defined at the same time , when using overflow:auto, the browser will automatically check the height of the floating area

Advantages: simple, less code, good browser support

Disadvantage: when the internal width and height exceed the parent div, Scroll bars will appear.

Recommendation: Not recommended, use it if you need scroll bars to appear or to ensure that scroll bars do not appear in your code.

6, the parent div also floats together

<style type="text/css"> .div1{background:#000080;border:1px solid red;/*解决代码*/width:98%;margin-bottom:10px;float:left} .div2{background:#800080;border:1px solid red;height:100px;width:98%;/*解决代码*/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> 
Copy after login

 

Principle: All codes float together and become a whole

Advantages: No advantages

Disadvantages: New floating problems will occur.

Suggestion: Not recommended for use, just for understanding.

7, the parent div defines display:table

<style type="text/css"> .div1{background:#000080;border:1px solid red;/*解决代码*/width:98%;display: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> 
Copy after login

 

Principle: Turn the div attribute into a table

Advantages: No advantages

Disadvantages: New unknown problems will arise.

Recommendation: Not recommended, just for understanding.

8, add br tag clear:both at the end

<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> <br class="clearfloat" /> </div> <div class="div2"> div2 </div>
Copy after login

 

Principle: The parent div defines zoom:1 to solve IE floating Question, add the br tag clear:both

at the end. Suggestion: Not recommended, just for understanding.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template