清除浮动之父级div定义伪类:after和zoom

这是比较常用的一种清除浮动的方法,各大网站都在采用:

<!Doctype html>
<html>
<head></head>
<body>
<style type="text/css"> 
   .div1{background:#333;border:1px solid red;}
   .div2{background:#888;border:1px solid red;height:100px;margin-top:10px}
   
   .left{float:left;width:20%;height:200px;background:#fff}
   .right{float:right;width:30%;height:80px;background:#fff}
   
   /*清除浮动代码*/
   .clearfloat:after{display:block;clear:both;content:"";visibility:hidden;height:0}
   .clearfloat{zoom:1}
   </style> 
<div class="div1 clearfloat"> 
<div class="left">Left</div> 
<div class="right">Right</div> 
</div>
<div class="div2">
   div2
   </div>
</body>
</html>

 

posted @ 2019-10-14 13:33  xiaoYu&  阅读(882)  评论(0编辑  收藏  举报