background-position-x CSS property sets the initial horizontal position for each background image. The position is relative to the position layer set by background-origin .

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

The value of this property is overridden by any declaration of the background or background-position shorthand properties applied to the element after it.

句法

/* Keyword values */
background-position-x: left;
background-position-x: center;
background-position-x: right;
/* <percentage> values */
background-position-x: 25%;
/* <length> values */
background-position-x: 0px;
background-position-x: 1cm;
background-position-x: 8em;
/* Side-relative values */
background-position-x: right 3px;
background-position-x: left 25%;
/* Multiple values */
background-position-x: 0px, center;
/* Global values */
background-position-x: inherit;
background-position-x: initial;
background-position-x: unset;
					

background-position-x property is specified as one or more values, separated by commas.

left

Aligns the left edge of the background image with the left edge of the background position layer.

center

Aligns the center of the background image with the center of the background position layer.

right

Aligns the right edge of the background image with the right edge of the background position layer.

<length>

The offset of the given background image's left vertical edge from the background position layer's left vertical edge. (Some browsers allow assigning the right edge for offset).

<percentage>
The offset of the given background image's horizontal position relative to the container. A value of 0% means that the left edge of the background image is aligned with the left edge of the container, and a value of 100% means that the right edge of the background image is aligned with the right edge of the container, thus a value of 50% horizontally centers the background image.

形式定义

初始值 left
适用于 所有元素
继承 no
百分比 refer to width of background positioning area minus height of background image
计算值 A list, each item consisting of: an offset given as a combination of an absolute length and a percentage, plus an origin keyword
动画类型 discrete

形式句法

[ center | [ [ left | right | x-start | x-end ]? <length-percentage>? ]! ]#

where
<length-percentage> = <length> | <percentage>

范例

基本范例

The following example shows a simple background image implementation, with background-position-x and background-position-y used to define the image's horizontal and vertical positions separately.

HTML

<div></div>
					

CSS

div {
  width: 300px;
  height: 300px;
  background-color: skyblue;
  background-image: url(https://media.prod.mdn.mozit.cloud/attachments/2020/07/29/17350/3b4892b7e820122ac6dd7678891d4507/firefox.png);
  background-repeat: no-repeat;
  background-position-x: center;
  background-position-y: bottom 10px;
}
					

结果

规范

规范 状态 注释
CSS Backgrounds and Borders Module Level 4
The definition of 'background-position-x' in that specification.
编者草案 Initial specification of longhand sub-properties of background-position to match longstanding implementations.

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
background-position-x Chrome 完整支持 1 Edge 完整支持 12 Firefox 完整支持 49 IE 完整支持 6 Opera 完整支持 15 Safari 完整支持 1 WebView Android 完整支持 ≤37 Chrome Android 完整支持 18 Firefox Android 完整支持 49 Opera Android 完整支持 18 Safari iOS 完整支持 1 Samsung Internet Android 完整支持 1.0
Two-value syntax (support for offsets from any edge) Chrome 不支持 No Edge 不支持 12 — 79 Firefox 完整支持 49 IE 完整支持 9 Opera 不支持 No Safari 不支持 No WebView Android 不支持 No Chrome Android 不支持 No Firefox Android 完整支持 49 Opera Android 不支持 No Safari iOS 不支持 No Samsung Internet Android 不支持 No

图例

完整支持

完整支持

不支持

不支持

另请参阅

元数据

  • 最后修改: