BFC 的理解
块级格式化上下文,它是指一个独立的块级渲染区域,只有 Block-level BOX 参与,该区域拥有套渲染规则来约束块级盒子的布局,且与区域外部无关。
从一个现象开始说起
- 一个盒子不设置 height,当内容子元素都浮动时,无法撑起自身
- 这个盒子没有形成 BFC
如何创建 BFC
- float 的值不是 none
- position 的值不是 static 或者 relative
- display 的值是 inline-block,flex,或者 inline-flex
- overflow:hidden
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> .father { overflow: hidden; } .son { width: 200px; height: 200px; background-color: aliceblue; float: left; } </style> </head> <body> <div class="father"> <div class="son"></div> <div class="son"></div> <div class="son"></div> </div> </body> </html>
|
BFC 的其他作用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> .father { width: 200px; height: 300px; background-color: antiquewhite; } .son { width: 100px; height: 100px; background-color: aliceblue; margin: 20px; } </style> </head> <body> <div class="father"> <div class="son"></div> </div> </body> </html>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> .son { width: 100px; height: 100px; background-color: aliceblue; float: left; }
.son-last { width: 200px; height: 200px; background-color: aqua; overflow: hidden; } </style> </head> <body> <div class="father"> <div class="son"></div> <div class="son"></div> <div class="son-last"></div> </div> </body> </html>
|