微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

什么是清除CSS样式“float”的最好方法?

我很习惯使用< br style =“clear:both”/>来清除我的浮动广告。但东西不断变化,我不知道这是否是最好的做法。

一个CSS hack(从positioneverything)可用,让你实现相同的结果,没有清除div。但是…他们声称黑客是一个过时的,而你或许应该看看this hack.但阅读通过700页的评论:)似乎可能有些地方后面的黑客不工作。

我想避免任何javascript黑客因为我希望我的清算工作,无论javascript是否启用。

什么是当前最佳实践,以浏览器独立方式清除div?

解决方法

更新:在2014年,您应该使用一个使用伪元素的修复技术,像@RodrigoManguinho提到的。这是清除浮动的现代方式。对于更新的方法,参见Nicholas gallagher的微清晰度:
/**
 * 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;
}

原始答案:

我真的不喜欢使用额外的非语义标记,所以我不使用清除元素。而不是只应用overflow:hidden;到浮点的父级以清除它们。工程交叉浏览器,没有问题。我相信overflow:auto;也工作。

显然,如果你想使用一个不同的overflow属性,但是由于IE6扩展盒的bug,我很少有理由溢出我的容器。

See more info on using overflow instead of clear to avoid adding extra markup

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。