/*解决高度塌陷,又可以确保父元素和子元素的垂直外边距不会重叠*/
.clearfix:before,.clearfix:after{
content: "";
display: table;
clear: both;
}
.clearfix{
zoom: 1;
}
在父元素上添加这个样式即可
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
.box1{
border: 1px solid red;
}
.box2{
width: 100px;
height: 100px;
background-color: blue;
margin-top: 20px;
float: left;
}
/*解决高度塌陷,又可以确保父元素和子元素的垂直外边距不会重叠*/
.clearfix:before,.clearfix:after{
content: "";
display: table;
clear: both;
}
.clearfix{
zoom: 1;
}
</style>
</head>
<body>
<div class="box1 clearfix">
<div class="box2"></div>
</div>
</body>
</html>
预览:
![](https://img.haomeiwen.com/i3990671/c2a83bc427507780.png)
网友评论