Skip to content

垂直外边距塌陷

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>垂直外边距塌陷</title>
    <style>
        /* 相邻的垂直外边距塌陷 */
        .box1 {
            width: 100px;
            height: 100px;
            background: pink;
            margin-bottom: 20px;
        }

        .box2 {
            width: 100px;
            height: 100px;
            background: pink;
            margin-top: 10px;
        }

        /* 嵌套的垂直外边距塌陷 */
        .father {
            width: 200px;
            height: 200px;
            background: blue;
        }

        .son {
            width: 50px;
            height: 50px;
            margin-top: 50px;
            background: pink;
        }

    </style>
</head>

<body>
    <!-- 相邻的垂直外边距塌陷,咱俩的关系没有那么好! -->
    <div class="box1"></div>
    <div class="box2"></div>
    <!-- 嵌套的垂直外边距塌陷 儿子把爹边距下来了-->
    <div class="father">
        <div class="son">
        </div>
    </div>
</body>

</html>