background-position

Sets the position of the background image within the element.

Initial value
0% 0%
Inherited
No
Animatable
yes, as a length or percentage
Applies to
all elements
JavaScript
element.style.backgroundPosition = "center top"
On this page

Example

#
CSS
.search {
  background: url("/icons/search.svg") no-repeat left 12px center / 16px;
  padding-left: 36px;
}
Preview

Definition and usage

#

The background-position property specifies where the background image sits in the element. It accepts keywords (top, center, bottom, left, right), percentages or lengths, first for the horizontal axis, then for the vertical one.

Percentages work cleverly: 0% 0% puts the image’s top left corner at the element’s top left corner, and 100% 100% puts the image’s bottom right corner at the element’s bottom right corner. 50% 50% centres the image.

With background-size: cover, the position decides which part of the photo stays visible after cropping. It is worth matching it to the most important part of the photo.

Syntax

#
Syntax
background-position: <x-position> <y-position>?;

Values

#
  • left top

    The top left corner. The default value.
  • center

    The centre of the element on both axes.
  • right bottom

    The bottom right corner.
  • right 16px top 16px

    with offset

    16px from the right and 16px from the top.
  • 25% 75%

    percentages

    A point at 25% horizontally and 75% vertically.

Values in practice

#

left top

The top left corner. The default value.
.demo { background-position: left top; }

center

The centre of the element on both axes.
.demo { background-position: center; }

right bottom

The bottom right corner.
.demo { background-position: right bottom; }

right 16px top 16px

16px from the right and 16px from the top.
.demo { background-position: right 16px top 16px; }

25% 75%

A point at 25% horizontally and 75% vertically.
.demo { background-position: 25% 75%; }

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Place an icon in a text field with background-position: left 12px center and make room with padding.
  • With cover, set the position to the most important part of the photo, for example center top for portraits.

Related entries

#

Practise it in lessons

#

Spotted a mistake or missing an example? Write to us.