父元素的高度为自适应,子元素进行浮动时就会发生高度塌陷,下面介绍高度塌陷的3种方法:

1.overflow:hidden

给父元素添加overflow:hidden属性(overflow:hidden;会隐藏定位到BFC区域外的内容)

2.在浮动元素下添加一个空的div盒子,并添加声明

clear:both;height:0;overflow:hidden

<div>浮动元素</div>
<div class="box"></div>

.box{clear:both;height:0;overflow:hidden;}
缺点:造成大量的代码冗余

3.万能清除浮动法

选择符:after{ content:".";clear:both; height:0;overflow:hidden;visibility:hidden;}

相关文章:

  • 2021-12-01
  • 2021-08-08
  • 2021-12-22
  • 2022-12-23
  • 2022-12-23
  • 2022-02-24
猜你喜欢
  • 2022-12-23
  • 2021-06-11
  • 2021-11-21
  • 2021-10-29
  • 2022-12-23
  • 2021-08-24
相关资源
相似解决方案