欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

CSS清除浮動 clearfix:after 使用技巧及兼容Firefox等符合W3C標準的瀏覽器

  發(fā)布時間:2011-10-10 14:35:38   作者:佚名   我要評論
在寫HTML代碼的時候,發(fā)現(xiàn)在Firefox等符合W3C標準的瀏覽器中,如果有一個DIV作為外部容器,內(nèi)部的DIV如果設置了float樣式,則外部的容器DIV因為內(nèi)部沒有clear,導致不能被撐開。
在寫HTML代碼的時候,發(fā)現(xiàn)在Firefox等符合W3C標準的瀏覽器中,如果有一個DIV作為外部容器,內(nèi)部的DIV如果設置了float樣式,則外部的容器DIV因為內(nèi)部沒有clear,導致不能被撐開。看下面的例子:

HTML4STRICT代碼:
代碼:

復制代碼
代碼如下:

<div style="border:2px solid red;">
<div style="float:left;width:80px;height:80px;border:1px solid blue;">TEST DIV</div>
<div style="float:left;width:80px;height:80px;border:1px solid blue;">CSSBBS</div>
<div style="float:left;width:80px;height:80px;border:1px solid blue;">TEST DIV</div>
<div style="float:left;width:80px;height:80px;border:1px solid blue;">TEST DIV</div>
<div style="float:left;width:80px;height:80px;border:1px solid blue;">TEST DIV</div>
</div>

大家可以看到,作為外部容器的邊框為紅色的DIV,沒有被撐開。這是因為內(nèi)部的DIV因為float:left之后,就丟失了clear:both和display:block的樣式,所以外部的DIV不會被撐開。
我們想讓外部容器的DIV隨著內(nèi)部DIV增多而增加高度,要怎么解決呢?
以前我都是用這樣的方法來解決:
HTML4STRICT代碼:
代碼:

復制代碼
代碼如下:

<div style="border:2px solid red;">
<div style="float:left;width:80px;height:80px;border:1px solid blue;">TEST DIV</div>
<div style="float:left;width:80px;height:80px;border:1px solid blue;">TEST DIV</div>
<div style="float:left;width:80px;height:80px;border:1px solid blue;">TEST DIV</div>
<div style="float:left;width:80px;height:80px;border:1px solid blue;">TEST DIV</div>
<div style="float:left;width:80px;height:80px;border:1px solid blue;">TEST DIV</div>
<div style="clear:both;"></div>
</div>

我們看到,在容器DIV內(nèi)要顯示出來的float:left的所有的DIV之后,我們添加了這樣的一個DIV:<div style="clear:both"></div> 。這樣,其實就在最后增加了clear的動作。
但是,我總覺得,這么多加一個DIV有點不妥。一是多了一個沒有意義的DIV,二是在用dojo做Drag & Drop的時候,由于這個DIV是容器DIV的一個字節(jié)點,如果這個節(jié)點被移動,則會造成排版上的Bug:如果要顯示的藍框的DIV被移到這個DIV之后,則因為clear:both,它會被強制換一行顯示。所以,我一直在尋找更好的解決辦法。
昨天在無數(shù)次的詢問了Google大仙后,我終于找到了How To Clear Floats Without Structural Markup 這篇文章,找到了解決的辦法。
首先設置這樣的CSS:
CSS代碼:
代碼:

復制代碼
代碼如下:

.clearfix:after {
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}

然后,我們再修改原來的HTML代碼,讓外部的容器DIV來使用這個CSS:
HTML4STRICT代碼:

復制代碼
代碼如下:

<style>
.clearfix:after {
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
/* Hides from IE-mac \*/
* html .clearfix {height: 1%;}
/* End hide from IE-mac */
</style>
<div style="border:2px solid red;" class="clearfix">
<div style="float:left;width:80px;height:80px;border:1px solid blue;">TEST DIV</div>
<div style="float:left;width:80px;height:80px;border:1px solid blue;">TEST DIV</div>
<div style="float:left;width:80px;height:80px;border:1px solid blue;">TEST DIV</div>
<div style="float:left;width:80px;height:80px;border:1px solid blue;">TEST DIV</div>
<div style="float:left;width:80px;height:80px;border:1px solid blue;">TEST DIV</div>
</div>

在Firefox里測試一下,哈哈,這樣做的確很有效,顯示正常,而且dojo的 Drag & Drop 也不會有問題了。原來,這個clearfix的CSS使用了after這個偽對象,它將在應用clearfix的元素的結尾添加content中的內(nèi)容。在這里添加了一個句號".",并且把它的display設置成block;高度設為0;clear設為both;visibility設為隱藏。這樣就達到了撐開容器的目的啦。
但是,在文章中說,Windows IE并不支持這樣做。所以要讓IE也完美顯示,則必須在clearfix這個CSS定義的后面加上一些專門為IE設定的HACK。CSS如下:
CSS代碼:
代碼:

復制代碼
代碼如下:

.clearfix:after {
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
/* Hides from IE-mac \*/
* html .clearfix {height: 1%;}
/* End hide from IE-mac */

因為轉義字符"\",Mac IE瀏覽器會忽略掉這段Hack,但Windows IE不會,它會應用 * html .clearfix {height: 1%;} 來達到撐開DIV容器的目的(貌似Mac IE沒有辦法解決這個問題,不過幸好用戶數(shù)量是在是太少了,Safari支持就可以了:p)。
測試一下,果然大功告成。
總結:
在css里面添加如下代碼:

復制代碼
代碼如下:

/******clear float*******/
.clearfix:after {
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
.clearfix {display: inline-table;}
/* Hides from IE-mac \*/
* html .clearfix {height: 1%;}
.clearfix {display: block;}
/* End hide from IE-mac */

相關文章

最新評論