首先在不少不少年之前咱们经常使用的清除浮动是这样的。php
1
|
.clear{
clear
:
both
;
line-height
:
0
;}
|
如今可能还能够在不少老的站点上能够看到这样的代码,至关暴力有效的解决浮动的问题。可是这个用法有一个致命伤,就是每次清除浮动的时候都须要增长一个空标签来使用。css
这种作法若是在页面复杂的布局要常常清楚浮动的时候就会产生不少的空标签,增长了页面无用标签,不利于页面优化。可是我发现大型网站中 竟然还在使用这种清楚浮动的方法。有兴趣的同窗能够上他们首页搜索一下他们的.blank0这个样式名称。html
所以有不少大神就研究出了 clearfix 清除浮动的方法,直接解决了上面的缺陷,不须要增长空标签,直接在有浮动的外层加上这个样式就能够了,这也是咱们今天要讨论的clearfix进化史。浏览器
起源app
1
2
3
4
5
6
7
8
9
10
11
12
|
.clearfix:after {
visibility
:
hidden
;
display
:
block
;
font-size
:
0
;
content
:
" "
;
clear
:
both
;
height
:
0
;
}
.clearfix {
display
:
inline-table
; }
* html .clearfix {
height
:
1%
; }//Hides <span id=
"5_nwp"
style=
"width: auto; height: auto; float: none;"
><a id=
"5_nwl"
href=
"http://cpro.baidu.com/cpro/ui/uijs.php?app_id=0&c=news&cf=1001&ch=0&di=128&fv=17&is_app=0&jk=b88e21d880bd37e5&k=from&k0=from&kdi0=0&luki=1&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=e537bd80d8218eb8&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F6259%2Ehtml&urlid=0"
target=
"_blank"
mpid=
"5"
style=
"text-decoration: none;"
><span style=
"color:#0000ff;font-size:14px;width:auto;height:auto;float:none;"
>from</span></a></span> IE-mac
.clearfix {
display
:
block
; }//End
hide
from IE-mac
|
解释一下以上的代码:ide
从新对 IE/Mac 外的IE应用 block 显示属性。布局
最后一行用于结束针对 IE/Mac 的hack。(是否是以为很坑爹,Mac下还有IE)测试
起源代码可能也是很早期的时候了,再日后Mac下的IE5也发展到IE6了,各类浏览器开始向W3C这条标准慢慢靠齐了。因此就有了下面这个写法出现了。优化
1
2
3
4
5
6
7
8
9
10
|
.clearfix:after {
visibility
:
hidden
;
display
:
block
;
font-size
:
0
;
content
:
" "
;
clear
:
both
;
height
:
0
;
}
* <span id=
"4_nwp"
style=
"width: auto; height: auto; float: none;"
><a id=
"4_nwl"
href=
"http://cpro.baidu.com/cpro/ui/uijs.php?app_id=0&c=news&cf=1001&ch=0&di=128&fv=17&is_app=0&jk=b88e21d880bd37e5&k=html&k0=html&kdi0=0&luki=5&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=e537bd80d8218eb8&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F6259%2Ehtml&urlid=0"
target=
"_blank"
mpid=
"4"
style=
"text-decoration: none;"
><span style=
"color:#0000ff;font-size:14px;width:auto;height:auto;float:none;"
>html</span></a></span> .clearfix { zoom:
1
; }
/* IE6 */
*:first-child+html .clearfix { zoom:
1
; }
/* IE7 */
|
IE6 和 IE7 都不支持 :after 这个伪类,所以须要后面两条来触发IE6/7的haslayout,以清除浮动。幸运的是IE8支持 :after 伪类。所以只须要针对IE6/7的hack了。网站
在一个有float 属性元素的外层增长一个拥有clearfix属性的div包裹,能够保证外部div的height,即清除"浮动元素脱离了文档流,包围图片和文本的 div 不占据空间"的问题。
Jeff Starr 在这里针对IE6/7用了两条语句来触发haslayout。我在想做者为何不直接用 * 来直接对 IE6/7 同时应用 zoom:1 或者直接就写成:
1
2
3
4
5
6
7
8
9
|
.clearfix:after {
visibility
:
hidden
;
display
:
block
;
font-size
:
0
;
content
:
" "
;
clear
:
both
;
height
:
0
;
}
.clearfix{*zoom:
1
;}
|
可是对于不少同窗这种优化程度代码仍是不够给力,clearfix 发展到如今的两个终极版。
构成Block Formatting Context的方法有下面几种:
float的值不为none。
overflow的值不为visible。
display的值为table-cell, table-caption, inline-block中的任何一个。
position的值不为relative和static。
很明显,float和position不合适咱们的需求。那只能从overflow或者display中选取一个。
由于是应用了.clearfix和.menu的菜单极有多是多级的,因此overflow: hidden或overflow: auto也不知足需求
(会把下拉的菜单隐藏掉或者出滚动条),那么只能从display下手。
咱们能够将.clearfix的display值设为table-cell, table-caption, inline-block中的任何一个
可是display: inline-block会产生多余空白,因此也排除掉。
剩下的只有table-cell, table-caption,为了保证兼容能够用display: table来使.clearfix造成一个Block Formatting Context
由于display: table会产生一些匿名盒子,这些匿名盒子的其中一个(display值为table-cell)会造成Block Formatting Context。
这样咱们新的.clearfix就会闭合内部元素的浮动。
后面又有人对此进行了改良:
终极版一:
1
2
3
4
5
6
7
|
.clearfix:after {
content
:
"\200B"
;
display
:<span id=
"2_nwp"
style=
"width: auto; height: auto; float: none;"
><a id=
"2_nwl"
href=
"http://cpro.baidu.com/cpro/ui/uijs.php?app_id=0&c=news&cf=1001&ch=0&di=128&fv=17&is_app=0&jk=b88e21d880bd37e5&k=block&k0=block&kdi0=0&luki=6&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=e537bd80d8218eb8&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F6259%2Ehtml&urlid=0"
target=
"_blank"
mpid=
"2"
style=
"text-decoration: none;"
><span style=
"color:#0000ff;font-size:14px;width:auto;height:auto;float:none;"
>
block
</span></a></span>;
height
:
0
;
clear
:
both
;
}
.clearfix {*zoom:
1
;}
/*IE/7/6*/
|
解释下:content:"\200B";这个参数,Unicode字符里有一个“零宽度空格”,即 U+200B,代替原来的“.”,能够缩减代码量。并且再也不使用visibility:hidden。
终极版二:
1
2
3
4
5
6
7
8
|
.clearfix:before,.clearfix:after{
content
:
""
;
display
:table;
}
.clearfix:after{
clear
:
both
;}
.clearfix{
*zoom:
1
;
/*IE/7/6*/
}
|
这两个终极版代码都很简洁,终极版一和二均可以使用,以上代码都通过测试,你们赶忙用一下吧,若是有什么问题请及时跟我反馈,若是你还停留在clearfix的老代码的时候就赶忙更新一下代码吧。
转自:http://www.admin10000.com/document/6259.html