background-position
介绍
background-position
为每一个背景图片设置初始位置。这个位置是相对于由background-origin
定义的位置图层。
取值
关键字
center
,用来居中背景图片。关键字
top
,left
,bottom
,right
。用来指定把这个背景图放在哪一个边缘。- 如果只指定一个值,另一个唯独默认被设置成 50%(与
center
等效)。 - 如果一个值是
top
或bottom
,另一个值不能再次指定top
或bottom
否则解析时会报错。 - 如果一个值是
left
或right
,同理另一个值不能再次指定left
或right
。
- 如果只指定一个值,另一个唯独默认被设置成 50%(与
<length>
或<percentage>
。指定相对于左边缘 x 轴或上边缘 y 轴的的坐标。- 如果只指定一个值,这个值指定的是相对于左边缘 x 轴的坐标。相对于上边缘 y 轴的坐标会默认被设置成 50%。
样例:
/_ Keyword values _/
background-position: top;
background-position: bottom;
background-position: left;
background-position: right;
background-position: center;
/_ <percentage> values _/
background-position: 25% 75%;
/_ <length> values _/
background-position: 0px 0px;
background-position: 1rem 2rem;
background-position: 10em 8em;
/_ Multiple images _/
background-position: 0px 0px, center;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
标准化语法
[ left | center | right | top | bottom | <length-percentage> ] | [ left | center | right | <length-percentage> ] [ top | center | bottom | <length-percentage> ]
<length-percentage> = <length> | <percentage>
1
2
3
2
3
与 Web 的区别
- 不支持Global Value类型的关键字
inherit
、initial
和unset
。 - 不支持Edge offsets语法,例如:
background-position: bottom 10px right 20px
1