CSS Reference ยท Colors and Backgrounds
The background-position property
Reference for CSS background-position: keywords, lengths, percentages, the four-value edge offset syntax, how percentages really work, and live examples.
The background-position property sets where each background image sits inside its positioning area (the padding box by default, see background-origin). It does not stretch the image; it moves it. If the image is smaller than the area, you decide which part of the area it occupies, and the rest is filled by repeating or left empty.
You can use keywords (left, center, right, top, bottom), lengths, percentages, or a mix. A single value sets the horizontal position and centers the image vertically.
.logo { background-position: right 12px top 12px; } .hero { background-position: center 30%; }
Try it
Syntax
background-position: <bg-position>#; /* one value: horizontal, vertical becomes center */ background-position: left; /* two values: horizontal then vertical (keywords may be in either order) */ background-position: 25% 75%; background-position: bottom right; /* four values: edge keyword plus offset */ background-position: right 10px bottom 5px;
- Initial value:
0% 0% - Applies to: all elements
- Inherited: no
- Animatable: yes (lengths and percentages)
- Computed value: for each layer, two lengths or percentages with keywords converted (
left=0%,center=50%,right=100%)
Values
| Value | Description |
|---|---|
left, center, right |
Horizontal keywords. |
top, center, bottom |
Vertical keywords. |
<length> |
An offset of the image's top-left corner from the top-left corner of the area. Negative values move it out. |
<percentage> |
Aligns the point at that percentage of the image with the point at the same percentage of the area. |
| Edge + offset | In the three- and four-value forms, an offset is measured from the named edge: right 10px sits 10px from the right. |
Usage notes
- Percentages are not what they seem.
50%means "the image's midpoint meets the area's midpoint", which is why50% 50%centers.100%puts the image's right edge against the area's right edge. A background the same size as the area has nowhere to move, so percentages have no visible effect. - A single value sets one axis, and the other is centered.
background-position: leftmeansleft center, andbackground-position: topmeanscenter top, because a keyword that clearly belongs to one axis is read on that axis. - Two numeric values are always x then y. Keywords can be swapped; numbers cannot mix with reversed keywords.
- Use edge offsets for icons.
right 12px centerkeeps an arrow at a fixed distance from the edge, in any width.calc(100% - 12px)can do the same but is longer. - Lists. Multiple layers take comma-separated positions in the same order as the images.
- In the shorthand, write the position first, then a slash and the size:
center / cover. background-attachment: fixedpositions relative to the viewport instead of the element.
Browser support
Supported in every browser. The three- and four-value edge offset syntax is supported by all current browsers.
Related
background-position-x,background-position-yโ set one axisbackground-originโ which box is the positioning areabackground-size,background-repeatobject-positionโ the same idea for<img>and<video>- Lesson: Backgrounds