之前清除浮動的時候老是在想要清除浮動的元素後面添加瀏覽器
1 <div style="clear:both;"></div>
或者寫在br標籤裏面來解決,但這樣會增長無語義的標籤,下面是用after僞類實現,兼容多種瀏覽器spa
1 .clearfix:after{ 2 content:""; 3 display:block; 4 height:0; 5 clear:both; 6 visibility:hidden; 7 }
爲兼容IE6,IE7,由於ie6,ie7不能用after僞類。加上下面代碼code
1 .clearfix{zoom:1}
通常狀況下,若是父層內部有浮動(float)可能致使父層的高度爲0,加上clearfix後就好了blog
HTML:it
1 <div class="parent clearfix"> 2 <div class="left">left</div> 3 <div class="right">right</div> 4 </div>
CSS:class
1 .clearfix{zoom:1} 2 .clearfix:after{ 3 content:""; 4 display:block; 5 height:0; 6 clear:both; 7 visibility:hidden; 8 } 9 .parent{ 10 background-color:red; 11 width:120px; 12 } 13 .left{ 14 float:left; 15 background-color:pink; 16 height:60px; 17 } 18 .right{ 19 float:right; 20 background-color:#abcdef; 21 }