標籤:and rds absolute height 原理 bsp pen 可見度 style
什麼是.clearfix
.clearfix:after {
content: "."; /*內容為“.”就是一個英文的句號而已。也可以不寫。*/
display: block; /*加入的這個元素轉換為區塊層級元素。*/ clear: both; /*清除左右兩邊浮動。*/ visibility: hidden; /*可見度設為隱藏。注意它和display:none;是有區別的。visibility:hidden;仍然佔據空間,只是看不到而已;*/ line-height: 0; /*行高為0;*/ height: 0; /*高度為0;*/ font-size:0; /*字型大小為0;*/
}
.clearfix {
zoom: 1;
/*這是針對於IE6的,因為IE6不支援:after偽類,這個神奇的zoom:1讓IE6的元素可以清除浮動來包裹內部元素。*/
}
<div class="clearfix"> <div class="floated"></div> </div>
上面的代碼就是.clearfix的定義和應用,簡單的說下.clearfix的原理:
1、在IE6, 7下zoom: 1會觸發hasLayout,從而使元素閉合內部的浮動。
2、在標準瀏覽器下,.clearfix:after這個偽類會在應用到.clearfix的元素後面插入一個clear: both的區塊層級元素,從而達到清除浮動的作用。
3、在需要清除浮動的時候,只要寫一個.clearfix就行了,然後在需要清浮動的元素中 添加clearfix類名就好了。
<html > <head> <title> css用clearfix清除浮動執行個體</title> <meta name="generator" content="editplus" /> <meta name="author" content="" /> <meta name="keywords" content="" /> <meta name="description" content="" /> </head> <body> <style type="text/css"> /*所有主流瀏覽器都支援 :after 虛擬元素。*/ .clearfix:after{content:".";display:block;height:0;clear:both;visibility:hidden} .clearfix{*+height:1%;}/*不知道有什麼用處,不加ie7也沒有問題*/ .box{ background:#111;width:500px; position:relative;} .l{float:left; background:#333;width:200px; height:100px;} .r{float:right;background:#666;width:200px; height:200px;} .s{width:100px; height:100px;background:#999;position:absolute;right:-50px;;} </style> <div class="box clearfix"> <div class="l">left</div> <div class="r">right</div> <div class="s">absolute</div> </div> </body> </html>
css用clearfix清除浮動