使用伪元素来清除浮动
css样式:
.clearfloat::after{
content: "";
display: block;
clear: both;
height: 0;
overflow: hidden;
}
.clearfloat{
zoom: 1;
}
首先创建三个盒子用来展现效果
<style>
.box{
width: 500px;
height: 500px;
background: coral;
}
.box1{
width: 200px;
height: 200px;
background: fuchsia;
float: left; //浮动盒子
}
.box2{ //正常盒子
width: 200px;
height: 200px;
background: lightgreen;
}
</style>
<body>
<div class="box">
<div class="box1">浮动盒子</div>
<div class="box2">正常盒子</div>
</div>
</body>
然后添加伪元素样式进行清除浮动
<style>
.box{
width: 500px;
height: 500px;
background: coral;
}
.box1{
width: 200px;
height: 200px;
background: fuchsia;
float: left;
}
.box2{
width: 200px;
height: 200px;
background: lightgreen;
}
//伪元素
.clearfloat::after{
content: "";
display: block;
clear: both;
height: 0;
overflow: hidden;
}
.clearfloat{
zoom: 1;
}
</style>
<body>
<div class="box">
<div class="box1">浮动盒子</div>
<div class="clearfloat"></div> //添加一个空div
<div class="box2">正常盒子</div>
</div>
</body>
效果