清除浮动的四种常用方法及其优缺点

清除浮动的四种常用方法及其优缺点

浮动带来的问题

众所周知,清除浮动主要是为了解决如下两个问题

1,子元素浮动导致父元素高度为0

浮动前

<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .container{
        width:300px;
        background: #f3e5f5;
        border: 5px solid #6a1b9a;
      }

      .box1{
        width:100px;
        height: 100px;
        background: #ba68c8;
      }

      .box2{
        width:100px;
        height: 100px;
        background: #ab47bc;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="box1"></div>
      <div class="box2"></div>
    </div>
  </body>
</html>

image-20210912160808136

浮动后

.box1{
    width:100px;
    height: 100px;
    background: #ba68c8;
    float:left;
}

.box2{
    width:100px;
    height: 100px;
    background: #ab47bc;
    float:left;
}

image-20210912160906472

2,元素浮动后释放文档流导致布局混乱

浮动前

<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>

      .box0{
        width:100px;
        height: 100px;
        background: #ffcc80;
      }

      .container{
        width:300px;
        background: #f3e5f5;
        border: 5px solid #6a1b9a;
      }

      .box1{
        width:100px;
        height: 100px;
        background: #ba68c8;
      }

      .box2{
        width:100px;
        height: 100px;
        background: #ab47bc;
      }
    </style>
  </head>
  <body>
    <div class="box0"></div>
    <div class="container">
      <div class="box1"></div>
      <div class="box2"></div>
    </div>
  </body>
</html>

image-20210912161213044

浮动后

.box0{
    width:100px;
    height: 100px;
    background: #ffcc80;
    float:left;
}

image-20210912161250393

清除浮动的四种常用方法

1,额外标签法

在container div的最后位置添加一个新的元素,并为其设置clear:both;

image-20210912160906472

接着之前父元素因为子元素浮动高度变为0的例子,我们给container div添加一个p元素并为其设置clear:both;,如下

<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .container{
        width:300px;
        background: #f3e5f5;
        border: 5px solid #6a1b9a;
      }

      .box1{
        width:100px;
        height: 100px;
        background: #ba68c8;
        float:left;
      }

      .box2{
        width:100px;
        height: 100px;
        background: #ab47bc;
        float:left;
      }
      
      .p1{
        clear:both;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="box1"></div>
      <div class="box2"></div>
      <p class="p1">我是额外标签</p>
    </div>
  </body>
</html>

image-20210912162531927

可以看到浮动被清除了,父元素的高度也恢复了。

方法点评:

优点: 容易理解。

缺点: 语义化差,添加了无意义的元素

推荐指数: ☆☆☆☆☆

2,设置overflow触发BFC

通过设置overflow:hidden;或者overflow:auto;来触发BFC特性,使得其可以清除浮动。理论上讲,只要触发了BFC特性,该元素就可以清除浮动(即可包含浮动元素)。

<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .container{
        width:300px;
        background: #f3e5f5;
        border: 5px solid #6a1b9a;
        overflow:hidden;
      }

      .box1{
        width:100px;
        height: 100px;
        background: #ba68c8;
        float:left;
      }

      .box2{
        width:100px;
        height: 100px;
        background: #ab47bc;
        float:left;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="box1"></div>
      <div class="box2"></div>
    </div>
  </body>
</html>

image-20210912163353057

方法点评:

优点: 使用简单,一小段代码即可生效。

缺点: 无法显示溢出的内容或者产生不美观的滚动条。

推荐指数: ☆☆☆☆☆

3,设置after伪元素

通过给container div设置after伪元素来清除浮动,其原理和额外标签法类似,但是after伪元素不占用DOM。

<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .container{
        width:300px;
        background: #f3e5f5;
        border: 5px solid #6a1b9a;
      }

      .container::after{
        content: "";
        display: block;
        clear:both;
      }

      .clearfix{
        *zoom: 1; /*IE6清除浮动的方式 *号只有IE6-IE7执行,其他浏览器不执行*/
      }

      .box1{
        width:100px;
        height: 100px;
        background: #ba68c8;
        float:left;
      }

      .box2{
        width:100px;
        height: 100px;
        background: #ab47bc;
        float:left;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="box1"></div>
      <div class="box2"></div>
    </div>
  </body>
</html>

image-20210912164920416

方法点评:

优点: 不占用DOM,渲染效率提高,结构语义化更好。

缺点: IE6-7不支持伪元素::after,考虑兼容得添加zoom:1触发hasLayout来清除浮动。

推荐指数: ★★★☆☆

4,设置before和after双伪元素

与设置after伪元素同理,增加设置before伪元素是为了解决元素上方浮动元素所带来的影响

增设before伪元素前

<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>

      .box0{
        width:100px;
        height: 100px;
        background: #ffcc80;
        float:left;
      }

      .container{
        width:300px;
        /* background: #f3e5f5; */
        /* border: 5px solid #6a1b9a; */
      }

      .container::after{
        content: "";
        display: block;
        clear:both;
      }

      .clearfix{
        *zoom: 1; /*IE6清除浮动的方式 *号只有IE6-IE7执行,其他浏览器不执行*/
      }

      .box1{
        width:100px;
        height: 100px;
        background: #ba68c8;
        float:left;
      }

      .box2{
        width:100px;
        height: 100px;
        background: #ab47bc;
        float:left;
      }
    </style>
  </head>
  <body>
    <div class="box0"></div>
    <div class="container">
      <div class="box1"></div>
      <div class="box2"></div>
    </div>
  </body>
</html>

image-20210912171406197

此例中,我们希望box0和container div各在自己的一行,这就需要用到before伪元素

增设before伪元素后

<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>

      .box0{
        width:100px;
        height: 100px;
        background: #ffcc80;
        float:left;
      }

      .container{
        width:300px;
        /* background: #f3e5f5;
        border: 5px solid #6a1b9a; */
      }

      .container::before,.container::after{
        content: "";
        display: block;
        clear:both;
      }
    
      .clearfix{
        *zoom: 1; /*IE6清除浮动的方式 *号只有IE6-IE7执行,其他浏览器不执行*/
      }

      .box1{
        width:100px;
        height: 100px;
        background: #ba68c8;
        float:left;
      }

      .box2{
        width:100px;
        height: 100px;
        background: #ab47bc;
        float:left;
      }
    </style>
  </head>
  <body>
    <div class="box0"></div>
    <div class="container">
      <div class="box1"></div>
      <div class="box2"></div>
    </div>
  </body>
</html>

image-20210912171846679

方法点评:

优点: 在设置单个after伪元素的优点的基础上,还确保了被设置容器的所在行的独立性

缺点: IE6-7不支持伪元素::after,考虑兼容得添加zoom:1触发hasLayout来清除浮动。

推荐指数: ★★★★☆

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值