CSS高度塌陷的介绍以及解决办法

一、概念

高度塌陷:给子元素设置浮动,子元素脱离文档流,就不能再撑开父元素的高度,

导致父元素高度的丢失,从而影响了整个页面的布局。

演示:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title></title>
    <style type="text/css">
      .outer {
        border: 10px red solid;
.inner {
        width: 100px;
        height: 100px;
        background-color: blue;
        float: left;
      }
      .inner2{
        background-color: green;

      }

      .box3 {
        height: 100px;
        background-color: yellow;
      }
    </style>
  </head>
  <body>
    <div class="outer">
      <div class="inner"></div>
      <div class="inner2 inner"></div>

    </div>

    <div class="box3"></div>
  </body>
</html>

二、解决办法

1.方法一

固定父元素的高度(不推荐)

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title></title>
    <style type="text/css">
      .outer {
        border: 10px red solid;
        height: 200px;
        
      }

      .inner {
        width: 100px;
        height: 100px;
        background-color: blue;
        float: left;
      }
      .inner2{
        background-color: green;

      }

      .box3 {
        height: 100px;
        background-color: yellow;
      }
    </style>
  </head>
  <body>
    <div class="outer">
      <div class="inner"></div>
      <div class="inner2 inner"></div>

    </div>

    <div class="box3"></div>
  </body>
</html>

2.方法二

开启隐含属性BFC

页面元素中的隐含属性:Block Formatting Context 即块格式化上下文,简称BFC当开启元素的BFC以后,元素会变成一个独立的布局区域,不会在布局上影响到外面的元素BFC 理解为一个封闭的大箱子,箱子内部的元素不会影响到外部。

如何开启元素的BFC

      1.设置元素浮动(不推荐)

        - 使用这种方式开启,虽然可以撑开父元素,但是会导致父元素的宽度丢失

          而且使用这种方式也会导致下边的元素上移,不能解决问题

      2.设置元素为inline-block(不推荐)

        - 可以解决问题,但是会导致宽度丢失,而且会出现三像素的空白问题,不推荐使用这种方式

      3.将元素的overflow设置为一个非visible的值

            推荐方式:将overflow设置为hidden是副作用最小的开启BFC的方式。

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title></title>
    <style type="text/css">
      .outer {
        border: 10px red solid;
        position: absolute;
      }

      .inner {
        width: 100px;
        height: 100px;
        background-color: blue;
        float: left;
      }

      .box3 {
        width: 100px;
        height: 100px;
        background-color: yellow;
        float: left;
      }
      .box{
        width: 200px;
        height: 200px;
        background-color: green;
        /* position: absolute; */
        overflow: hidden;
      }
      .box1{
        width: 100px;
        height: 100px;
        background-color: royalblue;
      }
    </style>
  </head>
  <body>

3.方法三

可以直接在高度塌陷的父元素的最后,添加一个空白的di

     由于这个div并没有浮动,所以他是可以撑开父元素的高度的,然后在对其进行清除浮动,这样可以通过这个空白的div来撑开父元素的高度,基本没有副作用使用这种方式虽然可以解决问题,但是会在页面中添加多余的结构。

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title></title>
    <style type="text/css">
      .box1 {
        border: 10px solid red;
      }

      .box2 {
        width: 100px;
        height: 100px;
        background-color: blue;
        float: left;
      }
      .box3{
        clear: both;
      }
    </style>
  </head>
  <body>
    <div class="box1">
      <div class="box2">box2</div>
      <div class="box3"></div>
    </div>
  </body>
</html>

       也可以通过after伪类向元素的最后添加一个空白的块元素,然后对其清除浮动,这样做和添加一个div的原理一样,可以达到一个相同的效果,而且不会在页面中添加多余的div,这是我们最推荐使用的方式,几乎没有副作用。

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title></title>
    <style type="text/css">
      .box1 {
        border: 10px solid red;
      }
      .box2 {
        width: 100px;
        height: 100px;
        background-color: blue;
        float: left;
      }
     .clearfix::after{
        content: '';
        display: block;
        clear: both;
      }
    </style>
  </head>
  <body>
    <div class="box1 clearfix">
      <div class="box2"></div>
    </div>
  </body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值