1. 什么是BFC?
W3C对BFC的定义如下: 浮动元素和绝对定位元素,非块级盒子的块级容器(例如 inline-blocks, tablecells, 和 table-captions),以及overflow值不为"visiable"的块级盒子,都会为他们的内容创建新的
BFC(Block Fromatting Context, 即块级格式上下文)。
2. 触发条件
一个HTML元素要创建BFC,则满足下列的任意一个或多个条件即可: 下列方式会创建块格式化上下
文:
根元素()
浮动元素(元素的 float 不是 none)
绝对定位元素(元素的 position 为 absolute 或 fixed)
行内块元素(元素的 display 为 inline-block)
表格单元格(元素的 display为 table-cell,HTML表格单元格默认为该值)
表格标题(元素的 display 为 table-caption,HTML表格标题默认为该值)
匿名表格单元格元素(元素的 display为 table、table-row、 table-row-group、table-headergroup、table-footer-group(分别是HTML table、row、tbody、thead、tfoot的默认属性)或
inline-table)
overflow 值不为 visible 的块元素 -弹性元素(display为 flex 或 inline-flex元素的直接子元素)
网格元素(display为 grid 或 inline-grid 元素的直接子元素) 等等。
3. BFC渲染规则
(1)BFC垂直方向边距重叠
(2)BFC的区域不会与浮动元素的box重叠
(3)BFC是一个独立的容器,外面的元素不会影响里面的元素
(4)计算BFC高度的时候浮动元素也会参与计算
4. BFC应用场景
(1)避免外边距折叠
例如:
<!DOCTYPE html>
<html>
<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>
.box1 {
margin: 50px;
width: 200px;
height: 100px;
background-color: aqua;
}
.box2 {
margin: 50px;
width: 200px;
height: 100px;
background-color: blueviolet;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
</body>
</html>
结果:
改变后:
<style>
.box1 {
margin: 50px;
width: 200px;
height: 100px;
background-color: aqua;
}
.box2 {
margin: 20px;
width: 200px;
height: 100px;
background-color: blueviolet;
}
.bfc {
overflow: hidden;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="bfc"></div>
<div class="box2"></div>
</body>
结果:
(2)防止浮动导致父元素高度塌陷
例如:
<!DOCTYPE html>
<html>
<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>
.box1 {
margin: 50px;
width: 500px;
border: 8px solid #b82f2f;
}
.cont {
float: left;
width: 300px;
height: 50px;
background-color: blueviolet;
}
</style>
</head>
<body>
<div class="box1">
<div class="cont"></div>
</div>
</body>
</html>
结果
改变后:
<style>
.box1 {
margin: 50px;
width: 500px;
border: 8px solid #b82f2f;
overflow: hidden;
}
.cont {
float: left;
width: 300px;
height: 50px;
background-color: blueviolet;
}
</style>
</head>
<body>
<div class="box1">
<div class="cont"></div>
</div>
</body>
结果: