之前老是加一個<div sytle="clear:both"></div>來解決,但這樣會增長無語義的標籤,下面是用after僞類實現,兼容多種瀏覽器
.clearfix:after{
content:".";
display:block;
height:0;
clear:both;
visibility:hidden; css
}瀏覽器
爲兼容IE6,IE7,由於ie6,ie7不能用after僞類。加上下面代碼it
.clearfix{zoom:1}class
通常狀況下,若是父層內部有浮動(float)可能致使父層的高度爲0,加上clearfix後就好了:
<style type="text/css">
.clearfix:after{content:".";display:block;height:0;clear:both;visibility:hidden;}float
.clearfix{zoom:1}
.parent{width:120px;}
.left{float:left;display:inline;}
.right{float:right;display:inline;}
</style>兼容
<div class="clearfix">
<div class="left">
left
</div>
<div class="right">
right
</div>
</div>di