以下两个案例分别使用HTML标签和CSS伪元素解决高度塌陷问题
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>高度塌陷的解决方法</title>
<style>
/* 方案一 */
.box1{
border: 10px tomato solid;
}
.box2{
width: 100px;
height: 100px;
background-color: wheat;
float: left; /* 设置box2靠左侧浮动 */
}
.box3{
clear: both; /* 清除box3的侧面浮动 */
}
/* 方案二 */
.box4{
border: 10px tomato solid;
}
.box5{
width: 100px;
height: 100px;
background-color: wheat;
float: left; /* 设置box5靠左侧浮动 */
}
.box4::after{ /* 对box4的末尾进行操作 */
content: ''; /* 在box4的末尾加入一个空值(该空值是一个行内元素) */
display: block; /* 将该空值转换为块元素 */
clear: both; /* 清楚该空值的侧片浮动 */
}
</style>
</head>
<body>
<p>方案一:</p>
<!--解决方案1-->
<div class="box1">
<div class="box2"></div>
<div class="box3"></div>
</div>
<br>
<br>
<p>方案二:</p>
<!--解决方案2-->
<div class="box4">
<div class="box5"></div>
</div>
</body>
</html>
运行结果: