美文网首页
浮动元素导致的高度塌陷问题

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

作者: haoye999 | 来源:发表于2019-01-18 01:54 被阅读0次

相关定义与参考

高度塌陷 (例子)
: 父元素包含子元素,当子元素设置为浮动且父元素高度为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;
}

相关文章

  • 解决高度塌陷2

    为什么出现高度塌陷?当我们设置块级元素进行浮动的时候,会导致父元素塌陷,所以需要我们解决高度塌陷问题

  • 浮动导致的父容器高度塌陷

    浮动导致的父容器高度塌陷是指由于浮动元素脱离文档流,页面渲染时无法计算上浮动元素的高度,导致父元素的高度没有被撑开...

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

    相关定义与参考 几种比较好的解决方案- 1. 设置overflow- 2. 在浮动元素后添加兄弟元素- 3. 使用...

  • 高度塌陷 清除浮动 定位

    高度塌陷 父元素中的子元素浮动,导致父元素塌陷 开发中避免出现 写死父元素高度可以解决,但不能随子元素变化,不推...

  • 清除浮动的几种常用方法

    父元素自适应宽高度,子元素浮动导致高度塌陷 高度塌陷: 示例图: HTML: CSS: 第一种解决方案: 通过给父...

  • 清除浮动

    浮动元素脱离文档流,所以不占位置,当父元素没有设置高度时,不能撑开,导致父元素的高度塌陷,所以需要清除浮动。 方法...

  • 高度塌陷

    高度塌陷问题:子元素浮动父元素的高度没了,就会造成高度塌陷,可以开启bfc来解决这个问题,bfc有三种特性 1.父...

  • 前端零碎知识集锦

    float元素 浮动会让元素塌陷。即被浮动元素的父元素不具有高度。例如一个父元素包含了浮动元素,它将塌陷具有零高度...

  • 前端07day

    高度塌陷: 块元素默认父亲宽,高度由内容撑开父元素高度由子元素撑开浮动后塌陷 解决高度塌陷: BFC 1.父元素的...

  • 塌陷,导航,定位

    高度塌陷 块元素默认父亲宽,高度由内容撑开父元素高度由子元素撑开浮动后塌陷解决高度塌陷:BFC 1.父元素的垂直...

网友评论

      本文标题:浮动元素导致的高度塌陷问题

      本文链接:https://www.haomeiwen.com/subject/calprqtx.html