背景 - 2
背景图片默认是贴着元素的左上角显示。
使用background-position
来指定背景图片的位置
- 使用
top
、right
、bottom
、left
、center
中的任意两个来指定一个背景图片的位置
例如:background-position: top left;
图片显示在左上角background-position: bottom right;
图片显示在右下角
下表列出了所有的情况:
top left | top center | top right |
---|---|---|
center left | center center | center right |
bottom left | bottom center | bottom right |
如果只给出一个值,则第二个值默认是
center
。
使用两个偏移量来指定一个背景图片的位置
第一个值为水平偏移量
- 若指定的是正值,则图片向右移动指定的距离
- 若指定的是负值,则图片向左移动指定的距离
第二个值为垂直偏移量
- 若指定的是正值,则图片向下移动指定的距离
- 若指定的是负值,则图片向上移动指定的距离
例如:
background-position: 100px 0;
background-position: 50% 50%;
使用background-attchment
来指定背景图片是否随页面滚动
可选值:scroll
:背景图片会随页面其余部分的滚动而滚动(默认值)fixed
:页面其余部分滚动时背景图片不会移动
当背景图片的
background-attachment
设置为fixed
时,背景图片的定位变为相对于浏览器窗口定位。
background-attachment: fixed;
一般仅设置给body
,而不设置给其他元素。
如发现错误请联系我,谢谢你。
本文链接:http://ninefire.tk/HTML&CSS.basics/57.html