
定义和用法
background-position-x
是 CSS 中用于设置背景图像水平位置的属性。
background-position-x
通常与background-position-y
配合使用,分别控制背景图像的水平和垂直位置。
默认值: | 0% |
继承性: | 否 |
可动画性: | 支持。请参阅:可动画属性。 |
版本: | CSS3 |
JavaScript 语法: | object.style.backgroundPositionX="center" |
常用的实例
接下来通过多个实例演示background-position-x
属性的用法,来深化您对background-position-x
属性的理解。
1. 使用关键字设置背景图像在 x 轴的位置
.example {
background-image:url('/uploads/20250507/deer.png');
background-repeat: no-repeat;
background-position-x: center;
}
2. 使用百分比设置背景图像在 x 轴的位置
.example {
background-image:url('/uploads/20250507/deer.png');
background-repeat: no-repeat;
background-position-x: 20%;
}
3. 使用像素设置背景图像在 x 轴的位置
.example {
background-image:url('/uploads/20250507/deer.png');
background-repeat: no-repeat;
background-position-x: 50px;
}
CSS 语法
background-position-x: value;
属性值
值 | 描述 |
left |
将背景定位在 x 轴的左侧。 |
center |
将背景定位在 x 轴的中间。 |
right |
将背景定位在 x 轴的右侧。 |
x% |
x 轴的左侧为 0%,右侧为 100%。 百分比值指的是背景定位区域的宽度减去背景图像的宽度。 |
xpos |
距离左侧的水平距离。单位可以是像素(如 0px)或其他 CSS 单位。 |
xpos offset |
双值语法,仅在 Firefox 和 Safari 中支持。
|
initial |
将此属性设置为其默认值。参阅 initial。 |
inherit |
从其父元素继承此属性。参阅 inherit。 |
浏览器支持
表中的数字指定了完全支持background-position-x
属性的第一个浏览器版本。
![]() |
![]() |
![]() |
![]() |
![]() |
|
单值语法 | 1.0 | 12.0 | 49.0 | 1.0 | 15.0 |
双值语法 | 不支持 | 不支持 | 49.0 | 15.4 | 不支持 |
相关页面
CSS 教程:CSS 背景
CSS 参考手册:background-image 属性
CSS 参考手册:background-position 属性
CSS 参考手册:background-position-y 属性
评论区 0
发表评论
教程介绍
CSS 参考手册全面涵盖选择器、盒模型,文本属性等,助您快速检索 CSS 使用方法。
45
章节
44
阅读
0
评论
反馈提交成功
感谢您的反馈,我们将尽快处理您的反馈