DigitalOcean provides cloud products for every stage of your journey. Get started with $200 in free credit!
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% verticallyright
: 100% horizontallybottom
: 100% verticallyleft
: 0% horizontallycenter
: 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
Chrome | Firefox | IE | Edge | Safari |
---|---|---|---|---|
25 | 13 | 9 | 12 | 7 |
Mobile / Tablet
Android Chrome | Android Firefox | Android | iOS Safari |
---|---|---|---|
125 | 126 | 4.4 | 7.0-7.1 |
That’s the same level of support as the background-position-x
and background-position-y
properties.
More information
CSS Basics: Using Multiple Backgrounds
background background-position
Chris Coyier
Positioning Offset Background Images
background background-position
Chris Coyier
I like how percentage background-position works
background background-position
Chris Coyier
Focusing a `background-image` on a Precise Location with Percentages
background background-position
Jay Sitter
Moving Backgrounds With Mouse Position
background background-position
Chris Coyier
Related
background
.element { background: url(texture.svg) top center / 200px 200px no-repeat fixed #f8a100; }background
Sara Cope
background-attachment
.hero { background-attachment: fixed; }Chris Coyier
background-blend-mode
.element { background-blend-mode: screen; }Robin Rendle
background-clip
.element { background-clip: padding-box; }background-clip
Chris Coyier
background-color
element{ background-color: #ff7a18; }Chris Coyier
background-image
.element { background: url(texture.svg); }Chris Coyier
background-origin
.element { background-origin: border-box; }Chris Coyier
background-position
.element { background-position: 100px 5px; }background background-position
Chris Coyier
background-repeat
.element { background-repeat: repeat-x; }background-repeat
Geoff Graham
background-size
.element { background-size: 300px 100px; }Chris Coyier