清除浮動之父級div定義僞類:after和zoom

這是比較經常使用的一種清除浮動的方法,各大網站都在採用:css

<!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>
相關文章
相關標籤/搜索