


Advantages and Disadvantages of Seven Methods to Use CSS to Clear Floats
1, parent p definition height
<style type="text/css"> .p1{background:#000080;border:1px solid red;/*解决代码*/height:200px;} .p2{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> <p class="p1"> <p class="left">Left</p> <p class="right">Right</p> </p> <p class="p2"> p2 </p>
Principle: Parent p manually defines height, which solves the problem that parent p cannot automatically obtain the height.
Advantages: simple, less code, easy to master
Disadvantages: only suitable for layouts with a fixed height. To give a precise height, problems will occur if the height is different from the parent p
Recommendation: It is not recommended. It is only recommended to use
2 for a fixed-height layout, and add an empty p tag at the end clear:both
<style type="text/css"> .p1{background:#000080;border:1px solid red} .p2{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> <p class="p1"> <p class="left">Left</p> <p class="right">Right</p> <p class="clearfloat"></p> </p> <p class="p2"> p2 </p>
Principle: Add an empty p, use the clear:both improved by CSS to clear the float, so that the parent p can automatically obtain 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 is too many, a lot of empty ps will be added, which makes people feel bad
Suggestion: Not recommended Use, but this method is a clear floating method that was mainly used before
3, parent p definitionpseudo class:after and zoom
<style type="text/css"> .p1{background:#000080;border:1px solid red;} .p2{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> <p class="p1 clearfloat"> <p class="left">Left</p> <p class="right">Right</p> </p> <p class="p2"> p2 </p>
Principle: Only IE8 and above and non-IE browsers support:after. The principle is somewhat similar to method 2. zoom (IE transfer 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 Beginners don’t understand the principle, so they need to use two lines of code in order to be supported by mainstream browsers.
Recommendation: It is recommended to use it. It is recommended to define public classes to reduce CSS code.
4, parent p definition overflow:hidden
<style type="text/css"> .p1{background:#000080;border:1px solid red;/*解决代码*/width:98%;overflow:hidden} .p2{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> <p class="p1"> <p class="left">Left</p> <p class="right">Right</p> </p> <p class="p2"> p2 </p>
Principle: width or zoom:1 must be defined, and height cannot be defined. When using overflow:hidden, browse 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 exceeded 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 p defines overflow:auto
<style type="text/css"> .p1{background:#000080;border:1px solid red;/*解决代码*/width:98%;overflow:auto} .p2{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> <p class="p1"> <p class="left">Left</p> <p class="right">Right</p> </p> <p class="p2"> p2 </p>
Principle: width or zoom:1 must be defined, and height cannot be defined. When using overflow:auto, the browser will automatically check the floating area The height
Advantages: Simple, less code, good browser support
Disadvantage: When the internal width and height exceed the parent p, a scroll bar will appear.
Suggestion: 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 p also floats together
<style type="text/css"> .p1{background:#000080;border:1px solid red;/*解决代码*/width:98%;margin-bottom:10px;float:left} .p2{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> <p class="p1"> <p class="left">Left</p> <p class="right">Right</p> </p> <p class="p2"> p2 </p>
Principle: All codes float together and become a whole
Advantages: No advantages
Disadvantages: New floating problems will occur.
Suggestion: Not recommended for use, only for understanding.
7, parent p defines display:table
<style type="text/css"> .p1{background:#000080;border:1px solid red;/*解决代码*/width:98%;display:table;margin-bottom:10px;} .p2{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> <p class="p1"> <p class="left">Left</p> <p class="right">Right</p> </p> <p class="p2"> p2 </p>
Principle: Turn p attribute into table
Advantages: No advantages
Disadvantages: New unknown problems will arise.
Suggestion: Not recommended for use, only for understanding.
8, add br tag at the end clear:both
<style type="text/css"> .p1{background:#000080;border:1px solid red;margin-bottom:10px;zoom:1} .p2{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> <p class="p1"> <p class="left">Left</p> <p class="right">Right</p> <br class="clearfloat" /> </p> <p class="p2"> p2 </p>
The above is the detailed content of Advantages and Disadvantages of Seven Methods to Use CSS to Clear Floats. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

It's out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That's like this.

I'd say "website" fits better than "mobile app" but I like this framing from Max Lynch:

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...
