CSS background-origin Property
The background-origin property specifies which area of an element's box is used to position its background images.
It determines whether background images are positioned relative to the border box, padding box, or content box. This property works with background-position and background-size to control image placement without changing the element's dimensions or layout.
background-origin Syntax
selector {
background-origin: padding-box;
}
The property accepts three keywords that correspond to areas of the CSS box model.
.border-background {
background-origin: border-box;
}
.padding-background {
background-origin: padding-box;
}
.content-background {
background-origin: content-box;
}
Multiple values can be separated by commas when an element has multiple background image layers.
.layered-background {
background-origin: content-box, padding-box;
}
The property also accepts CSS-wide values such as inherit, initial, revert, revert-layer, and unset.
Property Values
| Value | Description |
|---|---|
border-box |
Positions the background image relative to the outer edge of the border box, including the border area. |
padding-box |
Positions the background image relative to the outer edge of the padding box, excluding the border area. This is the initial value. |
content-box |
Positions the background image relative to the content box, excluding the padding and border areas. |
The property applies to all elements, is not inherited, and has an initial value of padding-box. Its computed value is a list of positioning keywords, and its animation type is discrete.
The selected box determines the background positioning area used by properties such as background-position and background-size.
How background-origin Works
Every element in the CSS box model consists of a content area, padding, and borders. The background-origin property selects which of these areas establishes the coordinate system for positioning a background image.
Consider an element with a border, padding, and a small background image.
.panel {
padding: 30px;
border: 10px solid #1769aa;
background-image: url("icon.png");
background-repeat: no-repeat;
background-position: top left;
background-origin: padding-box;
}
With padding-box, the image is positioned at the top-left corner of the padding box, immediately inside the border.
Changing the value to border-box moves the positioning origin to the outer border edge. Changing it to content-box moves the positioning origin inward to the content area.
Positioning vs. Painting
The positioning area does not necessarily limit where the image can be painted. The background-clip property controls the painting boundary.
For example, a background image positioned relative to the content box can still extend into the padding or border areas when the clipping settings permit it.
The Default Behavior
By default, background-origin is padding-box, while background-clip is border-box.
This means background images are normally positioned relative to the padding box but may be painted beneath the border.
Using border-box
The border-box value positions the background image relative to the outer edge of the element's border.
.border-box-panel {
padding: 25px;
border: 10px solid #1769aa;
background-image: url("pattern.png");
background-position: top left;
background-repeat: no-repeat;
background-origin: border-box;
}
The background positioning area includes the content, padding, and border dimensions.
When background-position: top left is used, the image begins at the top-left corner of the border box.
Using a Background Gradient
.border-gradient {
padding: 25px;
border: 10px solid transparent;
background-image: linear-gradient(135deg, #1769aa, #69b7e8);
background-origin: border-box;
background-clip: border-box;
}
The gradient is sized and positioned relative to the border box, and the clipping value allows it to extend beneath the transparent border.
This value is useful when a background image needs to align with the outer dimensions of an element, including its border.
Using padding-box
The padding-box value is the default. It positions background images relative to the outer edge of the padding box.
.padding-box-panel {
padding: 25px;
border: 10px solid #1769aa;
background-image: url("pattern.png");
background-position: top left;
background-repeat: no-repeat;
background-origin: padding-box;
}
The positioning area includes the content and padding but excludes the border.
With background-position: top left, the image begins immediately inside the border.
Positioning a Decorative Image
.decorative-card {
padding: 30px;
border: 2px solid #1769aa;
background-image: url("decoration.svg");
background-position: right top;
background-repeat: no-repeat;
background-origin: padding-box;
}
The image is aligned to the top-right corner of the padding box rather than the outer border edge.
Use padding-box for ordinary background positioning when no special alignment is needed.
Using content-box
The content-box value positions background images relative to the element's content area, excluding both padding and borders.
.content-box-panel {
padding: 25px;
border: 10px solid #1769aa;
background-image: url("pattern.png");
background-position: top left;
background-repeat: no-repeat;
background-origin: content-box;
}
The background image begins at the top-left corner of the content box, after the border and padding.
Aligning an Image with Content
.content-decoration {
padding: 30px;
border: 2px solid #1769aa;
background-image: url("decoration.svg");
background-position: right top;
background-repeat: no-repeat;
background-origin: content-box;
}
The image aligns with the top-right corner of the content area rather than the padding or border edges.
Restricting Painting to the Content Box
.content-only-background {
padding: 25px;
border: 8px solid #1769aa;
background-image: linear-gradient(135deg, #e8f2fb, #69b7e8);
background-origin: content-box;
background-clip: content-box;
}
Using both properties with content-box positions the gradient relative to the content area and restricts its painting to that area.
This combination is useful for inset backgrounds that should not extend through the element's padding.
Working with background-position
The background-position property determines where a background image is placed within the positioning area selected by background-origin.
.positioned-background {
padding: 30px;
border: 10px solid #1769aa;
background-image: url("icon.png");
background-repeat: no-repeat;
background-origin: content-box;
background-position: right bottom;
}
The image is positioned at the bottom-right corner of the content box.
Using Percentage Positions
.centered-background {
background-image: url("icon.png");
background-repeat: no-repeat;
background-origin: padding-box;
background-position: 50% 50%;
}
The image is centered within the padding box. Percentage positioning aligns the specified point of the image with the corresponding point of the positioning area.
Using Length Values
.offset-background {
background-image: url("icon.png");
background-repeat: no-repeat;
background-origin: content-box;
background-position: 15px 20px;
}
The image is positioned 15 pixels from the left and 20 pixels from the top of the content box.
Changing background-origin changes the reference area for these positions without changing the specified offset values.
Working with background-size
The background-size property determines the dimensions of the background image. Its cover and contain values use the background positioning area selected by background-origin.
Using cover
.cover-background {
padding: 25px;
border: 10px solid #1769aa;
background-image: url("photo.jpg");
background-origin: content-box;
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}
The image scales proportionally until it covers the content box. Portions may extend outside the positioning area, depending on the clipping settings.
Using contain
.contain-background {
padding: 25px;
border: 10px solid #1769aa;
background-image: url("photo.jpg");
background-origin: padding-box;
background-size: contain;
background-position: center;
background-repeat: no-repeat;
}
The image scales proportionally so it fits entirely within the padding box.
Using Explicit Dimensions
.sized-background {
background-image: url("pattern.png");
background-origin: content-box;
background-size: 80px 80px;
background-repeat: no-repeat;
}
Explicit dimensions size the image directly. The origin still determines the area used to position it.
For fixed background images using background-attachment: fixed, the background positioning area is generally the viewport instead, so background-origin has no effect on that layer.
background-origin vs. background-clip
The background-origin and background-clip properties both reference areas of the CSS box model, but they perform different tasks.
| Property | Purpose | Initial Value |
|---|---|---|
background-origin |
Determines the positioning area for background images. | padding-box |
background-clip |
Determines where backgrounds are allowed to be painted. | border-box |
Positioning Within the Content Box
.origin-example {
padding: 25px;
background-image: url("pattern.png");
background-origin: content-box;
}
The image is positioned relative to the content box, but its painting area is not restricted to that box.
Clipping to the Content Box
.clip-example {
padding: 25px;
background-image: url("pattern.png");
background-clip: content-box;
}
The image is clipped to the content box, but its positioning area remains the padding box by default.
Combining Both Properties
.combined-example {
padding: 25px;
background-image: url("pattern.png");
background-origin: content-box;
background-clip: content-box;
}
Both the positioning area and painting area are restricted to the content box.
Use background-origin to control where the image is positioned and background-clip to control where it remains visible.
Multiple Background Layers
When an element has multiple background images, each image layer can use a different origin.
.layered-panel {
padding: 25px;
border: 8px solid #1769aa;
background-image: url("decoration.svg"), linear-gradient(#e8f2fb, #69b7e8);
background-position: right top, center;
background-repeat: no-repeat, no-repeat;
background-origin: content-box, padding-box;
}
The first background image is positioned relative to the content box. The second is positioned relative to the padding box.
Matching Values to Layers
.three-layers {
background-image: url("icon.png"), url("pattern.png"), linear-gradient(#e8f2fb, #fff);
background-origin: content-box, padding-box, border-box;
}
Each origin value corresponds to the background image at the same position in the comma-separated list.
If fewer origin values are specified than background image layers, the browser repeats the list as necessary. Extra values are not used when there are fewer image layers.
Using One Origin for Multiple Layers
.shared-origin {
background-image: url("icon.png"), url("pattern.png");
background-origin: content-box;
}
The single content-box value is repeated for both background image layers.
Background colors do not have an image positioning area. Their painting area is controlled by background-clip.
Using the background Shorthand
The background-origin property can be specified through the background shorthand.
.panel {
background: url("pattern.png") content-box;
}
When a single box keyword is supplied, it sets both background-origin and background-clip to that value.
Specifying Origin and Clipping Separately
.panel {
background: url("pattern.png") content-box padding-box;
}
When two box keywords are supplied, the first specifies background-origin and the second specifies background-clip.
Combining Position and Origin
.panel {
background: url("pattern.png") center / 80px 80px no-repeat content-box;
}
This positions an 80-by-80-pixel image at the center of the content box and clips the background to that box.
Shorthand Reset Behavior
.panel {
background-origin: content-box;
background: url("pattern.png") no-repeat;
}
The final shorthand declaration resets background-origin to its initial value of padding-box.
To preserve the origin, specify it within the shorthand or place the longhand declaration afterward.
.panel {
background: url("pattern.png") no-repeat;
background-origin: content-box;
}
background-origin Example
The following example compares border-box, padding-box, and content-box using identical elements with borders and padding.
Each box contains a small circular background image generated with a CSS radial gradient. The gradient is positioned at the top-left corner of the selected background positioning area.
<div class="background-origin-demo">
<div class="origin-box border-box">
<span>border-box</span>
</div>
<div class="origin-box padding-box">
<span>padding-box</span>
</div>
<div class="origin-box content-box">
<span>content-box</span>
</div>
</div>
.background-origin-demo {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
gap: 15px;
padding: 15px;
background-color: #f0f2f5;
}
.origin-box {
min-height: 170px;
padding: 25px;
box-sizing: border-box;
border: 10px solid rgb(23 105 170 / 45%);
background-color: #e8f2fb;
background-image: radial-gradient(circle, #1769aa 0 65%, transparent 70%);
background-size: 42px 42px;
background-position: top left;
background-repeat: no-repeat;
color: #173b5e;
text-align: center;
}
.origin-box span {
display: block;
margin-top: 50px;
font-weight: bold;
}
.origin-box.border-box {
background-origin: border-box;
}
.origin-box.padding-box {
background-origin: padding-box;
}
.origin-box.content-box {
background-origin: content-box;
}
Only the background-origin value changes between the three boxes.
In the border-box example, the circular image begins at the outer edge of the border box and is partly visible through the semi-transparent border.
In the padding-box example, the image begins immediately inside the border.
In the content-box example, the image begins farther inward, after both the border and padding.
The background color remains unchanged in all three examples because background-origin affects image positioning rather than the background color's painting area.
Important Considerations
Keep the following points in mind when using background-origin:
- Default value: The initial value is
padding-box. - Border positioning: The
border-boxvalue includes the border area in the background positioning area. - Padding positioning: The
padding-boxvalue positions images relative to the content and padding areas. - Content positioning: The
content-boxvalue excludes both padding and borders from the positioning area. - Background clipping: The property does not restrict where images are painted. Use
background-clipfor painting boundaries. - Background position: The selected origin determines the area used to calculate
background-position. - Background size: The positioning area affects how
coverandcontainsize background images. - Fixed backgrounds: The property has no effect on layers using
background-attachment: fixed. - Multiple layers: Comma-separated values can assign different origins to different background images.
- Background colors: The property does not position background colors, which are controlled by the background painting area.
- Shorthand resets: The
backgroundshorthand resets the origin topadding-boxwhen no origin value is specified. - Element layout: Changing the background origin does not alter the element's dimensions, padding, borders, or content position.
- Accessibility: Background images remain decorative and should not be the only way important information is communicated.
Use background-origin when a background image needs to align with a specific area of an element without changing its layout or clipping behavior.
Browser Support
The background-origin property has Baseline Widely available status and is supported in modern versions of Chrome, Edge, Firefox, Safari, and other major browsers.
All three standard values, border-box, padding-box, and content-box, have broad browser support.
Modern browsers do not require vendor prefixes for this property.
Providing a Fallback
.panel {
background-image: url("pattern.png");
background-position: center;
background-repeat: no-repeat;
background-origin: padding-box;
}
@supports (background-origin: content-box) {
.panel {
background-origin: content-box;
}
}
This example uses the widely supported default positioning area and applies content-box positioning in browsers that recognize the value.
Visit Can I Use? to check browser compatibility for background image positioning and related options.
Specifications
The background-origin property is defined in the CSS Backgrounds and Borders Module Level 3 specification.
