清除浮动早已是一个前端开发人员必学的一课。毫无疑问,多年来,我们已经接触过多种清除浮动的方法,现在“clearfix methods”越来越被大家熟知。在深入剖析“clearfix”的多种用法之前,我们来先看看clearfix方法试图解决哪些问题。

  场景:  .el-1.el-2是并排浮动在.container元素里,同时.main元素是紧挨着.container

  预期效果: 我们想要.container的高度扩展到它的子元素的高度(例如:.el-1或者.el-2的高度),同时我们也希望.main是在.container的下面的。

  实际效果: .container折叠了,并且没有高度。就像它没有包含内容,.main没有在预期的位置,同时在.container的背景和边框也没有了。

  基于上述的场景,页面代码可能如下所示:

<div class="container">
<div class="el-1">A long string of stuff here...</div>
<div class="el-2">A short string of stuff here...</div>
</div>
<div class="main">
Some stuff here...
</div>

  CSS代码可能如下所示:

 

.el-1, .el-2 {
float: left;
width: 50%;
} .el-1 {
/* styles for .el-1 here */
} .el-2 {
/* styles for .el-2 here */
} .container{
background:#ccc;
border:1px solid #000;
}
.main {
/* styles for .main here */
}

最终,演示效果如下:

Result

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.

This is the .main element.

 

通过观看demo,检查.container的CSS样式,可以看到它的确是折叠了。你可以在container的最上面看到一条黑色边框,并且看不到背景颜色。所以container的容器没有包含.el-1.el-2,所以.main移到一个尴尬的地方。

可能跟你想的相反,这不是浏览器的一个bug,也不是floats的错误的使用。floats就是这么工作的很多时候结果不像我们工程师预期想的那样,这时候我们就简单地需要“清除浮动”。

Clearing floats(clearfixing) 主要是指强制使容器元素去包含它的子元素。因此,它强制使随后的元素显示在它下面。经过多年,现在已经有很多方法用来清除浮动。在我们学习这些方法之前,让我们先来看看CSS的clear属性。clear是CSS重要属性之一,我们可以使用它来帮组我们解决这个问题。

The "clear" Property

MDN 是这样定义clear

The clear CSS property specifies whether an element can be next to floating elements that precede it or must be moved down(cleared) below them

从clear的定义上,我们可以明白为什么clear属性能够清楚的清除浮动。现在让我们深入挖掘一些方法吧。

方法1:课堂上的方法

这种方法是一种老的方法。老方法是相对的,当然真正的老方法是使用表格布局(在这种情况下清除浮动是毫无意义的)。考虑到这点,老方法是需要使用到floats的。

思路很简单:在包含浮动的容器的底部插入一个带有clear属性的空元素。使用一个指定的类来实现这样就可以在HTML中重用它了。这是长期使用的一种经典方法。下面是它的CSS样式:

.clear {
clear: both;
}

HTML结构可能长这样:

<div class="container">
<div class="el-1">I'm floated...</div>
<div class="el-2">I'm also floated...</div>
<br class="clear">
</div> <div class="main">
Bravo, sirs and madams. I'm in the clear.
</div>

下面是使用这个方法实现的demo

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.

This is the .main element.

注释:如果你不关心折叠的容器,而关心失去定位的.main元素,那么你可以选择把"cleared"元素放在container的后面。但是如果这样做了,那么你可能同时也对.main进行clear:both的声明。

这种方法是很久之前经常使用的方法,他很管用也很简单。然而,现在希望把内容从样式风格中分离出来,并且使用保持语义。这种方法现在已经不被推荐了。

方法2:溢出(overflow)的方法

.container定义overflow属性,我们将会使container扩展到包含整个浮动元素的高度。CSS如下:

.container {
overflow: hidden; /* can also be "auto" */
}

HTML保持不变,不需要添加额外的元素。

demo如下:

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.

This is the .main element.

如你所看到的一样,我们使container扩展到包含整个浮动元素的高度,background colours, borders 都能够成功运用。一切都很好。

然而这个方法的一个主要缺点是子元素如果超过容器将会被隐藏(overflow为hidden)或者产生一个滚动条(overflow为auto)。比之前的方法要好,但是仍不理想。让我们继续研究吧。

方法3:“clearfix”类

你可能经常听到它,但是它是什么?所有追求酷的人都在使用它,同时你也想使用它。“clearfix”(意思是修复浮动的清除)在样式表中定义了一个.clearfix类,我们可以把它应用到任何包含浮动的元素上。它会迫使容器元素扩大,同时使后续元素在容器元素下面。那么它是怎么工作的?它使用了CSS的伪元素:::before::after.Nicolas Gallagher非常完美的描述它:

The ... generates pseudo-elements and sets their display to table. This creates an anonymous table-cell ... The :after pseudo-element is used to clear the floats. As a result ... the total amount of code needed is reduced.

CSS如下:

.clearfix:before,
.clearfix:after {
content: "";
display: table;
} .clearfix:after {
clear: both;
} .clearfix {
zoom:; /* ie 6/7 */
}

HTML需要稍微修改下,修改如下:

<div class="container clearfix">
<div class="el-1">I'm floated...</div>
<div class="el-2">I'm also floated...</div>
</div> <div class="main">
Bravo, sirs and madams. I'm in the clear.
</div>

下面是添加clearfix的demo:

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.

This is the .main element.

Chris Coyier曾建议过如果你不需要执行IE8一下的版本,你只需要这样修改:

.clearfix:after{
content:"";
display:table;
clear:both;
}

简单,高效、语义性、易于重用。

注释:上面的代码称为“micro clearfix”,Nicolas Gallagher极力推广它。不同点在于Nicolas用的是不同的类。和之前的方法相比较,Peter-Paul KochThierry Koblentz他们都具有类似的技术.基本上,clearfix拥有相当长的历史,我们用的以上方法都是不同的迭代。

方法4:未来之星--contain-floats

有趣的是,W3C规范已经为min-height属性(和min/max属性)添加了一个新的值,为了帮助解决这个问题。如下所示:

.container {
min-height: contain-floats;
}

它将会实现像clearfix或overflow方法同样的效果,但是只需要简单的一行代码,并且没有我们之前提到的任何缺点。当然,你可以在CSS中创建一个单独的可重用的clearfix类。

现在看起来还没有任何浏览器支持这个值,但是还是很值得留意的。

总结

对,就是你,伙计,一个快速完成"clearfix"的方法。.clearfix已经成为标准,我强烈推荐这种方法而不要用前两种方法。它会使生活变得easier.当然,最适合你的就是最奏效的,但是,正如前面提到的,我建议现在就不要使用方法1,坚持使用标准的clearfix

翻译出自Clearing Floats: An Overview of Different clearfix Methods

Clearing Floats清除浮动--clearfix的不同方法的使用概述的更多相关文章

  1. 清除浮动clearfix

    css用clearfix清除浮动 更多2013/11/4 来源:css学习浏览量:11901 学习标签: css clearfix 本文导读:写css 时总为浮动而烦恼,如果用了浮动,浮动的父层不会跟 ...

  2. css清除浮动clearfix:after的用法详解

    如果外部有一个div容器,其内部div容器设置了float样式,则外部的容器div因为内部没有clear,导致不能撑开.解决方法:  CSS代码: 复制代码 代码如下: .clearfix:after ...

  3. 清除浮动 .clearfix

    子元素的浮动 清除子元素的浮动 .clearfix{zoom:1}/*IE 6-8*/.clearfix:after{content:'\20';display:block;height:0;clea ...

  4. clear-fix清除浮动的两种写法

    1. [代码]clearfix 清除浮动 .clearfix:after { content: "."; display: block; height: 0; font-size: ...

  5. 详解 清除浮动 的多种方式(clearfix)

    说明 本文适合知道HTML 与 CSS基础知识的读者,或者想要了解清除浮动背后原理的读者! 1.什么是浮动 首先我们需要知道定位 元素在页面中的位置就是定位,解决问题之前我们先来了解下几种定位方式 : ...

  6. .clearfix:after(清除浮动)中各个属性及值详细解说

    清除浮动.clearfix:after一词,从事web前端的朋友们对此不会陌生吧,下面为大家介绍的是.clearfix:after中用到的所有属性及值的含义,对此感兴趣的朋友可以参考下哈想,希望对大家 ...

  7. css清除浮动的集中方法

    一:浮动产生的副作用 1.父元素的背景不能显示 2.父元素的边框不能撑开 3.padding和margin失效 二:清除浮动的方法 1.给父元素设置高度:这样可以清除浮动,但是子元素内容高度不固定,这 ...

  8. 浮动、清除浮动、BFC

    一. 浮动 1. 浮动的定义 使元素脱离文档流,按照向左或向右的方向移动,直到它的外边缘碰到包含它的框或另一个浮动框为止. 脱离文档流就是在页面中不占位置了. 左浮动右浮动此处就不再赘述了. 2. 看 ...

  9. 【Web】网页清除浮动的方法

    网页中,经常用浮动的div来布局,但是会出现父元素因为子元素浮动引起内部高度为0的问题,为了解决这个问题,我们需要清除浮动,下面介绍4中清除浮动的方法. 在CSS中,clear属性用户清除浮动,语法: ...

随机推荐

  1. OO之工厂模式

    以下为工厂模式的详解,包括简单工厂,普通工厂模式,抽象工厂. 引子: 假设有一个交通工具公司,生产自行车,汽车,飞机等,现要销售该公司的产品,要怎么设计呢? 在交通工具商店中加一个if else判断如 ...

  2. c 计算 语句 执行 时间

    当然,你也可以用clock函数来计算你的机器运行一个循环或者处理其它事件到底花了多少时间:   #include “stdio.h” #include “stdlib.h” #include “tim ...

  3. c语言编程之二叉排序树

    二叉排序树,又称为二叉查找树.它是一颗空树,或者是具有下面的性质的二叉树: 1.若它的左子树不空,则左子树上所有节点的值均小于它的根结构的值: 2.若它的右子树不空,则右子树上所有节点的值均大于它的根 ...

  4. 二分---LIGHTOJ 1062

    1062 - Crossed Ladders PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB A ...

  5. shell编程之echo命令

    Linux echo命令不能显示文件中的内容.显示字符串或者变量功能说明:显示文字.语 法:echo [-ne][字符串]或 echo [--help][--version]补充说明:echo会将输入 ...

  6. poj 2449 Remmarguts' Date K短路+A*

    题目链接:http://poj.org/problem?id=2449 "Good man never makes girls wait or breaks an appointment!& ...

  7. 【BZOJ】【1492】【NOI207】货币兑换Cash

    DP/CDQ分治 orz Hzwer copy了下他的代码……结果在while(j<top......)这一句中把一个括号的位置打错了……找了我一个多小时才找到TAT 很神奇……顺便贴下CDQ的 ...

  8. json_encode charset

    json_encode  utf-8   mysql   charset  utf8

  9. 在centos 6.5 在virtual box 上 安装增强版工具

    centos 6.5 在virtual box 上 安装增强版工具: 出现:centos unable to find the source of your current linux kernel ...

  10. Sqli-labs less 28a

    Less-28a 本关与28基本一致,只是过滤条件少了几个. http://127.0.0.1/sqllib/Less-28a/?id=100%27)unIon%0bsElect%0b1,@@base ...