什么是clearfix?

什么是clearfix?

最近我查看了一些网站的代码,看到每个人<div> 都有一个班级clearfix

经过快速谷歌搜索,我了解到它有时适用于IE6,但实际上是什么是一个clearfix?

与没有clearfix的布局相比,您能提供一些带有clearfix的布局示例吗?


互换的青春
浏览 1894回答 5
5回答

达令说

其他答案都是正确的。但我想补充一点,它是人们第一次学习CSS并被滥用float来完成所有布局的时间遗留物。float意味着在长文本旁边做漂浮图像之类的东西,但很多人用它作为主要的布局机制。因为它不是真正的意思,你需要像“clearfix”这样的黑客才能使它工作。这些天display: inline-block是一个可靠的选择(IE6和IE7除外),虽然更现代的浏览器在flexbox,网格布局等名称下具有更有用的布局机制。

HUH函数

在clearfix允许的容器来包装其浮动孩子。没有clearfix或等效的样式,容器不会缠绕其漂浮的儿童并且坍塌,就像它漂浮的儿童被绝对定位一样。clearfix有几个版本,Nicolas Gallagher和Thierry Koblentz是主要作者。如果您想要支持旧浏览器,最好使用此clearfix:.clearfix:before,&nbsp;.clearfix:after&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;content:&nbsp;""; &nbsp;&nbsp;&nbsp;&nbsp;display:&nbsp;table;}.clearfix:after&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;clear:&nbsp;both;}.clearfix&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;*zoom:&nbsp;1;}在SCSS中,您可以使用以下技术:%clearfix&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&:before,&nbsp;&:after&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;content:"&nbsp;"; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;display:table; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&:after&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;clear:both; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*zoom:1; &nbsp;&nbsp;&nbsp;&nbsp;}}#clearfixedelement&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;@extend&nbsp;%clearfix;}如果您不关心支持旧浏览器,那么版本较短:.clearfix:after&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;content:""; &nbsp;&nbsp;&nbsp;&nbsp;display:table; &nbsp;&nbsp;&nbsp;&nbsp;clear:both;}

千巷猫影

简单地说,clearfix是一个黑客。这是我们都必须忍受的丑陋事物之一,因为它确实是确保浮动的子元素不会溢出父母的唯一合理方式。还有其他布局方案,但浮动在今天的网页设计/开发中太普通了,忽略了clearfix黑客的价值。我个人倾向于Micro Clearfix解决方案(Nicolas Gallagher).container:before,.container:after&nbsp;{ &nbsp;&nbsp;content:""; &nbsp;&nbsp;display:table;}.container:after&nbsp;{ &nbsp;&nbsp;clear:both;}.container&nbsp;{ &nbsp;&nbsp;zoom:1;&nbsp;/*&nbsp;For&nbsp;IE&nbsp;6/7&nbsp;(trigger&nbsp;hasLayout)&nbsp;*/}参考
打开App,查看更多内容
随时随地看视频慕课网APP