CSS background-position-y 属性
示例
如何定位背景图片在 y 轴上的位置
div {
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-position-y: center;
}
自己动手试一试 »
更多“自己尝试”的例子见下文。
定义和用法
background-position-y
属性设置背景图片在 y 轴上的位置。
提示: 默认情况下,背景图片放置在元素的左上角,并垂直和水平重复。
查看演示 ❯默认值 | 0% |
---|---|
继承 | no |
可动画 | 是。 阅读关于animatable 尝试一下 |
版本 | CSS3 |
JavaScript 语法 | object.style.backgroundPositionY="center" 尝试一下 |
浏览器支持
表中的数字指定了完全支持该属性的第一个浏览器版本。
属性 | |||||
---|---|---|---|---|---|
background-position-y | 1.0 | 12.0 | 49.0 | 1.0 | 15.0 |
background-position-y (使用双值语法) |
不支持 | 不支持 | 49.0 | 15.4 | 不支持 |
CSS 语法
background-position-y: 值;
属性值
值 | 描述 | 演示 |
---|---|---|
top | 将背景图片定位在 y 轴的顶部。 | 演示 ❯ |
bottom | 将背景图片定位在 y 轴的底部。 | 演示 ❯ |
center | 将背景图片定位在 y 轴的中心。 | 演示 ❯ |
y% | 在 y 轴上,顶部为 0%,底部为 100%。百分比值是指背景定位区域的高度减去背景图片的高度。 | 演示 ❯ |
ypos | 与顶部边缘的垂直距离。单位可以是像素 (0px) 或任何其他 CSS 单位。 | 演示 ❯ |
ypos 偏移 | 双值语法,仅在 Firefox 和 Safari 中支持。 - ypos 被设置为 "top" 或 "bottom"。 - offset 是背景图片和由 ypos 设置的 "top" 或 "bottom" 边缘之间的垂直距离。单位可以是像素或任何其他 CSS 单位。 |
|
initial | 将此属性设置为其默认值。阅读关于initial | |
inherit | 从其父元素继承此属性。阅读关于inherit |
更多示例
示例
如何将背景图片定位在顶部
body {
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position-y: bottom;
}
自己动手试一试 »
示例
如何使用百分比在 y 轴上定位背景图片
body {
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position-y: 50%;
}
自己动手试一试 »
示例
如何使用像素在 y 轴上定位背景图片
body {
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position-y: 80px;
}
自己动手试一试 »
相关页面
CSS 教程:CSS 背景
CSS 参考: background-image 属性
CSS 参考: background-position 属性
CSS 参考: background-position-x 属性
HTML DOM 参考: backgroundPosition 属性