BFC BFC: Block Formatting Context BFC layout rules
How to create a Block Formatting Context 1. The value of float is not none. 2. The value of position is not static or relative. 3. The value of display is inline-block , table-cell , flex , table-caption or inline-flex The role of BFC 1. Use BFC to avoid margin overlap. 2. Adaptive two-column layout 3. Clear floats. Clear Float Clearing floats is mainly to solve the problem that the internal height of the parent element is 0 due to the floating of child elements. Method to clear floats 1. Additional labeling method After the last floating label, add a new label and set it to clear: both; (not recommended) Advantages: easy to understand, convenient Disadvantages: Adding meaningless tags, poor semantics <style> .div1 { background: #00a2d4; } .left { float: left; width: 200px; height: 200px; background: #9889c1; } .right { float: right; width: 200px; height: 200px; background:orangered; } .clear { clear: both; } </style> </head> <body> <div class="div1"> <div class="left">Left</div> <div class="right">Right</div> <div class="clear"></div> </div> <div class="div2"></div> </body> 2. Add overflow attribute to the parent Clear floating by triggering BFC. (Not recommended) Advantages: concise code Disadvantages: When the content increases, it is easy to cause the content to be hidden due to the failure of automatic line wrapping, and the overflowing elements cannot be displayed. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .div1 { background: #00a2d4; overflow: hidden; } .left { float: left; width: 200px; height: 200px; background: #9889c1; } .right { float: right; width: 200px; height: 200px; background:orangered; } </style> </head> <body> <div class="div1"> <div class="left">Left</div> <div class="right">Right</div> </div> <div class="div2"></div> </body> </html> 3. Use the after pseudo element to clear the float (recommended) Advantages: Conforms to the closed floating concept and has a correct structural semantics. Disadvantages: ie6-7 does not support pseudo-elements: after, use zoom: 1 to trigger hasLayout. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .div1 { background: #00a2d4; } .left { float: left; width: 200px; height: 200px; background: #9889c1; } .right { float: right; width: 200px; height: 200px; background:orangered; } .clearfix:after { content: ""; /*The content is empty*/ display: block; /*Convert to block-level element*/ height: 0; /*Height is 0*/ clear: both; /*clear float*/ visibility: hidden; /*Hide the box*/ } .clearfix { *zoom: 1; /*IE6\7 processing method*/ } </style> </head> <body> <div class="div1 clearfix"> <div class="left">Left</div> <div class="right">Right</div> </div> <div class="div2"></div> </body> </html> 4. Use before and after double pseudo elements to clear floats Advantages: Not only can it clear floating, but it can also solve the problem of height collapse (add the class name clearfix to the parent box) Disadvantage: Use zoom:1 to trigger hasLayout. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .div1 { background: #00a2d4; } .left { float: left; width: 200px; height: 200px; background: #9889c1; } .right { float: right; width: 200px; height: 200px; background:orangered; } .clearfix:after, .clearfix:before { content: ""; display: table; } .clearfix:after { clear: both; } .clearfix { *zoom: 1; } </style> </head> <body> <div class="div1 clearfix"> <div class="left">Left</div> <div class="right">Right</div> </div> <div class="div2"></div> </body> </html> Summarize This is the end of this article about CSS clear float and BFC. For more relevant CSS float BFC content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! |
<<: Press Enter to automatically submit the form. Unexpected discovery
>>: Do you know how to use mock in vue project?
Table of contents 1. MySQL data backup 1.1, mysql...
Table of contents 1. Detailed syntax of entires()...
Install mysql under win10 1. Download MySQL from ...
Table of contents Preface 1. Less 2. Import your ...
Dockerfile is a file used to build a docker image...
1. Slow query due to lack of index or invalid ind...
This article shares the specific code for WeChat ...
This article introduces the effect of website pro...
Table of contents Problems with resource manageme...
Suggestion: Handwriting code as much as possible c...
Table of contents CSS3 Box Model a. CSS3 filter b...
CentOS6.9 installs Mysql5.7 for your reference, t...
This article example shares the specific code of ...
How is the MySQL Select statement executed? I rec...
Table of contents render.js part create-context.j...