背景颜色
background-color:颜色值;
一般情况下元素背景默认颜色值是transparent(透明)
背景图片
background-image: url();
参数值 | 作用 |
none | 无背景图(默认的) |
url | 使用绝对地址或相对地址指定背景图像 |
背景平铺
background-repeat: no-repeat | repeat |repeat-x | repeat-y
参数值 | 作用 |
repeat | 背景图像在纵向和横向上平铺(默认的) |
no-repeat | 背景图像不平铺 |
repeat-x | 背景图像在横向上平铺 |
repeat-y | 背景图像在纵向上平 |
页面元素既可以添加背景颜色也可以添加背景图片,只不过背景图片会压住背景颜色。
背景图片的位置
background-position: x y;
参数代表的意思是:x坐标和y坐标。可以使用方位名词或者精确单位 。
参数值 | 说明 |
length | 百分数 | 由浮点数和单位标识符组成的长度值 |
position | top | center | bottom | left | center | right 方位名词 |
参数是方位名词
- 如果指定的两个值都是方位名词,则两个值前后顺序无关,比如left top 和top left效果一致;
- 如果只指定了一个方位名词,另一个值省略,则第二个值默认居中对齐。
参数是精确单位
- 如果参数值是精确坐标,那么第一个肯定是x轴坐标,第二个一定是y轴坐标;
- 如果只指定一个数值,那该数值一定是x坐标,另一个默认垂直居中。
参数是混合单位
如果指定的两个值是精确单位和方位名词混合使用,则第一个是x坐标,第二个是y坐标。
背景图像固定(背景附着)
background-attachment属性设置背景图像是否固定或者随着页面的其余部分滚动。
background-attachment后期可以制作视差滚动的效果。
background-attachment: scroll | fixed
参数 | 作用 |
scroll | 背景图像是随对象内容滚动 |
fixed | 背景图像固定 |
背景复合写法
当使用简写属性时,没有特定的书写顺序,一般习惯约定顺序为:
background:背景颜色 背景图片地址 背景平铺 背景图像滚动 背景图片位置
background: black url(images/bg.jpg) no-repeat fixed center top;
背景色半透明
background: rgba(0, 0, 0, 0.3);
- 最后一个参数是alpha透明度,取值范围在0-1之间
- 我们习惯把0.3的0省掉,写为background: rgba(0, 0, 0, .3);
- 注意:背景半透明是指盒子背景半透明,盒子里面的内容不受影响
背景总结
属性 | 作用 | 值 |
background-color | 背景颜色 | 预定义的颜色值/十六进制/RGB代码 |
background-image | 背景图片 | url(图片路径) |
background-repeat | 是否平铺 | repeat/no-repeat/repeat-x/repeat-y |
background-position | 背景位置 | length/position 分别是x和y坐标 |
background-attachment | 背景附着 | scroll背景位置(背景滚动)fixed(背景固定) |
背景简写 | 书写更简单 | 背景颜色 背景图片地址 背景平铺 背景滚动 背景位置 |
背景色半透明 | 背景颜色半透明 | background:rgba(0,0,0,0.3); 后面必须是4个值 |