.clearfix:after中为什么设置display: table

相较于display: block;有什么优点吗?

图片描述

阅读 7.3k
6 个回答

实际上设置display:table实际是利用了使父容器形成BFC
而display:block是不会清除浮动的,它是利用了clear:both来清除浮动的
建议看看这篇文章,如果有兴趣还可以专门研究一下BFC
CSS清除浮动(Clear与BFC)

这里的display: table是用在伪元素上, "使父容器形成BFC"的说法不对.
触发了BFC的只是伪元素, 与display: block的区别是, 有这个伪元素的BFC隔在中间, 能防止.clearfix上下方的元素margin合并

防止margin合并

display:block会导致换行呀

display:table 会清楚前后的浮动元素 block则不会

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