CSS background-position-x Property


Example

How to position a background-image on x-axis:

div {
  background-image: url('w3css.gif');
  background-repeat: no-repeat;
  background-position-x: center;
}
Try it Yourself »

More "Try it Yourself" examples below.


Definition and Usage

The background-position-x property sets the position of a background image on the x-axis.

Tip: By default, a background-image is placed at the top-left corner of an element, and repeated both vertically and horizontally.

Show demo ❯

Default value: 0%
Inherited: no
Animatable: yes. Read about animatable Try it
Version: CSS3
JavaScript syntax: object.style.backgroundPositionX="center" Try it

Browser Support

The numbers in the table specify the first browser version that fully supports the property.

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


CSS Syntax

background-position-x: value;

Property Values

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

More Examples

Example

How to position a background-image to the right:

body {
  background-image: url('w3css.gif');
  background-repeat: no-repeat;
  background-position-x: right;
}
Try it Yourself »

Example

How to position a background-image using percent:

body {
  background-image: url('w3css.gif');
  background-repeat: no-repeat;
  background-position-x: 50%;
}
Try it Yourself »

Example

How to position a background-image using pixels:

body {
  background-image: url('w3css.gif');
  background-repeat: no-repeat;
  background-position-x: 150px;
}
Try it Yourself »

Example

Use different background properties to create a bacground image that covers its container:

.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 */
}
Try it Yourself »

Related Pages

CSS tutorial: CSS Background

CSS reference: background-image property

CSS reference: background-position property

CSS reference: background-position-y property

HTML DOM reference: backgroundPosition property


Copyright 1999-2023 by Refsnes Data. All Rights Reserved.