代码的优化

  1. 代码的嵌套不要超过三层
  2. .cc 权重 > 图下面组群权重(可以理解为:一车土 > 10捧土)

image.png

背景属性 background

背景颜色 background-color

  1. background-color: blueviolet;

背景图片 background-image

  1. ./在当前目录
  2. ../是返回上一级,跳出当前文件夹
    background-image: url(images/2.png);
    

    背景图是否平铺 background-repeat

    background-repeat: repeat;      /* 默认值 平铺 */
    background-repeat: no-repeat; /* 常用 不平铺 */
    background-repeat: repeat-x;  /* 横向平铺 */
    background-repeat: repeat-y;  /* 纵向平铺 */
    

    背景图位置 background-position

  • background-position: 水平位置 垂直位置;

    background-position-x: left/center/right/数值/百分比;  /* 水平位置 */
    background-position-y: top/center/bottom/数值/百分比;  /* 垂直位置 */
    /* 注意!背景图位置是可以简写的(也称为复合属性) */
    /* background-position: 水平位置 垂直位置; */
    
    background-position: right bottom;
    background-position: 200px center;
    background-position: center center; 
    /* 等价于 background-position: center; */
    

    整体简写

  • background: 颜色 图片 平铺 位置;

  • (顺序可以更换 颜色 平铺 图片 位置)
    background: pink url(images/2.png) no-repeat center;
    

    背景图大小 background-size: 水平大小 垂直大小;

    代码汇总

    ```html <!DOCTYPE html>
知其然知其所以然,知其然知其所以然,知其然知其所以然,知其然知其所以然,知其然知其所以然,知其然知其所以然,知其然知其所以然,知其然知其所以然

![image.png](https://cdn.nlark.com/yuque/0/2022/png/25380982/1648687926174-e58bee85-4634-4104-b3c4-d19b65ac6ae9.png#clientId=u75cc1aaa-1c4e-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=658&id=u91b6cf6f&margin=%5Bobject%20Object%5D&name=image.png&originHeight=823&originWidth=847&originalType=binary&ratio=1&rotation=0&showTitle=false&size=8527&status=done&style=stroke&taskId=uf814c3f1-e833-4a2c-bda2-ddc4f10d991&title=&width=677.6)
<a name="cnvgT"></a>
## 背景图固定 background-attachment: fixed;

- 注意!元素设置背景图固定之后,背景图的位置是根据浏览器来设定的
```html
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
        div {
            width: 1000px;
            height: 500px;
            /* 背景:颜色 图片 平铺 位置 */
            background: pink url(../images/2.png) no-repeat center top;
            /* 背景图固定 注意!元素设置背景图固定之后 背景图的位置是根据浏览器来设定的 */
            background-attachment: fixed;
        }
    </style>
</head>

<body>
    <div>背景图最后一个属性啦!!!</div>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
    <h2>内容</h2>
</body>

</html>

image.png666.gif

边框属性 border

边框 border: 大小 线型 颜色;

  1. 会撑大盒子
  2. 去除边框 border: 0;
  3. 线型
  • solid 实线
  • dotted 点状线
  • dashed 虚线
  • double 双实线
    border: 1px solid #000;
    border: 10px double #0f0f0f;
    
  1. 边框可以单独设置某一边

    左边框:border-left: 2px solid #000;
    右边框:border-right: 1px solid #999;
    上边框:border-top: 3px solid #333;
    下边框:border-bottom: 5px solid #222;
    
  2. 复合属性

    border-right: 8px solid #000;
    /*
     * 上述写的 是简写(复合属性)
     * 8px 边框大小 border-width
     * solid 边框线型 border-style
     * #000 边框颜色 border-color
    */
    

    设置边框大小 border-width

  3. border-width: 5px;

  4. 单独设置边框大小,不设置线型,则不显示边框

    设置边框线型 border-style

  • border-style: solid;
  • 也就是说,边框设置了大小和线型,如果没有设置颜色,默认是黑色

    单独设置线型

  1. 注意!边框 我们一般简写为:
  • border: 大小 线型 颜色 ;
  • border-left: 大小 线型 颜色 ;
  1. 但是,如果你单独设置了线型,它也是会出来边框的,出来一个3px的黑色边框

    设置边框颜色 border-color

  • border-color: chartreuse;
  • 上面这三个属性 等同于 :
    border: 5px solid chartreuse;
    

    代码汇总

    ```html <!DOCTYPE html>

<!—

* 注意! 边框我们一般简写 border:大小 线型 颜色
*                        border-left:大小 线型 颜色
* 但是 如果你单独设置了线型 它也是会出来边框的 出来一个3px的黑色边框 

—>

<a name="xbxqQ"></a>
## 边框的注意事项

1. 块状元素可以设置上下左右边框
1. 行内元素在一行显示 ,不可以设置宽高,大小由内容撑开
- 行内元素可以设置左右边框
```html
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        div {/* 块状元素 可以设置上下左右边框 */
            width: 200px;
            height: 200px;
            background: cyan;
            border-top: 5px solid darkblue;
            border-bottom: 10px solid darkmagenta;
            border-left: 10px solid darksalmon;
            border-right: 14px solid darkgoldenrod;
        }

        span {/* 行内元素 在一行显示 不可以设置宽高 大小由内容撑开 */
            /* width: 200px;无效
            height: 200px;无效 */
            background: pink;
            border-left: 10px solid tomato;
            border-right: 5px solid blue;
        }
    </style>
</head>

<body>
    <div>块状元素</div>
    <span>行内元素</span>
</body>

</html>

image.png

使用边框书写三角形

  • transparent 表示边框值为透明

]_T5661~)3JL$YXL8LY9S]2.png
![6~_DA4%OS@XM~G$Z($0{YW.png
XU43X$@GGZOZO%`XHMMLQ3I.png
![HLJH)26V(86E1(RE5T`Z7A.png

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        div {
            width: 0px;
            height: 0px;
            background: pink;
            /* 设置边框 */
            border: 100px solid #000;
            border-left-color: transparent;/* 单独更改左边框的颜色 */
            border-right-color: transparent;/* 单独更改右边框的颜色 */
            border-top-color: transparent;/* 单独更改上边框的颜色 */
        }

        p {/* 三角形的书写 设置宽高都为0 设置一个透明的边框 然后你想要朝下三角形 就设置上边框的颜色  */
            width: 0px;
            height: 0px;
            border: 100px solid transparent;
            border-top-color: pink;/* 单独更改上边框的颜色 */
        }
    </style>
</head>

<body>
    <div></div>
    <p></p>
</body>

</html>

image.png