background-position

Avatar of Chris Coyier
Chris Coyier on (Updated on )

UGURUS offers elite coaching and mentorship for agency owners looking to grow. Start with the free Agency Accelerator today.

The background-position property in CSS allows you to move a background image (or gradient) around within its container.

html {
  background-position: 100px 5px; 
}

It has three different types of values:

  • Length values (e.g. 100px 5px)
  • Percentages (e.g. 100% 5%)
  • Keywords (e.g. top right)

The default values are 0 0. This places your background image at the top-left of the container.

Length values are pretty simple: the first value is the horizontal position, second value is the vertical position. So 100px 5px will move the image 100px to the right and five pixels down. You can set length values in px, em, or any of the other CSS length values.

Percentages work a little differently. Get your math hats out: moving a background image by X% means it will align the X% point in the image to the X% point in the container. For example, 50% means it will align the middle of the image with the middle of the container. 100% means it will align the last pixel of the image with the last pixel of the container, and so on.

Keywords are just shortcuts for percentages. It’s easier to remember and write top right than 0 100%, and that’s why keywords are a thing. Here is a list of all five keywords and their equivalent values:

  • top: 0% vertically
  • right: 100% horizontally
  • bottom: 100% vertically
  • left: 0% horizontally
  • center: 50% horizontally if horizontal isn’t already defined. If it is then this is applied vertically.

It’s interesting to note that it doesn’t matter what order you use for the keywords: top center is the same as center top. You can only do this if you’re exclusively using keywords, though. center 10% is not the same as 10% center.

Demo

This demo shows examples of background-position set with length units, percentages, and keywords.

Declaring values

You can give background-position up to four values in modern browsers:

  • If you declare one value, that value is the horizontal offset. The browser sets the vertical offset to center.
  • When you declare two values, the first value is the horizontal offset and the second value is the vertical offset.

Things get a little trickier when you start using three or four values, but you also get more control over your background placement. A three- or four-value syntax alternates between keywords and length or percentage units. You can use any of the keyword values except center in a three- or four-value background-position declaration.

When you specify three values, the browser interpets the “missing” fourth value as 0. Here’s an example of a three-value background-position:

.three-values {
  background-position: right 45px bottom;
}

This positions the background image 45px from the right and 0px from the bottom of the container.

Here’s an example of a four-value background-position:

.four-values {
  background-position: right 45px bottom 20px;
}

This puts the background image 45px from the right and 20px from the bottom of the container.

Notice the order of the values in the examples above: keywords followed by length units. A three- or four-value background-position must follow that format, with a keyword preceding a length or percentage unit.

Demo

This demo includes examples of one value, two value, three value, and four value background-position.

Browser support

The basic values are supported everywhere. The four-value syntax has this support:

This browser support data is from Caniuse, which has more detail. A number indicates that browser supports the feature at that version and up.

Desktop

ChromeFirefoxIEEdgeSafari
25139127

Mobile / Tablet

Android ChromeAndroid FirefoxAndroidiOS Safari
1151154.47.0-7.1

That’s the same level of support as the background-position-x and background-position-y properties.

More information