一、BFC解决父元素高度塌陷
使用特性:
- 开启BFC的父元素可以包含浮动元素
如何开启
给父元素设置:overflow: hidden; (副作用小)
<style>
.outer {
border: 1px solid red;
overflow:hidden;
}
.inner {
width: 200px;
height: 200px;
background-color: #bfc;
float: left;
}
</style>
</head>
<body>
<div class="outer">
<div class="inner"></div>
</div>
</body>
二、BFC解决外边距传递
使用特性:
- 父元素的垂直外边距不会和子元素重叠
如何开启:
给父元素设置:overflow: hidden; (副作用小)
<style>
.outer {
width: 200px;
height: 200px;
background-color: red;
overflow: hidden;
}
.inner {
width: 100px;
height: 100px;
background-color: #bfc;
margin-top: 100px;
}
</style>
</head>
<body>
<div class="outer">
<div class="inner"></div>
</div>
</body>
三、BFC解决外边距重叠
使用特性:
- 父元素的垂直外边距不会和子元素重叠
如何开启:
给父元素设置:overflow: hidden; (副作用小)
<style>
.box {
width: 200px;
height: 200px;
background-color: red;
margin-bottom: 100px;
}
.box2{
width: 200px;
height: 200px;
background-color: #bfc;
margin-top: 100px;
}
.outer {
/* 开启BFC */
overflow: hidden;
/* display: table; */
}
</style>
</head>
<body>
<div class="outer">
<div class="box"></div>
</div>
<div class="box2"></div>
</body>
四.BFC解决元素覆盖
使用特性:
- 开启BFC的元素不会被浮动元素覆盖
开启BFC:
display:inline-block
<style>
.box1{
width: 200px;
height: 200px;
background-color: red;
float: left;
}
.box2{
width: 100%;
height: 200px;
background-color: #bfc;
display: inline-block;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
</body>