内填充和外边距

146 阅读1分钟

内填充

<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>
    <!-- 内填充的作用:元素和其内容之间的间距 -->
    <!-- 注:内填充需要更改div标签的宽度 -->
    <style>
        .box{
            width: 280px;
            height: 280px;
            background-color: blue;
            /* 内填充,四周全有 */
            padding:10px;
            /* 左填充 */
            padding-left: 10px;
     **       /* 右填充 */
            padding-right: 10px;
     **   }
        p{
            background-color: red;
            width: 100px;
            height: 50px;
        }
    </style>
</head>
<body>
    <div class="box">
        <p>hello world</p>
    </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>
        .a{
            width: 300px;
            height: 300px;
            background-color: red;
            /* 外边距,四周都有 */
            margin:30px;

            margin: 10px 20px;
                /* 如果有两个值,上下相同为10px,左右相同为20px */

            margin: 10px 20px 30px 40px ;
                /* 如果有四个值,上边距为10px,右边距为20px,下边距为30px,左边距为40px */

            /* 元素的水平居中 */
            margin: 0 auto;
                /* 上下间距为0,左右间距相等 */
        }
        .b{
            width: 300px;
            height: 300px;
            background-color: blue;
        }
    </style>
</head>
<body>
    <div class="a"></div>
    <div class="b"></div>
</body>
</html>