Style backgroundPosition 属性
更多“自己尝试”的例子见下文。
描述
backgroundPosition 属性设置或返回元素内背景图像的位置。
另请参阅
HTML 样式: background 属性
CSS 教程: CSS 背景
CSS3 教程: CSS3 背景
CSS 参考: background-image 属性
CSS 参考: background-position 属性
语法
返回 backgroundPosition 属性
object.style.backgroundPosition
设置 backgroundPosition 属性
object.style.backgroundPosition = value
属性值
值 | 描述 |
---|---|
top left top center top right center left center center center right bottom left bottom center bottom right |
如果只指定一个关键词,另一个值将是“center”。 |
x% y% | x 值表示水平位置,y 值表示垂直位置。左上角是 0% 0%。右下角是 100% 100%。如果只指定一个值,另一个值将是 50%。 |
xpos ypos | x 值表示水平位置,y 值表示垂直位置。左上角是 0 0。单位可以是像素 (0px 0px) 或任何其他 CSS 单位。如果只指定一个值,另一个值将是 50%。您可以混合使用 % 和单位。 |
initial | 将此属性设置为其默认值。阅读关于 initial |
inherit | 从其父元素继承此属性。阅读关于 inherit |
技术详情
默认值 | 0% 0% |
---|---|
返回值 | 一个字符串,表示背景图像的位置 |
CSS 版本 | CSS1 |
浏览器支持
backgroundPosition
是 CSS1 (1996) 功能。
所有浏览器都完全支持。
Chrome | Edge | Firefox | Safari | Opera | IE |
是 | 是 | 是 | 是 | 是 | 是 |
更多示例
示例
将 <div> 元素中背景图像的位置更改为 center bottom
document.getElementById("myDiv").style.backgroundPosition = "center bottom";
自己动手试一试 »
示例
将 <div> 元素中背景图像的位置更改为水平 200px 和垂直 40px
document.getElementById("myDiv").style.backgroundPosition = "200px 40px";
自己动手试一试 »