CSS background-position Property
The background-position property controls where a background image is placed within an element's background positioning area.
It accepts position keywords, lengths, percentages, and offsets from specific edges. The property is commonly used to center photographs, align decorative images, control image cropping, and position multiple background layers independently.
background-position Syntax
selector {
background-position: center;
}
The property accepts one, two, three, or four position components, depending on the syntax used.
.default-position {
background-position: 0% 0%;
}
.centered-position {
background-position: center;
}
.keyword-position {
background-position: right bottom;
}
.length-position {
background-position: 20px 30px;
}
.percentage-position {
background-position: 25% 75%;
}
.edge-offset-position {
background-position: right 20px bottom 15px;
}
.multiple-positions {
background-position: left top, right bottom;
}
The property also accepts CSS-wide values such as inherit, initial, revert, revert-layer, and unset.
Property Values
| Value | Description |
|---|---|
left |
Aligns the image with the left edge of the positioning area. |
center |
Centers the image along the specified axis. |
right |
Aligns the image with the right edge of the positioning area. |
top |
Aligns the image with the top edge of the positioning area. |
bottom |
Aligns the image with the bottom edge of the positioning area. |
<length> |
Specifies an offset using units such as px, em, or rem. |
<percentage> |
Aligns a percentage point of the image with the corresponding percentage point of the positioning area. |
edge offset |
Uses an edge keyword followed by a length or percentage to position the image relative to that edge. |
comma-separated list |
Specifies different positions for multiple background image layers. |
The property applies to all elements, is not inherited, and has an initial value of 0% 0%, equivalent to left top. Its computed value is a list of positions, and compatible position values can be animated.
When two numeric values are supplied, the first controls horizontal positioning and the second controls vertical positioning.
How background-position Works
The background-position property places a background image within a background positioning area. By default, that area is the element's padding box.
.panel {
width: 300px;
height: 200px;
background-image: url("photo.jpg");
background-repeat: no-repeat;
background-position: center;
}
The image is centered within the padding box. Its original dimensions are preserved unless background-size changes them.
The Initial Position
.default-background {
background-image: url("pattern.png");
background-position: 0% 0%;
}
The initial value positions the image at the top-left corner of the positioning area.
Background Position Does Not Move Content
The property moves only the background image. It does not change the position of text, child elements, borders, or the element itself.
Positioning and Repetition
When a background repeats, background-position establishes the placement of the image tiles. Repetition can make some positioning differences less noticeable, particularly with seamless patterns.
For demonstrations where the image position should be clearly visible, use background-repeat: no-repeat.
Keyword Positioning
Position keywords provide a simple way to align background images with an element's edges or center.
Horizontal and Vertical Keywords
.top-left {
background-position: left top;
}
.top-center {
background-position: center top;
}
.top-right {
background-position: right top;
}
.middle-left {
background-position: left center;
}
.middle-center {
background-position: center center;
}
.middle-right {
background-position: right center;
}
.bottom-left {
background-position: left bottom;
}
.bottom-center {
background-position: center bottom;
}
.bottom-right {
background-position: right bottom;
}
These combinations provide nine common alignment positions.
Using One Keyword
.centered {
background-position: center;
}
.aligned-right {
background-position: right;
}
.aligned-bottom {
background-position: bottom;
}
A single keyword positions the image along its corresponding axis and centers it along the other axis.
For example, right is equivalent to right center, while bottom is equivalent to center bottom.
Keyword Order
.first-example {
background-position: left top;
}
.second-example {
background-position: top left;
}
These two declarations produce the same result because the horizontal and vertical keywords identify different axes.
Keywords are convenient for aligning decorative icons, logos, and photographs without calculating numeric offsets.
Length Values
Length values position a background image at a specific distance from the top-left corner of the positioning area.
.offset-image {
background-image: url("icon.png");
background-repeat: no-repeat;
background-position: 20px 30px;
}
The image is positioned 20 pixels from the left edge and 30 pixels from the top edge.
Using Different Units
.pixel-position {
background-position: 20px 30px;
}
.rem-position {
background-position: 1rem 2rem;
}
.em-position {
background-position: 1em 1.5em;
}
Pixel values provide fixed offsets. Relative units such as em and rem allow offsets to scale with font sizes.
Negative Lengths
.negative-offset {
background-position: -20px -10px;
}
Negative values move the image to the left or above the positioning area's starting edge. Portions outside the background painting area may be clipped.
Negative offsets are useful for shifting decorative images or displaying a specific portion of a larger image.
Percentage Values
Percentage positioning aligns a point within the background image with the corresponding point in the positioning area.
.percentage-background {
background-image: url("photo.jpg");
background-repeat: no-repeat;
background-position: 50% 50%;
}
At 50% 50%, the center of the image aligns with the center of the positioning area.
How Percentages Are Calculated
For each axis, the browser calculates the difference between the positioning area's size and the rendered image's size, then applies the percentage to that difference.
For example, if the positioning area is 300 pixels wide and the image is 100 pixels wide, the available horizontal difference is 200 pixels.
| Position | Horizontal Offset | Result |
|---|---|---|
0% |
0px | Left edges aligned |
25% |
50px | Image shifted 50px right |
50% |
100px | Image centered horizontally |
75% |
150px | Image shifted 150px right |
100% |
200px | Right edges aligned |
Percentage Positioning Formula
/* Horizontal or vertical offset:
(positioning area size - image size) * percentage */
.example {
background-position: 25% 75%;
}
The first percentage controls horizontal alignment, while the second controls vertical alignment.
If the image and positioning area have identical dimensions along an axis, changing the percentage does not move the image along that axis.
If the image is larger than the positioning area, the calculated difference is negative, allowing percentage positioning to shift which part of the image remains visible.
Three-Value and Four-Value Syntax
The three-value and four-value forms allow an image to be positioned relative to specific edges rather than only from the top-left corner.
Three-Value Syntax
.right-offset {
background-position: right 20px center;
}
.bottom-offset {
background-position: center bottom 15px;
}
The first example places the image 20 pixels from the right edge and centers it vertically.
The second centers the image horizontally and positions it 15 pixels above the bottom edge.
Four-Value Syntax
.bottom-right-offset {
background-position: right 20px bottom 15px;
}
.top-left-offset {
background-position: left 10px top 25px;
}
Each edge keyword is followed by its corresponding offset.
In the first example, the image is positioned 20 pixels from the right edge and 15 pixels from the bottom edge.
Percentage Edge Offsets
.percentage-edge-offset {
background-position: right 10% bottom 20%;
}
The percentages are calculated from the available space between the image and the positioning area's dimensions along each axis.
Choosing the Correct Syntax
Use two-value syntax for ordinary horizontal and vertical positioning. Use three-value or four-value syntax when the image must maintain a specified distance from the right or bottom edge.
Edge-offset syntax is particularly useful for icons and decorative graphics that should remain aligned with a specific corner as the element changes size.
Working with background-size
The background-size property determines the rendered dimensions of the background image. Those dimensions affect how background-position is calculated.
Centering an Image with cover
.hero {
min-height: 300px;
background-image: url("hero.jpg");
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}
The cover value scales the image proportionally until it covers the positioning area. The center position keeps the image centered, with excess portions cropped as necessary.
Changing the Visible Area
.hero-top {
background-size: cover;
background-position: center top;
}
.hero-bottom {
background-size: cover;
background-position: center bottom;
}
These declarations can reveal different portions of the same image when its rendered dimensions exceed the positioning area.
Using contain
.contained-image {
background-image: url("logo.png");
background-size: contain;
background-position: center;
background-repeat: no-repeat;
}
The contain value scales the image to fit entirely within the positioning area, while center aligns it in the middle.
When using cover or contain, the visible effect of positioning depends on the image's aspect ratio and the dimensions of the positioning area.
Working with background-origin
The background-origin property determines which box area is used as the reference for background-position.
| Origin | Positioning Area |
|---|---|
border-box |
The area extending to the outer edge of the border. |
padding-box |
The content and padding area, excluding the border. This is the default. |
content-box |
The content area, excluding padding and borders. |
Positioning Relative to Content
.content-aligned-image {
padding: 30px;
border: 5px solid #1769aa;
background-image: url("icon.png");
background-repeat: no-repeat;
background-origin: content-box;
background-position: right top;
}
The image is positioned at the top-right corner of the content box rather than the padding or border edges.
Positioning Relative to Borders
.border-aligned-image {
padding: 30px;
border: 5px solid #1769aa;
background-image: url("icon.png");
background-repeat: no-repeat;
background-origin: border-box;
background-position: left top;
}
The image is positioned relative to the outer border edge.
For backgrounds using background-attachment: fixed, the positioning area is generally the viewport instead of the element's box.
Multiple Background Layers
The background-position property supports comma-separated values for positioning multiple background images independently.
.layered-panel {
background-image: url("icon.png"), url("pattern.png");
background-position: right top, left bottom;
background-repeat: no-repeat, no-repeat;
}
The first image appears at the top-right corner. The second appears at the bottom-left corner.
Different Numeric Positions
.multi-position {
background-image: url("decoration-one.png"), url("decoration-two.png");
background-position: 20px 30px, right 15px bottom 20px;
background-repeat: no-repeat, no-repeat;
}
The first image uses offsets from the top-left corner. The second uses offsets from the right and bottom edges.
Matching Values to Layers
.three-layers {
background-image: url("one.png"), url("two.png"), url("three.png");
background-position: left top, center, right bottom;
}
Each position corresponds to the background image at the same position in the comma-separated list.
If fewer positions are specified than background image layers, the browser repeats the position list as necessary. Extra positions are not used when there are fewer image layers.
Background colors do not have a position. The property affects background image layers, including CSS gradients.
background-position-x and background-position-y
The background-position-x and background-position-y properties control the horizontal and vertical components of background positioning separately.
.separate-position {
background-position-x: right;
background-position-y: bottom;
}
This is equivalent to setting background-position: right bottom.
Changing One Axis
.horizontal-offset {
background-position-x: 25%;
}
.vertical-offset {
background-position-y: 40px;
}
These longhand properties can be useful when only one positioning axis needs to change.
Using the Shorthand
.combined-position {
background-position: 25% 40px;
}
The shorthand sets both horizontal and vertical positioning in one declaration.
When a later background-position declaration is applied, it sets both axes and overrides earlier longhand values where the cascade permits.
Using the background Shorthand
The background-position property can be specified as part of the background shorthand.
.banner {
background: url("banner.jpg") center no-repeat;
}
The image is centered and displayed without repetition.
Combining Position and Size
.hero {
background: #173b5e url("hero.jpg") center / cover no-repeat;
}
The slash separates background-position from background-size.
When specifying size in the shorthand, a position must appear before the slash.
Using an Edge Offset
.decorative-panel {
background: url("icon.png") right 20px bottom 15px / 48px 48px no-repeat;
}
This places a 48-by-48-pixel image 20 pixels from the right edge and 15 pixels from the bottom edge.
Shorthand Reset Behavior
.panel {
background-position: right bottom;
background: url("pattern.png") no-repeat;
}
The final shorthand declaration resets background-position to its initial value of 0% 0%.
To preserve the position, specify it in the shorthand or place the longhand declaration afterward.
.panel {
background: url("pattern.png") no-repeat;
background-position: right bottom;
}
background-position Example
The following example compares four background positions using the same circular gradient image. Each box has the same dimensions, and the background image is sized to 54 by 54 pixels.
<div class="background-position-demo">
<div class="position-box top-left">left top</div>
<div class="position-box centered">center</div>
<div class="position-box bottom-right">right bottom</div>
<div class="position-box offset">right 15px bottom 15px</div>
</div>
.background-position-demo {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
gap: 15px;
}
.position-box {
display: flex;
align-items: center;
justify-content: center;
min-height: 160px;
padding: 15px;
box-sizing: border-box;
border: 2px solid #1769aa;
border-radius: 6px;
background-color: #e8f2fb;
background-image: radial-gradient(circle, #1769aa 0 60%, #69b7e8 62% 75%, transparent 77%);
background-size: 54px 54px;
background-repeat: no-repeat;
color: #173b5e;
font-weight: bold;
text-align: center;
}
.position-box.top-left {
background-position: left top;
}
.position-box.centered {
background-position: center;
}
.position-box.bottom-right {
background-position: right bottom;
}
.position-box.offset {
background-position: right 15px bottom 15px;
}
Each box displays the same circular image at a different location. Only the background-position value changes.
The first box aligns the image with the top-left corner. The second centers the image horizontally and vertically.
The third places the image against the bottom-right corner. The fourth keeps the image 15 pixels away from both the right and bottom edges.
The foreground text remains in its normal position because background-position affects only the background image.
Important Considerations
Keep the following points in mind when using background-position:
- Default value: The initial value is
0% 0%, equivalent toleft top. - Positioning area: The background positioning area is determined by
background-originand defaults to the padding box. - Keywords: Use
left,right,top,bottom, andcenterfor common alignments. - Length values: Numeric lengths specify offsets, usually measured from the top-left corner.
- Percentage values: Percentages align corresponding points of the image and positioning area rather than simply measuring a percentage of the container.
- Edge offsets: Three-value and four-value syntax allows positioning relative to the right or bottom edges.
- Background size: Image dimensions affect the visible result of percentage positioning and cropping.
- Background repetition: Repeating backgrounds may make positioning changes less noticeable.
- Multiple layers: Comma-separated values can assign different positions to individual background images.
- Background colors: The property positions background images, not background colors.
- Fixed backgrounds: Backgrounds using
background-attachment: fixedare generally positioned relative to the viewport. - Shorthand resets: The
backgroundshorthand resets positioning to0% 0%when no position is specified. - Layout: Changing the background position does not move the element or its foreground content.
- Accessibility: When positioning photographs behind text, ensure the visible portion maintains sufficient text contrast.
Use background-position to control which part of an image is visible or to align decorative imagery without modifying the element's layout.
Browser Support
The background-position property has Baseline Widely available status and is supported in modern versions of Chrome, Edge, Firefox, Safari, and other major browsers.
Keyword, length, percentage, and multiple-background positioning are broadly supported. The three-value and four-value edge-offset syntax is supported by modern browsers but may not work in older browser versions.
Providing a Fallback
.decorative-panel {
background-image: url("icon.png");
background-repeat: no-repeat;
background-position: right bottom;
background-position: right 20px bottom 15px;
}
Browsers that do not recognize the edge-offset syntax can retain the earlier right bottom position.
Visit Can I Use? to check compatibility for advanced background positioning syntax.
Specifications
The background-position property is defined in the CSS Backgrounds and Borders Module Level 3 specification.
