| 属性 | 描述 |
|---|---|
| background-color | 元素的背景颜色 |
| background-image | 元素的背景图片 |
| background-position | 背景图片的起始位置 |
| background-attachment | 背景图片是否固定 |
| background-repeat | 背景图片是否重复及如何重复 |
background-color
背景颜色的值可以是颜色的英文名,Hex 和 RGB。background-color 的作用区域包括内容,内边距 padding 和边框,不包括外边距 margin。
background-color 有一个特殊的值 transparent ,它是全透明黑色的标识,类似 rgba(0,0,0,0)。当我们不希望元素有背景颜色,且不希望浏览器的设置影响到元素的背景颜色,可以通过 transparent 设定。
应该是类似 iOS 中
color = clear,背景色是透明的。
|
|
background-image
用于设定元素的背景图片,background-image 覆盖的位置与 background-color 一致,默认左上角是起始位置,并在水平和垂直方向上重复。background-image 的值可以是绝对地址,也可以是相对地址,语法是 url(...) ,或者是 none,同时设定背景颜色和图片时,图片会覆盖背景颜色。
background-repeat
背景图片的重复方式使用 background-repeat 标记,它的值有以下几个:
| 值 | 描述 |
|---|---|
| repeat | 重复 |
| no-repeat | 不重复 |
| repeat-x | x 轴重复 |
| repeat-y | y 轴重复 |
background-attachment
用于标记图片的显示方式,有以下几个常用的值:
| 值 | 描述 |
|---|---|
| scroll | 默认值,背景图片跟随滚动条滚动。 |
| fixed | 当页面的其余部分滚动时,背景图片不会移动 |
background-position
background-position 有以下几个常用的值:
|
|
另外需要注意的是,当 background-attachment 是 fixed 时,位置是按浏览器窗口计算,当 background-attachment 是 scroll 时,位置是按父元素计算。