前端解决因为浮动,父级边框塌陷问题

为父类添加一个伪类:after

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<style>
    #father {
        border: 1px red solid;
    }

    /*父类添加一个伪类:after*/
    #father:after {
        content: '';
        display: block;
        clear: both;
    }

    .d1 {
        background: blue;
        width: 100px;
        height: 100px;
        float: left;
        display: inline-block;
    }

    .d2 {
        background: blueviolet;
        width: 100px;
        height: 150px;
        float: left;
        display: inline-block;
    }

    .d3 {
        background: brown;
        width: 100px;
        height: 200px;
        float: right;
        display: inline-block;
    }

</style>
<body>
<div id="father">
    <div class="d1"></div>
    <div class="d2"></div>
    <div class="d3"></div>
</div>

</body>
</html>  
    #father:after {
        content: '';
        display: block;
        clear: both;
    }

 

posted @ 2022-02-17 15:42  蛋挞小子  阅读(34)  评论(0编辑  收藏  举报