块级格式化上下文
BFC
块级格式化上下文Block Formatting Context
,是Web
页面的可视CSS
渲染的一部分,是块盒子的布局过程发生的区域,也是浮动元素与其他元素交互的区域,是用于布局块级盒子的一块渲染区域,并且与这个区域的外部毫无关系,是一个独立的区域,是一个环境。
触发BFC
- 根元素
<html>
。
- 浮动元素,元素的
float
不是none
。
- 绝对定位元素,元素的
position
为absolute
或fixed
。
- 行内块元素,元素的
display
为inline-block
。
- 表格单元格,元素的
display
为table-cell
。
- 表格标题,元素的
display
为table-caption
。
- 匿名表格单元格元素,元素的
display
为table
、table-row
、table-row-group
、table-header-group
、table-footer-group
、inline-table
。
overflow
值不为visible
的块元素。
display
值为flow-root
的元素。
contain
值为layout
、content
或paint
的元素。
- 弹性元素,
display
为flex
或inline-flex
元素的直接子元素。
- 网格元素,
display
为grid
或inline-grid
元素的直接子元素。
- 多列容器,元素的
column-count
或column-width
不为auto
,包括column-count
为1
。
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 29 30 31 32 33
| <!DOCTYPE html> <html> <head> <title>避免浮动溢出</title> <style type="text/css"> .parent{ border: 1px solid #eee; } .child{ float: left; width: 100px; height: 300px; background-color: red; } </style> </head> <body> <div class="parent"> <div class="child"></div> </div>
<div style="clear: both;height: 20px;"></div>
<div class="parent" style="display: flex;"> <div class="child"></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 33 34 35 36 37
| <!DOCTYPE html> <html> <head> <title>避免外边距合并</title> <style type="text/css"> .parent{ border: 1px solid #eee; } .child{ height: 100px; width: 100px; margin: 10px; background-color: red; } </style> </head> <body> <div class="parent"> <div class="child"></div> <div class="child"></div> </div>
<div style="height: 20px;"></div>
<div class="parent" > <div class="child"></div> <div style="display: flow-root;"> <div class="child"></div> </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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| <!DOCTYPE html> <html> <head> <title>避免浮动文字环绕</title> <style type="text/css"> .parent{ border: 1px solid #eee; } .child{ float: left; width: 100px; height: 100px; background-color: red; } </style> </head> <body> <div class="parent"> <div class="child"></div> <div> <div>1</div> <div>1</div> <div>1</div> <div>1</div> <div>1</div> <div>1</div> <div>1</div> <div>1</div> <div>1</div> <div>1</div> </div> </div>
<div style="clear: both;height: 20px;"></div>
<div class="parent" > <div class="child"></div> <div style="display: inline-block;"> <div>1</div> <div>1</div> <div>1</div> <div>1</div> <div>1</div> <div>1</div> <div>1</div> <div>1</div> <div>1</div> <div>1</div> </div> </div> </body> </html>
|
参考
1 2 3 4 5 6 7
| https://www.jianshu.com/p/0d713b32cd0d https://segmentfault.com/a/1190000009624181 https://segmentfault.com/a/1190000013514597 https://blog.csdn.net/qq_41257129/article/details/89641726 https://blog.csdn.net/sinat_36422236/article/details/88763187 https://developer.mozilla.org/zh-CN/docs/Web/Guide/CSS/Block_formatting_context https://developer.mozilla.org/zh-CN/docs/Web/CSS/CSS_Flow_Layout/Intro_to_formatting_contexts
|