背景 - 2

Author Avatar
Ninefire 6月 06, 2018
  • 在其它设备中阅读本文章

背景图片默认是贴着元素的左上角显示。

使用background-position来指定背景图片的位置

  • 使用toprightbottomleftcenter中的任意两个来指定一个背景图片的位置
    例如:
    background-position: top left;图片显示在左上角
    background-position: bottom right;图片显示在右下角

下表列出了所有的情况:

top lefttop centertop right
center leftcenter centercenter right
bottom leftbottom centerbottom 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