background-position

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.

See the Pen background-position values by CSS-Tricks (@css-tricks) on CodePen.

Declaring Values

You can give background-position up to four values in modern browsers (see the Browser Support table for details).

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:

#threevalues {
  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:

#fourvalues {
  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.

See the Pen background-position 1, 2, 3, and 4 value syntax by CSS-Tricks (@css-tricks) on CodePen.

Related

More Resources

Browser Support

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

Chrome Safari Firefox Opera IE Android iOS
25+ 7+ 13+ 10.5+ 9+ 4+ 7.1+

Comments

  1. User Avatar
    Suyog
    Permalink to comment#

    thank you so much. You made my day !

  2. User Avatar
    Christian Solorzano
    Permalink to comment#

    What if I want the image to be centered right with some margin-right of about 20px?

    How can I accomplish that?

  3. User Avatar
    Swagato Bhatta
    Permalink to comment#

    Is there any best practice to use or not to use Keywords vs percentage?

  4. User Avatar
    ji
    Permalink to comment#

Submit a Comment

Posting Code

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.

icon-anchoricon-closeicon-emailicon-linkicon-logo-staricon-menuicon-nav-guideicon-searchicon-staricon-tag