清除浮动的几种方法

2022-03-08 14:05:03

额外标签法

<!DOCTYPE html>
<html lang="en">
<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>
        .father {
            width: 400px;
            background-color: pink;
        }

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

        .clearfix {
            clear: both;
        }
    </style>
</head>
<body>
    <div class="father">
        <div class="son"></div>
        <div class="clearfix"></div>
    </div>
</body>
</html>

单伪元素清除法

<!DOCTYPE html>
<html lang="en">
<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>
        .father {
            width: 400px;
            background-color: pink;
        }

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

        .clearfix::after {
            content: '';
            display: block;
            clear: both;
        }
        /* 补充写法 */
        .clearfix::after {
            content: '';
            display: block;
            clear: both;
            height: 0;
            visibility: hidden;
        }
    </style>
</head>
<body>
    <div class="father clearfix">
        <div class="son"></div>
    </div>
</body>
</html>

双伪元素清除法

<!DOCTYPE html>
<html lang="en">
<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>
        .father {
            width: 400px;
            background-color: pink;
        }

        .son {
            float: left;
            width: 100px;
            height: 400px;
            background-color: blue;
        }
        /* 防止 margin-top 塌陷 */
        .clearfix::before,
        .clearfix::after {
            content: '';
            display: table;
        }
        .clearfix::after {
            clear: both;
        }
    </style>
</head>
<body>
    <div class="father clearfix">
        <div class="son"></div>
    </div>
</body>
</html>

添加 overflow

<!DOCTYPE html>
<html lang="en">
<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>
        .father {
            width: 400px;
            background-color: pink;
            overflow: hidden;
        }

        .son {
            float: left;
            width: 100px;
            height: 400px;
            background-color: blue;
        }
    </style>
</head>
<body>
    <div class="father">
        <div class="son"></div>
    </div>
</body>
</html>