CSS background-position-x Property

The background-position-x property specifies the horizontal position of an element's background images.

It controls where background images appear along the horizontal axis without changing their vertical positions. The property accepts alignment keywords, lengths, percentages, and offsets from the left or right edge. It can also position multiple background image layers independently.

background-position-x Syntax

selector {
  background-position-x: center;
}

The property accepts horizontal alignment keywords, numeric offsets, percentages, and combinations of edge keywords with offsets.

.left-position {
  background-position-x: left;
}
.center-position {
  background-position-x: center;
}
.right-position {
  background-position-x: right;
}
.pixel-position {
  background-position-x: 25px;
}
.percentage-position {
  background-position-x: 75%;
}
.edge-offset-position {
  background-position-x: right 20px;
}
.multiple-positions {
  background-position-x: left, right;
}

The property also accepts CSS-wide values such as inherit, initial, revert, revert-layer, and unset.

Property Values

Value Description
left Aligns the left edge of the background image with the left edge of its positioning area.
center Centers the background image horizontally within its positioning area.
right Aligns the right edge of the background image with the right edge of its positioning area.
<length> Positions the image horizontally using a length such as px, em, or rem.
<percentage> Aligns the specified percentage point of the image with the corresponding point of the positioning area.
left <length-percentage> Positions the image at a specified offset from the left edge.
right <length-percentage> Positions the image at a specified offset from the right edge.
comma-separated list Specifies horizontal positions for multiple background image layers.

The property applies to all elements, is not inherited, and has an initial value of 0%, equivalent to left. Its computed value is a list of horizontal positions, and compatible numeric positions can be animated.

Only the horizontal component is affected. The vertical component is controlled separately by background-position-y.

How background-position-x Works

The background-position-x property positions a background image horizontally within the background positioning area.

.panel {
  width: 300px;
  height: 180px;
  background-image: url("decoration.png");
  background-repeat: no-repeat;
  background-position-x: right;
}

The image is aligned with the right edge of the positioning area. Its vertical position remains controlled by background-position-y, which defaults to 0%.

The Background Positioning Area

By default, the positioning area is the element's padding box. The background-origin property can change this to the border box or content box.

Changing Only the Horizontal Position

.decorative-panel {
  background-image: url("decoration.png");
  background-repeat: no-repeat;
  background-position: left center;
  background-position-x: right;
}

The final declaration moves the image to the right while preserving its centered vertical position.

Background Positioning Does Not Affect Layout

Moving a background image horizontally does not change the position of the element, its text, or its child elements.

The property changes only the placement of background image layers, including CSS-generated gradients.

Horizontal Position Keywords

The left, center, and right keywords provide three common horizontal alignments.

Aligning Left

.left-aligned {
  background-image: url("icon.png");
  background-repeat: no-repeat;
  background-position-x: left;
}

The image's left edge aligns with the left edge of the positioning area.

Centering Horizontally

.center-aligned {
  background-image: url("icon.png");
  background-repeat: no-repeat;
  background-position-x: center;
}

The image is centered horizontally. Its vertical position does not change.

Aligning Right

.right-aligned {
  background-image: url("icon.png");
  background-repeat: no-repeat;
  background-position-x: right;
}

The image's right edge aligns with the right edge of the positioning area.

Equivalent Percentage Values

Keyword Equivalent Value
left 0%
center 50%
right 100%

Use keywords when simple alignment is sufficient and exact numeric offsets are unnecessary.

Length Values

Length values position a background image at a specific horizontal distance from the left edge of its positioning area.

.pixel-offset {
  background-image: url("icon.png");
  background-repeat: no-repeat;
  background-position-x: 25px;
}

The image begins 25 pixels from the left edge.

Using Relative Units

.rem-offset {
  background-position-x: 2rem;
}
.em-offset {
  background-position-x: 1.5em;
}

Relative units allow positioning offsets to scale with font sizes. The rem unit is based on the root element's font size, while em is based on the element's font size.

Using Negative Lengths

.negative-offset {
  background-position-x: -20px;
}

A negative length shifts the image to the left of its normal starting position.

This can be useful for cropping decorative graphics or shifting a larger background image so a particular portion remains visible.

Using calc()

.calculated-offset {
  background-position-x: calc(50% + 20px);
}

The image is centered horizontally and then shifted 20 pixels to the right.

Use calculated positions when a design requires a combination of proportional alignment and a fixed offset.

Percentage Values

Percentage values align a point within the background image with the corresponding percentage point of the positioning area.

.percentage-position {
  background-image: url("icon.png");
  background-repeat: no-repeat;
  background-position-x: 50%;
}

The 50% value aligns the image's horizontal center with the center of the positioning area.

How Percentage Positioning Is Calculated

The browser subtracts the rendered image width from the positioning area's width and applies the specified percentage to the difference.

For example, consider a positioning area that is 300 pixels wide and an image that is 100 pixels wide.

Percentage Calculated Offset Result
0% 0px Image aligned left
25% 50px Image shifted 50px right
50% 100px Image centered
75% 150px Image shifted 150px right
100% 200px Image aligned right

Percentage Positioning Formula

/* Horizontal offset:
   (positioning area width - image width) * percentage */
.example {
  background-position-x: 75%;
}

If the background image and positioning area have the same width, changing the percentage does not move the image horizontally.

If the image is wider than the positioning area, the difference becomes negative. Percentage positioning can then control which portion of the oversized image remains visible.

Percentages are especially useful when an element changes width across different screen sizes.

Left and Right Edge Offsets

The background-position-x property can position an image at a specific distance from either horizontal edge.

Offset from the Left Edge

.left-offset {
  background-position-x: left 20px;
}

The image begins 20 pixels from the left edge of the positioning area.

Offset from the Right Edge

.right-offset {
  background-position-x: right 20px;
}

The image's right edge is positioned 20 pixels from the right edge of the positioning area.

Percentage Edge Offsets

.right-percentage-offset {
  background-position-x: right 10%;
}

The image is positioned using an offset equal to 10% of the difference between the positioning area's width and the rendered image width, measured inward from right alignment.

Responsive Right Alignment

.decorative-card {
  background-image: url("decoration.svg");
  background-size: 48px 48px;
  background-repeat: no-repeat;
  background-position-x: right 15px;
}

The image stays 15 pixels from the right edge as the card changes width.

This is often easier to maintain than calculating a left offset for elements with flexible dimensions.

Working with background-position-y

The background-position-x and background-position-y properties control the horizontal and vertical components of background positioning independently.

.separate-axes {
  background-image: url("icon.png");
  background-repeat: no-repeat;
  background-position-x: right;
  background-position-y: bottom;
}

This is equivalent to using the background-position shorthand with right bottom.

.combined-axes {
  background-position: right bottom;
}

Changing Only One Axis

.decorative-panel {
  background-image: url("icon.png");
  background-repeat: no-repeat;
  background-position: center center;
  background-position-x: right 20px;
}

The final declaration changes the horizontal position while leaving the vertical position centered.

Independent Numeric Values

.independent-offsets {
  background-position-x: 25%;
  background-position-y: 40px;
}

The image is positioned at 25% horizontally and 40 pixels from the top vertically.

Use the separate longhand properties when only one axis needs to change without replacing the other axis's settings.

Working with background-size

The background-size property controls the rendered dimensions of the background image, which affects horizontal positioning calculations.

Using a Fixed Image Size

.fixed-size-background {
  background-image: url("icon.png");
  background-size: 80px 80px;
  background-repeat: no-repeat;
  background-position-x: center;
}

The image is rendered at 80 by 80 pixels and centered horizontally.

Positioning an Image with cover

.hero {
  min-height: 300px;
  background-image: url("hero.jpg");
  background-size: cover;
  background-repeat: no-repeat;
  background-position-x: center;
}

The cover value scales the image until it covers the positioning area. Horizontal centering can crop equal portions from the left and right when the image is wider than the area.

Choosing Which Side Remains Visible

.hero-left {
  background-size: cover;
  background-position-x: left;
}
.hero-right {
  background-size: cover;
  background-position-x: right;
}

These values can reveal different horizontal portions of an oversized background image.

If the rendered image width matches the positioning area's width, changing its horizontal percentage position may produce no visible movement.

Working with background-origin

The background-origin property determines which area of the CSS box model is used to position background images.

Origin Horizontal Positioning Area
border-box Includes the element's border, padding, and content widths.
padding-box Includes the padding and content widths. This is the default.
content-box Uses only the content width.

Positioning Relative to the Content Box

.content-aligned {
  padding: 30px;
  border: 5px solid #1769aa;
  background-image: url("icon.png");
  background-repeat: no-repeat;
  background-origin: content-box;
  background-position-x: right;
}

The image aligns with the right edge of the content box rather than the padding or border edge.

Positioning Relative to the Border Box

.border-aligned {
  padding: 30px;
  border: 5px solid #1769aa;
  background-image: url("icon.png");
  background-repeat: no-repeat;
  background-origin: border-box;
  background-position-x: right;
}

The image aligns with the outer right edge of the border box.

For background layers using background-attachment: fixed, the positioning area is generally the viewport, so changing background-origin has no effect on those layers.

Multiple Background Layers

The background-position-x property accepts comma-separated values for positioning multiple background images independently.

.layered-panel {
  background-image: url("icon-one.png"), url("icon-two.png");
  background-position-x: left, right;
  background-position-y: top, bottom;
  background-repeat: no-repeat, no-repeat;
}

The first image appears at the top-left corner. The second appears at the bottom-right corner.

Using Different Horizontal Offsets

.multiple-offsets {
  background-image: url("one.png"), url("two.png"), url("three.png");
  background-position-x: 15px, center, right 20px;
  background-position-y: center;
  background-repeat: no-repeat;
}

The first image is positioned 15 pixels from the left, the second is centered horizontally, and the third is positioned 20 pixels from the right.

The single vertical value is repeated for all three layers, keeping them vertically centered.

Matching Values to Layers

Each horizontal position corresponds to the background image at the same position in the comma-separated list.

If fewer positions are provided than image layers, the browser repeats the list as necessary. Extra positions are not used when there are fewer image layers.

The first background image is painted above subsequent images, regardless of their horizontal positions.

Shorthand Properties and Resets

The background-position-x property is the horizontal component of the background-position shorthand.

.panel {
  background-position-x: right;
  background-position-y: center;
}

This is equivalent to the following shorthand declaration:

.panel {
  background-position: right center;
}

Changing the Horizontal Position After the Shorthand

.panel {
  background-position: center bottom;
  background-position-x: left;
}

The final declaration changes the horizontal position to the left while preserving the bottom vertical alignment.

Shorthand Override Behavior

.panel {
  background-position-x: right;
  background-position: center top;
}

The later background-position declaration sets both axes, replacing the earlier horizontal position with center.

Using the background Shorthand

.panel {
  background-position-x: right;
  background: url("pattern.png") no-repeat;
}

The background shorthand resets background positioning to its initial values when no position is specified.

To preserve a horizontal position, specify it after the shorthand.

.panel {
  background: url("pattern.png") no-repeat;
  background-position-x: right;
}

The final declaration places the image on the right while leaving its vertical position at the initial value of 0%.

background-position-x Example

The following example compares four horizontal background positions: left, center, right, and right 15px.

Each box contains the same circular gradient image. The vertical position remains centered so only the horizontal placement changes.

<div class="background-position-x-demo">
  <div class="position-box left">left</div>
  <div class="position-box center">center</div>
  <div class="position-box right">right</div>
  <div class="position-box offset">right 15px</div>
</div>
.background-position-x-demo {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 15px;
}
.position-box {
  display: flex;
  align-items: flex-end;
  justify-content: center;
  min-height: 160px;
  padding: 15px;
  box-sizing: border-box;
  border: 2px solid #1769aa;
  border-radius: 6px;
  background-color: #e8f2fb;
  background-image: radial-gradient(circle, #1769aa 0 60%, #69b7e8 62% 75%, transparent 77%);
  background-size: 54px 54px;
  background-repeat: no-repeat;
  background-position-y: center;
  color: #173b5e;
  font-weight: bold;
  text-align: center;
}
.position-box.left {
  background-position-x: left;
}
.position-box.center {
  background-position-x: center;
}
.position-box.right {
  background-position-x: right;
}
.position-box.offset {
  background-position-x: right 15px;
}

The horizontal position is the only background setting that changes between the four boxes.

Browser Result
left
center
right
right 15px

The first box aligns the circular image with the left edge. The second centers it horizontally.

The third aligns the image with the right edge, while the fourth places it 15 pixels inward from that edge.

All four images remain vertically centered because their background-position-y values are identical.

Important Considerations

Keep the following points in mind when using background-position-x:

  • Default value: The initial value is 0%, equivalent to left.
  • Horizontal positioning: The property changes only the horizontal position of background images.
  • Vertical positioning: Use background-position-y to control vertical placement independently.
  • Keywords: The left, center, and right keywords provide common horizontal alignments.
  • Length values: Numeric lengths provide fixed or relative horizontal offsets.
  • Percentage values: Percentages align corresponding points of the image and positioning area.
  • Edge offsets: Use values such as right 20px to maintain a specified distance from the right edge.
  • Negative values: Negative lengths can shift images beyond the left edge of the positioning area.
  • Background size: The image's rendered width affects percentage positioning and the visible result of cropping.
  • Background origin: The positioning area is controlled by background-origin and defaults to the padding box.
  • Multiple layers: Comma-separated values can assign different horizontal positions to individual background images.
  • Background repetition: Repeating patterns can make horizontal positioning changes less noticeable.
  • Shorthand declarations: Later background-position or background declarations can reset the horizontal position.
  • Element layout: Changing the background position does not move the element or its foreground content.

Use background-position-x when only the horizontal placement of a background image needs to change while preserving its vertical position.

Browser Support

The background-position-x property has broad support in modern browsers, including Chrome, Edge, Firefox, and Safari.

Basic horizontal keywords, lengths, and percentages are widely supported. More advanced edge-offset syntax may have different compatibility requirements in older browsers.

Providing a Fallback

.decorative-panel {
  background-image: url("icon.png");
  background-repeat: no-repeat;
  background-position: right center;
  background-position-x: right 20px;
}

The background-position declaration provides a widely supported fallback. Browsers that recognize the later horizontal edge offset can position the image 20 pixels from the right edge.

Visit Can I Use? to check browser compatibility for the property.

Specifications

The background-position-x property is defined in the CSS Backgrounds and Borders Module Level 4 specification. The related background-position shorthand is also described in Level 3.

CSS Backgrounds and Borders Module Level 4

CSS Backgrounds and Borders Module Level 3