


Advantages and Disadvantages of Seven Methods to Use CSS to Clear Floats
1, parent p definition height
<style> .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> </p><p>Left</p> <p>Right</p> <p> 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> .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> </p><p>Left</p> <p>Right</p> <p></p> <p> 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> .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> </p><p>Left</p> <p>Right</p> <p> 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> .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> </p><p>Left</p> <p>Right</p> <p> 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> .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> </p><p>Left</p> <p>Right</p> <p> 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> .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> </p><p>Left</p> <p>Right</p> <p> 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> .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> </p><p>Left</p> <p>Right</p> <p> 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> .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> </p><p>Left</p> <p>Right</p> <br> <p> 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!

In a perfect world, our projects would have unlimited resources and time. Our teams would begin coding with well thought out and highly refined UX designs.

Oh, the Many Ways to Make Triangular Breadcrumb Ribbons

SVG has its own set of elements, attributes and properties to the extent that inline SVG code can get long and complex. By leveraging CSS and some of the forthcoming features of the SVG 2 specification, we can reduce that code for cleaner markup.

You might not know this, but JavaScript has stealthily accumulated quite a number of observers in recent times, and Intersection Observer is a part of that

We may not need to throw out all CSS animations. Remember, it’s prefers-reduced-motion, not prefers-no-motion.

PWA (Progressive Web Apps) have been with us for some time now. Yet, each time I try explaining it to clients, the same question pops up: "Will my users be

It's extremely surprising to me that HTML has never had any way to include other HTML files within it. Nor does there seem to be anything on the horizon that

There are a lot of different ways to use SVG. Depending on which way, the tactic for recoloring that SVG in different states or conditions — :hover,


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools