The background-position-x property

The background-position-x property specifies a horizontal position of an element’s background image.

The property can take the following values: left, center, right, and you can also specify the background position in percentages or pixels.

Syntax

cssselector {
  background-position-x: value;
}

Example

Let's position the background on the left horizontally:

html<div id="elem"></div>
css#elem {
  background-position-x: left;
  background-image: url("bg.png");
  background-repeat: no-repeat;
  width: 400px;
  height: 300px;
  border: 1px solid black;
}

Результат:

Example

Now let's move the background to the right:

html<div id="elem"></div>
css#elem {
  background-position-x: right;
  background-image: url("bg.png");
  background-repeat: no-repeat;
  width: 400px;
  height: 300px;
  border: 1px solid black;
}

Результат:

Example

Let's place the background image in the center:

html<div id="elem"></div>
css#elem {
  background-position-x: center;
  background-image: url("bg.png");
  background-repeat: no-repeat;
  width: 400px;
  height: 300px;
  border: 1px solid black;
}

Результат:

Example

Now let’s set the position for the background as a percentage:

html<div id="elem"></div>
css#elem {
  background-position-x: 20%;
  background-image: url("bg.png");
  background-repeat: no-repeat;
  width: 400px;
  height: 300px;
  border: 1px solid black;
}

Результат:

See also