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?
MySQL temporary tables are very useful when we ne...
This article shares the specific code of React to...
1. Download the corresponding installation file f...
1 Start the Docker service First you need to know...
This article shares a digital clock effect implem...
According to the principles of W3C, each start tag...
Mixin method: The browser cannot compile: The old...
1. Commonly used high-order functions of arrays S...
This article is an integrated article on how to c...
Table of contents Overview Four examples Example ...
Table of contents Before transformation: After tr...
Vue3.0 has been out for a while, and it is necess...
This article shares the specific code of Navicat ...
This article example shares the specific code of ...
the difference: 1. InnoDB supports transactions, ...