谈谈你对BFC的理解
BFC是什么
BFC 是 Block Formatting Context (块级格式上下文)的缩写
BFC是一个独立的空间,里面子元素的渲染不影响外面的布局
他用于清除浮动和解决margin塌方问题
如何触发BFC
- overflow: hidden
- display: inline-block / table-cell / flex
- position: absolute / fixed
以上都可触发BFC
代码演示
解决margin塌方问题
<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>bfc解决margin塌方问题</title>
</head>
<style>
*{
margin: 0;
padding: 0;
}
section
{
width: 200px;
height: 200px;
background: aqua;
margin: 50px;
}
.box {
overflow: hidden;
}
</style>
<body>
<div class="box">
<section>
secton1
</section>
</div>
<section>
secton2
</section>
</body>
</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>清除浮动</title>
</head>
<style>
* {
margin: 0;
padding: 0;
}
section {
width: 200px;
height: 200px;
background: aqua;
margin: 20px;
}
.box {
display: inline-block;
background: aquamarine;
}
.left{
float: left;
}
.right{
float: right;
}
</style>
<body>
<div class="box">
<section class="left">left</section>
<section class="right">tight</section>
</div>
</body>
</html>