清除浮动

方法1:在浮动元素后面增加空白元素用上clear:both

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>清除浮动</title>
    <style>
        .box {
            background-color: #2AB561;
            border: 1px solid red;
        }

        .one {
            width: 100px;
            height: 50px;
            background-color: #0DA5D6;
            float: left;
        }

        .two {
            float: left;
            width: 100px;
            height: 50px;
            background-color: #1b6d85;
        }

        .three {
            width: 200px;
            height: 100px;
            background-color: #0f0f0f;
        }
        .four{
            clear: both;
        }

    </style>
</head>
<body>
<div class="box">
    <div class="one"></div>
    <div class="two"></div>
    <div class="four"></div>
</div>
<div class="three"></div>
</body>
</html>

方法2:父级添加overflow:hidden

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>清除浮动</title>
    <style>
        .box {
            background-color: #2AB561;
            border: 1px solid red;
            /*overflow触发BFC,BFC可以清除浮动*/
            overflow: hidden; 
        }

        .one {
            width: 100px;
            height: 50px;
            background-color: #0DA5D6;
            float: left;
        }

        .two {
            float: left;
            width: 100px;
            height: 50px;
            background-color: #1b6d85;
        }

        .three {
            width: 200px;
            height: 100px;
            background-color: #0f0f0f;
        }


    </style>
</head>
<body>
<div class="box">
    <div class="one"></div>
    <div class="two"></div>
</div>
<div class="three"></div>
</body>
</html>

方法3:添加伪元素

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>清除浮动</title>
    <style>
        .box {
            background-color: #2AB561;
            border: 1px solid red;
        }

        .one {
            width: 100px;
            height: 50px;
            background-color: #0DA5D6;
            float: left;
        }

        .two {
            float: left;
            width: 100px;
            height: 50px;
            background-color: #1b6d85;
        }

        .three {
            width: 200px;
            height: 100px;
            background-color: #0f0f0f;
        }

        .clearFloat:after {
            /*避免有空隙*/
            content: '.';
            /*变成块级元素*/
            display: block;
            /*隐藏content*/
            visibility: hidden;
            clear: both;
            /*清除盒子高度*/
            height: 0;

        }

        .clearFloat {
            /*兼容IE6,7*/
            *zoom: 1;
        }


    </style>
</head>
<body>
<div class="box clearFloat">
    <div class="one"></div>
    <div class="two"></div>
</div>
<div class="three"></div>
</body>
</html>

方法4:双伪元素清除浮动

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>清除浮动</title>
    <style>
        .box {
            background-color: #2AB561;
            border: 1px solid red;
        }

        .one {
            width: 100px;
            height: 50px;
            background-color: #0DA5D6;
            float: left;
        }

        .two {
            float: left;
            width: 100px;
            height: 50px;
            background-color: #1b6d85;
        }

        .three {
            width: 200px;
            height: 100px;
            background-color: #0f0f0f;
        }

        .clearFloat:before ,.clearFloat:after {
            /*避免有空隙*/
            content: '';
            /*变成块级元素*/
            display: table;
        }
        .clearFloat:after{
            clear: both;
        }

        .clearFloat {
            /*兼容IE6,7*/
            *zoom: 1;
        }


    </style>
</head>
<body>
<div class="box clearFloat">
    <div class="one"></div>
    <div class="two"></div>
</div>
<div class="three"></div>
</body>
</html>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 浮动,从诞生那天起,它就是个特别的属性——既为网页布局带来新的方法,却又随之产生一系列的问题。当然,随着时间的推移...
    郝特么冷阅读 860评论 0 6
  • 在CSS规范中,浮动定位不属于正常的页面流(page flow),是独立定位的。所以,只含有浮动元素的父容器,在显...
    隔壁的UNCLE张阅读 502评论 0 4
  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,815评论 1 92
  • 原文地址:浮动与清除浮动 浮动 浮动的概念 浮动元素会脱离文档流并向左/向右浮动,直到碰到父元素或者另一个浮动元素...
    薛普定朗谔克阅读 786评论 3 14
  • 关于清除浮动的四种方法 浮动对页面的影响: 如果一个父盒子中有一个子盒子,并且父盒子没有设置高,子盒子在父盒子中进...
    jrg陈咪咪sunny阅读 714评论 0 1