盒子模型外边距margin

1.下外边距

代码:

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

        .one{
            margin-bottom: 20px;
        }
    </style>
</head>

<body>
       <div class="one">1</div>
       <div>2</div>
</body>

</html>

运行效果:
        

2.上外边距

代码:

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


    
     .two{
           margin-top: 20px;
     }

    </style>
</head>

<body>
       <div class="one">1</div>
       <div class="two">2</div>
</body>

</html>

运行效果:

 3.margin的简写方式

marrgin:30px       上下左右都是30像素

代码:

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

        /* .one{
            margin-bottom: 20px;
        } */
    
     .two{
           /* margin-top: 20px; */
           margin: 30px;
     }

    </style>
</head>

<body>
       <div class="one">1</div>
       <div class="two">2</div>
</body>

</html>

运行效果:

和padding写法类似

猜你喜欢

转载自blog.csdn.net/weixin_42900834/article/details/123455834