css清除浮动,就是为了解决父元素高度塌陷问题

1. 给父元素设置个固定高

<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 {
            height: 200px;
            background-color: pink;
        }

        .son {
            float: left;
            width: 200px;
            height: 200px;
            background-color: skyblue;
        }
    </style>
</head>

<body>
    <div class="father">
        <div class="son">儿子</div>
    </div>
    <div class="box">下面的盒子</div>
</body>

 2. 额外标签法,在末尾加个块元素,一般都是加个div

<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>
        .son {
            float: left;
            width: 200px;
            height: 200px;
            background-color: skyblue;
        }
    </style>
</head>

<body>
    <div class="father">
        <div class="son">儿子</div>
    </div>
    <!-- 额外加个块级标签 -->
    <div style="clear:both;"></div>
    <div class="box">下面的盒子</div>
</body>

3. 给父元素添加overflow: hidden;属性

<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 {
            overflow: hidden;
        }

        .son {
            float: left;
            width: 200px;
            height: 200px;
            background-color: skyblue;
        }
    </style>
</head>

<body>
    <div class="father">
        <div class="son">儿子</div>
    </div>
    <div class="box">下面的盒子</div>
</body>

4. 使用after伪元素清除浮动

<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 {
            overflow: hidden;
        }

        .son {
            float: left;
            width: 200px;
            height: 200px;
            background-color: skyblue;
        }

        /* after伪元素清除浮动 */
        .clearfix:after {
            /*伪元素是行内元素 正常浏览器清除浮动方法*/
            content: "";
            display: block;
            height: 0;
            clear: both;
            visibility: hidden;
        }

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

<body>
    <div class="father clearfix">
        <div class="son">儿子</div>
    </div>
    <div class="box">下面的盒子</div>
</body>

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

<!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 {
            overflow: hidden;
        }

        .son {
            float: left;
            width: 200px;
            height: 200px;
            background-color: skyblue;
        }

        /* 双伪元素清除浮动 */
        .clearfix:after,
        .clearfix:before {
            content: "";
            display: block;
            clear: both;
        }
    </style>
</head>

<body>
    <div class="father clearfix">
        <div class="son">儿子</div>
    </div>
    <div class="box">下面的盒子</div>
</body>

</html>

原文链接:https://blog.csdn.net/qq_52845451/article/details/126572567

最后修改:2023 年 10 月 30 日
如果觉得我的文章对你有用,请随意赞赏