场景
父子盒子嵌套,父盒子没有设置高度,子盒子浮动,子盒子无法撑起父盒子的高度
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.box1 {
width: 100%;
border: 5px solid red;
}
.box2 {
float: left;
width: 100px;
height: 100px;
background-color: green;
}
.box3 {
width: 400px;
height: 200px;
background-color: blue;
}
</style>
</head>
<body>
<div class="box1">
<div class="box2"></div>
</div>
</body>
</html>
解决问题的方法:
在需要清除浮动的元素上添加这个class
其实这种方法是优化了那种在标签后面添加一个标签来清除浮动的方法,避免了不必要的标签的添加!
.clearfix::after {
content: '';
display: block;
clear: both;
}