Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Colors and Backgrounds

The background-position-x property

Reference for CSS background-position-x: horizontal keywords, lengths, percentages and edge offsets, how it relates to background-position, and examples.

The background-position-x property is the horizontal half of background-position. It moves each background image left or right inside its positioning area and leaves the vertical position alone.

It is useful when you want to change only one axis, for example in a hover rule or an animation, without having to repeat the other value.

CSS
.sprite { background-position-x: -48px; }
.arrow  { background-position-x: right 12px; }

Try it

Sliding a background sideways

Syntax

CSS
background-position-x: [ center | [ left | right ]? <length-percentage>? ]#;

background-position-x: 30px;
background-position-x: right 10px;
  • Initial value: 0%
  • Applies to: all elements
  • Inherited: no
  • Animatable: yes (lengths and percentages)
  • Computed value: a list of percentages or lengths, with keywords converted

Values

Value Description
left, center, right Aligns the image with that edge or the middle.
<length> Offset from the left edge. Negative values shift the image out of the area.
<percentage> Aligns the image's point at that percentage with the same point of the area.
left / right plus an offset Measures the offset from the chosen edge (right 20px).
Comma-separated list One value per layer.

Usage notes

  • It is a longhand of background-position. Setting background-position afterwards overwrites both axes, so keep the order of your rules in mind.
  • Good for sprites and animation. Stepping background-position-x through a sprite sheet moves frames sideways; animating it makes scrolling textures. Prefer transform on a child element for smooth, heavy animation, because background repaints cost more.
  • Percentages need slack. With an image exactly as wide as the area, 50% or 100% changes nothing. The effect depends on the difference between the area and the image width.
  • Remember the layer list. With several backgrounds, each comma-separated value applies to the matching layer.
  • Writing modes. This property is physical (always left and right), even in right-to-left text. Logical keywords x-start and x-end exist in the specification but are not widely supported yet.

Browser support

Supported in current browsers. It was missing in some older ones, so check a compatibility table if you support those.