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 |
---|---|---|---|
123 | 124 | 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
Positioning Offset Background Images
I like how percentage background-position works
Focusing a `background-image` on a Precise Location with Percentages
Moving Backgrounds With Mouse Position
Related
background
.element { background: url(texture.svg) top center / 200px 200px no-repeat fixed #f8a100; }
background-attachment
.hero { background-attachment: fixed; }
background-blend-mode
.element { background-blend-mode: screen; }
background-clip
.element { background-clip: padding-box; }
background-color
element{ background-color: #ff7a18; }
background-image
.element { background: url(texture.svg); }
background-origin
.element { background-origin: border-box; }
background-position
.element { background-position: 100px 5px; }
background-repeat
.element { background-repeat: repeat-x; }
background-size
.element { background-size: 300px 100px; }
thank you so much. You made my day !
What if I want the image to be centered right with some margin-right of about 20px?
How can I accomplish that?
How about ‘left 50% 20px’?
This works for me:
background-position: right 20px top 50%;
20px 50%
Is there any best practice to use or not to use Keywords vs percentage?
Can someone explain to me this:
When I use pure pixel value like 10px 20px, it works fine,the picture actually move within container, but when I use parcentage values like 50% 20% the picture doesn’t move within container, the same phenomenon occur when I use four values like right 30px left 35px and also when I use top right(actually it shows me the top right part of an image).
Any help would be greatly appreciated.
12
you can use negative integers
why doesn’t support safari browser “background-position: right 8px top 14px;”
Note: As of Chrome 66 – three-part syntax for background-positions are being deprecated due to parsing ambiguities … see here and [here] (https://drafts.csswg.org/css-values-4/#ref-for-propdef-background-position%E2%91%A0). So If I understand correctly:
background-position: right 45px bottom;
will have to be changed tobackground-position: right 45px bottom 0;
Can I use negative percentage? like background-position-y: -20%
Is it is recommendable or do we have any other alternatives? Please suggest
If setting
background-size: 200% 200%
, then the background position value will be doubled ?