翻译:micro-clearfix-hack

http://nicolasgallagher.com/micro-clearfix-hack/

clearfix hack是一种无需使用标记语言就能清楚浮动的很流行的方法。本文提供了一种新的clearfix的方法来减少对css的依赖。
例子:Micro clearfix hack
支持: Firefox 3.5+, Safari 4+, Chrome, Opera 9+, IE 6+
“micro clearfix”方法适用于现代浏览器并且在Thierry Koblentz的“clearfix reloaded”基础上同时包括了:before 和 :after两个为元素。
Here is the updated code (I’ve used a shorter class name too):
以下是代码(我使用了缩写的类名)

/**
 * For modern browsers
 * 1. The space content is one way to avoid an Opera bug when the
 *    contenteditable attribute is included anywhere else in the document.
 *    Otherwise it causes space to appear at the top and bottom of elements
 *    that are clearfixed.
 * 2. The use of `table` rather than `block` is only necessary if using
 *    `:before` to contain the top-margins of child elements.
 */
.cf:before,
.cf:after {
    content: " "; /* 1 */
    display: table; /* 2 */
}

.cf:after {
    clear: both;
}

/**
 * For IE 6/7 only
 * Include this rule to trigger hasLayout and contain floats.
 */
.cf {
    *zoom: 1;
}

“micro clearfix”生成伪类元素并把他们的“display”属性为“table”,这将创建一个匿名表格单元和一个新的块格式化上下文这意味着伪元素”:before“之前防止”top-margin“的崩溃。”伪元素”:after“用来清楚浮动。结果,就没必要隐藏任何的生成内容并且所需的代码总量就少了很多。
包括了“:before”选择器对于清除浮动来说是不必要的,但是防止现代浏览器顶部空白崩溃,这样做有两个好处:
它确保其他通过创建一个新的块格式化上下文的浮动密封方法的视觉一致性,例如overflow:hidden
使用zoom:1 来确保和IE 6/7的兼容性。
对于IE 6/7有很多情形不包括包含一个新的块状上下文的下边距的浮动。更多的细节可以参考Better float containment in IE using CSS expressions
content:" " 的使用避免了Opera浏览器在clearfixed元素周围创建新的空格元素的报错,如果在HTML中也需要可编辑属性。由于Sergio Cerrutti发现并给予修复。一个替换方法是使用font:0/0 a

Firefox浏览器遗留问题

3.5版本以下的火狐得益于Thierry的使用visibility:hidden 来隐藏插入元素的方法。这是因为在某些情况下,老版本的火狐需要用content:"." 来避免出现在body和第一个字元素之间多余的空格。(例子:jsfiddle.net/necolas/K538S/)
还有一种float-containment方法创建了一个新的块级上下文格式,例如给容器元素添加overflow:hidden 或者display:inline-block 将同样避免了老版本火狐浏览器的这种行为。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值