清除浮动为什么采用display:table

  .clearfix {
            *zoom: 1;
        }

        .clearfix:before,
        .clearfix:after {
            display: table;
            line-height: 0;
            content: "";
        }

        .clearfix:after {
          * clear: both;
        }

display:block和display:table完全看不出来区别啊?请问什么情况下有区别?
这里回答的感觉还是搞不懂

阅读 11.1k
4 个回答

display: table可以触发BFC块格式化上下文, 限制其内部元素float的不影响到BFC外面.
另见清除浮动 - MDN

然鹅这里display: table是用在伪元素上, 那他和display: block一样, 就只是将伪元素变成块级元素.

有一点小区别是:
如果是block, .clearfix内容为空的话, .clearfix上方和下方的元素可以发生margin合并,
如果是table, 相当于在上下方元素之间隔了一个BFC, 即使.clearfix内容为空, 上下方元素依然不margin合并.

zoom: 1不需要, line-height不需要, 这样就可以了:

    .clearfix::before,
    .clearfix::after {
      content: "";
      display: table;
    }

    .clearfix::after {
      clear: both;
    }

图片描述

先不说 tableblock 的区别,直接从样式上客观去看表现。

#test p {
  float:left;
  background: #f00;
}
#test:after,
#test:before {
  content:"before";
  display: table;
  background: #666;
}
#test:after {
  background: #0ff;
  content: "after";
}

然后把里面的 table 换成 block,效果就有了。

如果要深入了解,可以先了解一下 table 的一些特性和块级元素的特点。

不过个人感觉并不会有太大差异,清除浮动主要是靠相邻的下一级元素中的 clear 属性,而不是该元素的 display 属性。

如果说 block 会有高度啊什么的话,那是废话,只要设置高度为 0,哪里还有高度,再加 overflow,这个块元素的高度就跟不存在没区别。

最近看书正好遇到这个问题,我认为是没有影响的。用这种方法清除浮动,主要是在于两点:

  1. 使用clear属性:为了使::after的左边或者两边不允许出现浮动元素
  2. display为块级元素:内联元素会跟在浮动元素后面,clear属性也只有在块级元素才会起作用

那么只要display为块级元素就可以了,也就是block,list-item或者table中的任意一个

伪元素 display 属性的默认值是 inline ,清除浮动效果不完美(【经验证:只能清除该伪元素的位置以及之前空间的浮动效果】)。因为它只计算了伪元素当前位置距父元素内容区上边界的距离,即父元素的内容区高度(如果距离为零,就什么也没计算),仍然没有彻底解决布局流中因子元素浮动造成的父元素高度塌陷问题,后续元素可能仍处于浮动的影响范围。

display 属性设置为块级盒,父元素在布局流中占据的高度才能足够包容所有浮动的元素,此时才能彻底离开浮动的影响范围,清除浮动的影响。经测试,blocktable 在高度为零时没有区别。

::before 伪元素不适合清除浮动,因为它在源元素内部生成,是在原来的第一个子元素之前,而非本元素之前。在本元素生成盒,计算布局时,父元素在布局流中内容区高度仍然为零,如果父元素没有边框和内衬的话,仍将发生边距折叠。

同理,::after 也是在本元素的最后一个子元素之后,而非本元素之后,它们占用的空间仍然属于本元素。

测试代码如下:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
* {
  box-sizing: border-box;
}
.row {
background-color: gray;
}
.row::after {
  content: "after-pseudo-elements";
  clear: both;
  display:block;
}

.footer::before {
    content:"before-pseudo-elements";
    display:block;
}

[class*="col-"] {
  float: left;
  padding: 15px;
}

html {
  font-family: "Lucida Sans", sans-serif;
}

.header {
  background-color: #9933cc;
  color: #ffffff;
  padding: 15px;
}

.menu ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
}

.menu li {
  padding: 8px;
  margin-bottom: 7px;
  background-color: #33b5e5;
  color: #ffffff;
  box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
}

.menu li:hover {
  background-color: #0099cc;
}

.aside {
  background-color: #33b5e5;
  padding: 15px;
  color: #ffffff;
  text-align: center;
  font-size: 14px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
}

.footer {
  background-color: #0099cc;
  color: #ffffff;
  text-align: center;
  font-size: 12px;
  padding: 15px;
}

/* For mobile phones: */
[class*="col-"] {
  width: 100%;
}

@media only screen and (min-width: 600px) {
  /* For tablets: */
  .col-s-1 {width: 8.33%;}
  .col-s-2 {width: 16.66%;}
  .col-s-3 {width: 25%;}
  .col-s-4 {width: 33.33%;}
  .col-s-5 {width: 41.66%;}
  .col-s-6 {width: 50%;}
  .col-s-7 {width: 58.33%;}
  .col-s-8 {width: 66.66%;}
  .col-s-9 {width: 75%;}
  .col-s-10 {width: 83.33%;}
  .col-s-11 {width: 91.66%;}
  .col-s-12 {width: 100%;}
}
@media only screen and (min-width: 768px) {
  /* For desktop: */
  .col-1 {width: 8.33%;}
  .col-2 {width: 16.66%;}
  .col-3 {width: 25%;}
  .col-4 {width: 33.33%;}
  .col-5 {width: 41.66%;}
  .col-6 {width: 50%;}
  .col-7 {width: 58.33%;}
  .col-8 {width: 66.66%;}
  .col-9 {width: 75%;}
  .col-10 {width: 83.33%;}
  .col-11 {width: 91.66%;}
  .col-12 {width: 100%;}
}
</style>
</head>
<body>

<div class="header">
  <h1>Chania</h1>
</div>

<div class="row">
  <div class="col-3 col-s-3 menu">
    <ul>
      <li>The Flight</li>
      <li>The City</li>
      <li>The Island</li>
      <li>The Food</li>
    </ul>
  </div>

  <div class="col-6 col-s-9">
    <h1>The City</h1>
    <p>Chania is the capital of the Chania region on the island of Crete. The city can be divided in two parts, the old town and the modern city.</p>
  </div>

  <div class="col-3 col-s-12">
    <div class="aside">
      <h2>What?</h2>
      <p>Chania is a city on the island of Crete.</p>
      <h2>Where?</h2>
      <p>Crete is a Greek island in the Mediterranean Sea.</p>
      <h2>How?</h2>
      <p>You can reach Chania airport from all over Europe.</p>
    </div>
  </div>
</div>


  <p class="footer">Resize the browser window to see how the content respond to the resizing.</p>
</div>

</body>
</html>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题