如何在 x 轴上定位背景图片:
div {
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-position-x: center;
}
亲自试一试 »
下面有更多 "亲自试一试" 示例。
这个background-position-x
属性设置背景图片在 x 轴上的位置。
提示:默认情况下,一个背景图放置在元素的左上角,并垂直和水平重复。
表中的数字指定完全支持该属性的第一个浏览器版本。
Property | |||||
---|---|---|---|---|---|
background-position-x | 1.0 | 12.0 | 49.0 | 1.0 | 15.0 |
background-position-x (with two value syntax) |
Not supported | Not supported | 49.0 | 15.4 | Not supported |
background-position-x:
value;
Value | Description | Demo |
---|---|---|
left | Positions background left side on x-axis. | Demo ❯ |
right | Positions background right side on x-axis. | Demo ❯ |
center | Positions background center on x-axis. | Demo ❯ |
x% | Left side is 0% on x-axis, and right side is 100%. Percentage value refers to width of background positioning area minus width of background image. | Demo ❯ |
xpos | Horizontal distance from left side. Units can be pixels (0px) or any other CSS units. | Demo ❯ |
xpos offset | Two value syntax, only supported in Firefox and Safari. - xpos is set either to "left" or "right". - offset is horizontal distance from background image and "left" or "right" side set with xpos. Units can be pixels or any other CSS units. |
|
initial | Sets this property to its default value. Read about initial | |
inherit | Inherits this property from its parent element. Read about inherit |
如何将背景图片放置在右侧:
body {
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-position-x: right;
}
亲自试一试 »
如何使用百分比定位背景图片:
body {
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-position-x: 50%;
}
亲自试一试 »
如何使用像素定位背景图片:
body {
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-position-x: 150px;
}
亲自试一试 »
使用不同的背景属性创建覆盖其容器的免费图片:
.hero-image {
background-image: url("photographer.jpg"); /* The image used */
background-color: #cccccc; /* Used if the image is unavailable */
height: 300px; /* You must set a specified height */
background-position-x: center; /* Center the image */
background-repeat: no-repeat; /* Do not repeat the image */
background-size: cover; /* Resize the background image to cover the entire container */
}
亲自试一试 »
CSS教程:CSS 背景
CSS 参考:背景图片属性
CSS 参考:背景位置属性
CSS 参考:背景位置 y 属性
HTML DOM 参考:背景位置属性
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!