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

CSS清除浮動(dòng) clearfix:after 使用技巧及兼容Firefox等符合W3C標(biāo)準(zhǔn)的瀏覽器

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

HTML4STRICT代碼:
代碼:

復(fù)制代碼
代碼如下:

<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>

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

復(fù)制代碼
代碼如下:

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

復(fù)制代碼
代碼如下:

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

然后,我們?cè)傩薷脑瓉淼腍TML代碼,讓外部的容器DIV來使用這個(gè)CSS:
HTML4STRICT代碼:

復(fù)制代碼
代碼如下:

<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 也不會(huì)有問題了。原來,這個(gè)clearfix的CSS使用了after這個(gè)偽對(duì)象,它將在應(yīng)用clearfix的元素的結(jié)尾添加content中的內(nèi)容。在這里添加了一個(gè)句號(hào)".",并且把它的display設(shè)置成block;高度設(shè)為0;clear設(shè)為both;visibility設(shè)為隱藏。這樣就達(dá)到了撐開容器的目的啦。
但是,在文章中說,Windows IE并不支持這樣做。所以要讓IE也完美顯示,則必須在clearfix這個(gè)CSS定義的后面加上一些專門為IE設(shè)定的HACK。CSS如下:
CSS代碼:
代碼:

復(fù)制代碼
代碼如下:

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

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

復(fù)制代碼
代碼如下:

/******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 */

相關(guān)文章

最新評(píng)論