浮动元素导致的高度塌陷问题

相关定义与参考

高度塌陷 (例子)
: 父元素包含子元素,当子元素设置为浮动且父元素高度为auto时,子元素会完全脱离文档流,父元素高度坍塌为0。

块格式上下文(Block Formatting Content)
: The html element defines the initial Block Formatting Context (BFC) for your page. This means all the elements inside <html></html> lay themselves out according to Normal Flow following the rules for block and inline layout. Elements participating in a BFC use the rules outlined by the CSS Box Model, which defines how an element's margins, borders and padding interact with other blocks in the same context.

开启BFC的方法(MDN指定)

  • floated elements
  • absolutely positioned elements (including position: fixed and position: sticky
    elements with display: inline-block
  • table cells or elements with display: table-cell, including anonymous table cells created when using the display: table-* properties
  • table captions or elements with display: table-caption
  • block elements where overflow has a value other than visible
  • display: flow-root
  • elements with contain: layout, content, or strict
  • flex items
  • grid items
  • multicol containers
  • elements with column-span: all

几种比较好的解决方案

1. 设置overflow

给父元素设置overflow: hidden,这样可以开启BFC属性。(例子)

开启BFC方法
block elements where overflow has a value other than visible

副作用
: 若父元素内有相对定位的元素,overflow: hidden会将其隐藏,不可视。

<!-- HTML -->
<div class="fatherbox">
    <div class="childbox1"></div>
</div>
/* CSS */
.fatherbox {
  border: 3px solid burlywood;
  overflow: hidden;
}

.childbox1 {
  width: 300px;
  height: 300px;
  background-color: #999;
  float: left;
}
2. 在浮动元素后添加兄弟元素

给浮动元素添加兄弟元素,并设置clear: both,去除浮动元素对其影响。(例子)

开启BFC条件
elements with contain: layout, content, or strict

副作用
增加了一个空元素来装饰页面,违背了HTML用来布局的原则。

/* HTML */
<div class="fatherbox">
  <div class="childbox1"></div>
  <div class="childbox2"></div>
</div>
/* CSS */
.fatherbox {
  border: 3px solid burlywood;
}

.childbox1 {
  width: 300px;
  height: 300px;
  background-color: #999;
  float: left;
}

.childbox2 {
  clear: both;
}
3. 使用after伪类(推荐

给父元素添加after伪类。(例子)
此方法与第一种方法的原理相同,都是使用兄弟元素的clear: both将上面兄弟元素的影响去除。

开启BFC条件

elements with contain: layout, content, or strict

<!-- HTML -->
<div class="fatherbox clearfix">
    <div class="childbox1"></div>
</div>
/* CSS */
.fatherbox {
  border: 3px  solid  burlywood;
}

.childbox1 {
  width: 300px;
  height: 300px;
  background-color: #999;
  float: left;
}

.clearfix::after {
  /* 内容设置为空 */
  content: "";
  /* 应用clear清除浮动元素对当前元素的影响 */
  clear: both;
  /* 设置成块元素 */
  display: block;
}
4. 使用较新的标准display: flow-root

直接对父元素设置display: flow-root(例子)

开启BFC条件
display: flow-root

副作用
只有最新版Chrome、Firefox、Opera浏览器实现了此标准。

<!-- HTML -->
<div class="fatherbox">
    <div class="childbox1"></div>
</div>
/* CSS */
.fatherbox {
  border: 3px solid burlywood;
  display: flow-root;
}

.childbox1 {
  width: 300px;
  height: 300px;
  background-color: #999;
  float: left;
}

兼容性问题

若要兼容IE6,需设置zoom: 1

总结

第一种方法兼容性最好,第三种方法对于IE8以下不支持,但其副作用最小,第四种方法浏览器支持性最差。
综合各个方案性能,得出终极方案是在第三种方法的基础上添加IE8以下支持,即(例子)

<!-- HTML -->
<div class="fatherbox clearfix">
    <div class="childbox1"></div>
</div>
/* CSS */
.fatherbox {
  border: 3px  solid  burlywood;
}

.childbox1 {
  width: 300px;
  height: 300px;
  background-color: #999;
  float: left;
}

.clearfix {
  zoom: 1;
}

.clearfix::after {
  /* 内容设置为空 */
  content: "";
  /* 应用clear清除浮动元素对当前元素的影响 */
  clear: both;
  /* 设置成块元素 */
  display: block;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • (注1:如果有问题欢迎留言探讨,一起学习!转载请注明出处,喜欢可以点个赞哦!)(注2:更多内容请查看我的目录。) ...
    love丁酥酥阅读 4,262评论 2 5
  • 无名小姐,你知道你的问题在哪吗?你怯懦,你没有勇气,你害怕挺起胸膛说:“是的,生活就是这样。人们相爱,互相属于对方...
    山间一颗竹阅读 310评论 0 0
  • 文︱崔崔陶子 听过一个说法,家是中国人的心。 这句话在我对家的改造过程中体会到了。 说是改造还真...
    崔崔陶子阅读 394评论 0 0
  • 有人说:夏天是个蓝色的季节。因为有许多人开始接近蓝色的海洋、蓝色的池水;有人说:夏天是个绿色的季节,因为树木、草叶...
    nia你阿阅读 402评论 0 1
  • 产品人:治愈猫(微信:zhiyumao) “小张,你这个原型功能太多,你看看别家产品,哪有这么多功能按钮,记住,l...
    治愈猫叔阅读 6,659评论 0 16