CSS学习09-背景


CSS的背景

通过CSS背景属性,可以给页面元素添加背景样式。

背景属性可以设置背景颜色、背景图片、背景平铺、背景图片位置、背景图像固定等。


1. 背景颜色

background-color属性定义了元素的背景颜色。

background-color:颜色值;

一般情况下元素背景颜色默认值是transparent(透明),我们也可以手动指定背景颜色为透明色。

示例代码:

<!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: transparent;  透明的 清澈的 */
            background-color: pink;
        }
    </style>
</head>

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

</html>

2. 背景图片

background-image属性描述了元素的背景图像。实际开发常见于logo或者一些装饰性的小图片或者是超大的背景图片,优点是非常便于控制位置.(精灵图也是一种运用场景)

background-image:none | url(url);
参数值 作用
none 无背景图(默认的)
url 使用绝对或相对地址指定背景图像

示例代码:

<!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: 300px;
            height: 300px;
            /* 不要落下 url()  */
            background-image: url(images/logo.png);
        }
    </style>
</head>

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

</html>

3. 背景平铺

如果需要在HTML页面上对背景图进行平铺,可以使用background-repeat属性。

background-repeat: repeat | no-repeat | repeat-x | repeat-y;
参数 作用
repeat 背景图像在纵向和横向上平铺(默认的)
no-repeat 背景图像不平铺
repeat-x 背景图像在横向上平铺
repeat-y 背景图像在纵向平铺

示例代码:

<!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: 300px;
            height: 300px;
            background-color: pink;
            background-image: url(images/logo.png);
            /* 1. 背景图片不平铺 */
            /* background-repeat: no-repeat; */
            /* 2. 默认的情况下,背景图片是平铺的 */
            /* background-repeat: repeat; */
            /* 3. 沿着x轴平铺 */
            /* background-repeat: repeat-x; */
            /* 3. 沿着y轴平铺 */
            background-repeat: repeat-y;
            /* 页面元素既可以添加背景颜色也可以添加背景图片 只不过背景图片会压住背景颜色 */
        }
    </style>
</head>

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

</html>

4. 背景图片位置

利用background-position属性可以改变图片在背景中的位置。

background-position: x y;

参数代表的意思是:x坐标和y坐标。可以使用方位名词或者精确单位

参数
length 百分数 | 由浮点数字和单位标识符组成的长度值
position top | center | bottom | left | center | right 方位名词
  1. 参数是方位名词

    • 如果指定两个值,两个值都是方位名字,则两个值前后顺序无关,比如left top和top left效果一致
    • 如果只指定了一个方位名词,另一个值默认居中对齐
  2. 参数是精确单位

    • 如果参数值是精确坐标,那么第一个肯定是x坐标,第二个一定是y坐标
    • 如果只指定一个数值,那该数值一定是x坐标,另一个默认垂直居中
  3. 参数是混合单位

    • 如果指定的两个值是精确单位和方位名词混合使用,则第一个值是x坐标,第二个值是y坐标

示例代码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: 300px;
            height: 300px;
            background-color: pink;
            background-image: url(images/logo.png);
            background-repeat: no-repeat;
            /* background-position: 方位名词; */
            /* background-position: center top; */
            /* background-position: center right; */
            /* 如果是方位名词 right center 和 center right 效果是等价的 跟顺序没有关系  */
            /* background-position: right center; */
            /* 此时 水平一定是靠右对齐 第二个参数省略 y轴是垂直居中显示的 */
            /* background-position: right; */
            /* 此时 第一个参数一定是top y轴 顶部对齐   第二个参数省略  x轴是水平居中显示的 */
            background-position: top;

        }
    </style>
</head>

<body>
    <div></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: 300px;
            height: 300px;
            background-color: pink;
            background-image: url(images/logo.png);
            background-repeat: no-repeat;
            /* 20px 50px; x轴一定是20  y轴一定是50 */
            /* background-position: 20px 50px; */
            /* background-position: 50px 20px; */
            background-position: 20px;
        }
    </style>
</head>

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

</html>

示例代码3-混合单位

<!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: 300px;
            height: 300px;
            background-color: pink;
            background-image: url(images/logo.png);
            background-repeat: no-repeat;
            /* 20px center; 一定是x 为20  y 是 center 等价于 background-position: 20px;*/
            /* background-position: 20px center; */
            /* 水平是居中对齐  垂直是20 */
            background-position: center 20px;
        }
    </style>
</head>

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

</html>

5. 背景图像固定(背景附着)

background-attachment属性设置背景图像是否固定或者随着页面的其余部分滚动。

background-attachment后期可以制作视差滚动的效果。

background-attachment: scroll | fixed;
参数 作用
scroll 背景图像随对象内容滚动
fixed 背景图像固定

示例代码

<!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>
        body {
            background-image: url(images/bg.jpg);
            background-repeat: no-repeat;
            background-position: center top;
            /* 把背景图片固定住 */
            background-attachment: fixed;
            color: #fff;
            font-size: 20px;
        }
    </style>
</head>

<body>
    <p>天王盖地虎,宝塔镇河妖</p>
    <p>天王盖地虎,宝塔镇河妖</p>
    <p>天王盖地虎,宝塔镇河妖</p>
    <p>天王盖地虎,宝塔镇河妖</p>
    <p>天王盖地虎,宝塔镇河妖</p>
    <p>天王盖地虎,宝塔镇河妖</p>
    <p>天王盖地虎,宝塔镇河妖</p>
    <p>天王盖地虎,宝塔镇河妖</p>
    <p>天王盖地虎,宝塔镇河妖</p>
    <p>天王盖地虎,宝塔镇河妖</p>
    <p>天王盖地虎,宝塔镇河妖</p>
    <p>天王盖地虎,宝塔镇河妖</p>
    <p>天王盖地虎,宝塔镇河妖</p>
    <p>天王盖地虎,宝塔镇河妖</p>
    <p>天王盖地虎,宝塔镇河妖</p>
    <p>天王盖地虎,宝塔镇河妖</p>
    <p>天王盖地虎,宝塔镇河妖</p>
    <p>天王盖地虎,宝塔镇河妖</p>
    <p>天王盖地虎,宝塔镇河妖</p>
    <p>天王盖地虎,宝塔镇河妖</p>
    <p>天王盖地虎,宝塔镇河妖</p>
</body>

</html>

6. 背景复合写法

为了简化背景属性的代码,我们可以将这些属性合并简写在同一个属性background中,从而节约代码量。当使用简写属性时,没有特定的书写顺序,一般习惯约定顺序为:

background: 背景颜色 背景图片地址 背景平铺 背景滚动 背景位置;

例如:

background: transparent url(image.jpg) repeat-y  fixed center top ;

这是实际开发中,我们更提倡的写法。

示例代码

<!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>
        body {
            /* background-image: url(images/bg.jpg);
            background-repeat: no-repeat;
            background-position: center top; */
            /* 把背景图片固定住 */
            /* background-attachment: fixed;
            background-color: black; */
            background: black url(images/bg.jpg) no-repeat fixed center top;
            color: #fff;
            font-size: 20px;
        }
    </style>
</head>

<body>
    <p>天王盖地虎,宝塔镇河妖</p>
    <p>天王盖地虎,宝塔镇河妖</p>
    <p>天王盖地虎,宝塔镇河妖</p>
    <p>天王盖地虎,宝塔镇河妖</p>
    <p>天王盖地虎,宝塔镇河妖</p>
    <p>天王盖地虎,宝塔镇河妖</p>
    <p>天王盖地虎,宝塔镇河妖</p>
    <p>天王盖地虎,宝塔镇河妖</p>
    <p>天王盖地虎,宝塔镇河妖</p>
    <p>天王盖地虎,宝塔镇河妖</p>
    <p>天王盖地虎,宝塔镇河妖</p>
    <p>天王盖地虎,宝塔镇河妖</p>
    <p>天王盖地虎,宝塔镇河妖</p>
    <p>天王盖地虎,宝塔镇河妖</p>
    <p>天王盖地虎,宝塔镇河妖</p>
    <p>天王盖地虎,宝塔镇河妖</p>
    <p>天王盖地虎,宝塔镇河妖</p>
    <p>天王盖地虎,宝塔镇河妖</p>
    <p>天王盖地虎,宝塔镇河妖</p>
    <p>天王盖地虎,宝塔镇河妖</p>
    <p>天王盖地虎,宝塔镇河妖</p>
</body>

</html>

7. 背景颜色半透明

CSS3为我们提供了背景颜色半透明的效果。

background: rgba(0, 0, 0, 0.3);
  • 最后一个参数是alpha透明度,取值范围 0~1之间
  • 我们习惯把0.3 的 0 省略掉,写为background: rgba(0, 0, 0, .3);
  • 注意:背景半透明是指盒子背景半透明,盒子里面的内容不受影响
  • 低于IE 9的版本不支持

示例代码

<!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: 300px;
            height: 300px;
            /* background-color: black; */
            /* background: rgba(0, 0, 0, 0.3); */
            background: rgba(0, 0, 0, .3);
        }
    </style>

</head>

<body>
    <div>隐形的翅膀</div>
</body>

</html>

8. 背景总结

属性 作用
background-color 背景颜色 预定义的颜色值/十六进制/RGB代码
background-image 背景图片 url(图片路径)
background-repeat 是否平铺 repeat/no-repeat/repeat-x/repeat-y
background-position 背景位置 length/position 分别是x 和 y坐标, 切记 如果有 精确数值单位,则必须按照先X 后Y 的写法
background-attachment 背景固定还是滚动 scroll(背景滚动)/fixed(背景固定)
背景简写 书写更简单 背景颜色 背景图片地址 背景平铺 背景滚动 背景位置;
背景色半透明 背景颜色半透明 background: rgba(0,0,0,0.3); 后面必须是 4个值
posted @ 2021-05-12 16:18  Brianxq  阅读(90)  评论(0)    收藏  举报