BFC很神奇
折叠问题
<!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>
.box1 {
width: 200px;
height: 100px;
background-color: red;
margin-bottom: 20px;
}
.box2 {
margin-top: 30px;
width: 200px;
height: 100px;
background-color: orange;
}
.content {
overflow: auto;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
<p>解决之后:</p>
<div class="content">
<div class="box1"></div>
</div>
<div class="box2"></div>
<img
src="../../images/创建bfc.png"
alt=""
style="width: 700px; height: 300px"
/>
</body>
</html>
高度坍塌
<!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>
.content {
background-color: orange;
overflow: auto;
}
.item {
width: 500px;
height: 200px;
border: 1px solid red;
float: left;
}
</style>
</head>
<body>
<div class="content clear_float">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
</div>
</body>
</html>