CSS清除浮动的三种方法

清除浮动本质

清除浮动主要为了解决父级元素因为子级浮动引起内部高度为0 的问题。

<style>
        .box1 {
            /* height: 200px; 很多情况下,我们父级盒子,不方便给高度 考虑孩子高度会变  */
            width: 600px;
            /*height: 200px;*/
            background-color: pink;
        }
        .box2 {
            width: 600px;
            height: 240px;
            background-color: purple;
        }
        .son1 { /*  son1 和 son2 是标准流,会撑开盒子 */
            width: 150px;
            height: 100px;
            background-color: skyblue;
            float: left;
        }
        .son2 {
            width: 300px;
            height: 100px;
            background-color: hotpink;
            float: left;
        }
        /* 如果son1 和son2 都浮动了, 浮动元素不占有位置, 父亲没有高度 此时  底下盒子就会跑上来 */
   </style>
    </head>
    <body>
    <div class="box1">
        <div class="son1"></div>
        <div class="son2"></div>
        <!-- 在浮动盒子的后面添加一个空盒子 -->
    </div>

     <div class="box2"></div>

    </body>

如图所示,浮动元素不占有位置,底下盒子跑上来:


image.png

清除浮动的方法

其实本质叫做闭合浮动更好一些, 记住,清除浮动就是把浮动的盒子圈到里面,让父盒子闭合出口和入口不让他们出来影响其他元素。

在CSS中,clear属性用于清除浮动,其基本语法格式如下:

选择器{clear:属性值;}
属性值 描述
left 不允许左侧有浮动元素(清除左侧浮动的影响)
right 不允许右侧有浮动元素(清除右侧浮动的影响)
both 同时清除左右两侧浮动的影响

一、额外标签法

是W3C推荐的做法是通过在浮动元素末尾添加一个空的标签例如 <div style=”clear:both”></div>,或则其他标签br等亦可。
<html lang="en">
    <head>
        <meta charset="utf-8">
        <style>
        .box1 {
            /* height: 200px; 很多情况下,我们父级盒子,不方便给高度 考虑孩子高度会变  */
            width: 600px;
            /*height: 200px;*/
            background-color: pink;
        }
        .box2 {
            width: 600px;
            height: 240px;
            background-color: purple;
        }
        .son1 { /*  son1 和 son2 是标准流,会撑开盒子 */
            width: 150px;
            height: 100px;
            background-color: skyblue;
            float: left;
        }
        .son2 {
            width: 300px;
            height: 100px;
            background-color: hotpink;
            float: left;
        }
        /* 如果son1 和son2 都浮动了, 浮动元素不占有位置, 父亲没有高度 此时  底下盒子就会跑上来 */
        .clear {
            clear: both;  /* 清除浮动的影响 */
        }
        </style>
    </head>
    <body>
    <div class="box1">
        <div class="son1"></div>
        <div class="son2"></div>
        <!-- 在浮动盒子的后面添加一个空盒子 -->
            <!-- <div class="clear"></div> -->
    </div>

     <div class="box2"></div>

    </body>
</html>

优点: 通俗易懂,书写方便

缺点: 添加许多无意义的标签,结构化较差。 我只能说,w3c你推荐的方法我不接受,你不值得拥有。。。

父级添加overflow属性方法

可以通过触发BFC的方式,可以实现清除浮动效果。

可以给父级添加: overflow为 hidden|auto|scroll  都可以实现。
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <style>
        .box1 {
            /* height: 200px; 很多情况下,我们父级盒子,不方便给高度 考虑孩子高度会变  */
            width: 600px;
            background-color: pink;
            overflow: hidden; /*  触发BFC BFC可以清除浮动 这是解决方案 BFC我们后面讲解 */
        }
        .box2 {
            width: 600px;
            height: 240px;
            background-color: purple;
        }
        .son1 { /*  son1 和 son2 是标准流,会撑开盒子 */
            width: 150px;
            height: 100px;
            background-color: skyblue;
            float: left;
        }
        .son2 {
            width: 300px;
            height: 100px;
            background-color: hotpink;
            float: left;
        }
        </style>
    </head>
    <body>
    <div class="box1">
        <div class="son1"></div>
        <div class="son2"></div>
    </div>
    <div class="box2"></div>

    </body>
</html>  

优点: 代码简洁

缺点: 内容增多时候容易造成不会自动换行导致内容被隐藏掉,无法显示需要溢出的元素。

使用after伪元素清除浮动

使用方法:

 .clearfix:after {  content: "."; display: block; height: 0; clear: both; visibility: hidden;  }   

 .clearfix {*zoom: 1;}   /* IE6、7 专有 */
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <style>
        
        .box1 {
            /* height: 200px; 很多情况下,我们父级盒子,不方便给高度 考虑孩子高度会变  */
            width: 600px;
            background-color: pink;
        }
        .box2 {
            width: 600px;
            height: 240px;
            background-color: purple;
        }
        .son1 { /*  son1 和 son2 是标准流,会撑开盒子 */
            width: 150px;
            height: 100px;
            background-color: skyblue;
            float: left;
        }
        .son2 {
            width: 300px;
            height: 100px;
            background-color: hotpink;
            float: left;
        }
        /* p:after {
            content: "456";
        } */
    /*  伪元素产生出的是  行内元素
        在整个父元素后面内容   转成块元素  
        高度为0   隐藏掉点  清除浮动*/

        .clearfix:after {
            content: ".";  /* 内容为小点, 尽量加不要空, 否则旧版本浏览器有空隙 */
            display: block;  /* 转换为块级元素 */
            height: 0; /* 高度为0 */
            visibility: hidden;  /* 隐藏盒子 */
            clear: both; /* 清除浮动 */
        }
        .clearfix {  /* ie6.7浏览器的处理方式 */
            *zoom: 1; 
            /*  * 代表ie6.7能识别的特殊符号  带有这个*的属性 只有ie6.7才执行  
             zoom 就是ie6.7 清除浮动的方法 */
        }
        </style>
    </head>
    <body>
    <p>123</p>
    <div class="box1 clearfix">
        <div class="son1"></div>
        <div class="son2"></div>
    </div>
    <div class="box2"></div>

    </body>
</html>

优点: 符合闭合浮动思想 结构语义化正确

缺点: 由于IE6-7不支持:after,使用 zoom:1触发 hasLayout。

代表网站: 百度、淘宝网、网易等
注意: content:"."  里面尽量跟一个小点,或者其他,尽量不要为空,否则再firefox 7.0前的版本会有生成空格。

使用before和after双伪元素清除浮动

使用方法:

.clearfix:before,.clearfix:after { 
  content:".";
  display:table;
}
.clearfix:after {
 clear:both;
}
.clearfix {
  *zoom:1;
}
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <style>
        .box1 {
            /* height: 200px; 很多情况下,我们父级盒子,不方便给高度 考虑孩子高度会变  */
            width: 600px;
            background-color: pink;
        }
        .box2 {
            width: 600px;
            height: 240px;
            background-color: purple;
        }
        .son1 { /*  son1 和 son2 是标准流,会撑开盒子 */
            width: 150px;
            height: 100px;
            background-color: skyblue;
            float: left;
        }
        .son2 {
            width: 300px;
            height: 100px;
            background-color: hotpink;
            float: left;
        }
        .clearfix:before, .clearfix:after {
            content: "";
            display: block;  /* 触发bfc 防止外边距合并 */
        }
        .clearfix:after {
            clear: both;
        }
        .clearfix {
            *zoom: 1;
        }
        </style>
    </head>
    <body>
    <div class="box1 clearfix">
        <div class="son1"></div>
        <div class="son2"></div>
    </div>
    <div class="box2"></div>

    </body>
</html>

优点: 代码更简洁

缺点: 由于IE6-7不支持:after,使用 zoom:1触发 hasLayout。

代表网站: 小米、腾讯等
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容