The background-origin property in CSS determines the positioning area of the background image. It specifies where the background image is positioned in relation to the element's border, padding, or content box.
The background-origin property can take several values. Here are the common values you can use:
Value | Description | Example |
---|---|---|
border-box | The background image starts from the upper-left corner of the border box. |
Background Origin: Border Box - The background image or color will extend to the outer edge of the border, making it appear as part of the border.
|
padding-box | The background image starts from the upper-left corner of the padding box. |
Background Origin: Padding Box - The background image or color will start from the outer edge of the padding, excluding the border, making the background appear within the padding area.
|
content-box | The background image starts from the upper-left corner of the content box. |
Background Origin: Content Box - The background image or color will only extend to the outer edge of the content, not including the padding or border, making it appear only within the content area.
|
Here's how you can apply different background origin values to your elements using CSS:
Example: Border Box
.border-box {
background-origin: border-box;
background-color:green;
background-image: url('/images/common/cutecat100x128.png');
border: 45px dotted darkred;
padding: 80px;
margin: 10px;
height: 550px;
width: 550px;
background-repeat: no-repeat;
}
This will render the background image starting from the upper-left corner of the border box.
.padding-box {
background-origin: padding-box;
background-color:green;
background-image: url('/images/common/cutecat100x128.png');
border: 45px dotted darkred;
padding: 80px;
margin: 10px;
height: 550px;
width: 550px;
background-repeat: no-repeat;
}
This will render the background image starting from the upper-left corner of the padding box.
.content-box {
background-origin: content-box;
background-color:green;
background-image: url('/images/common/cutecat100x128.png');
border: 45px dotted darkred;
padding: 80px;
margin: 10px;
height: 550px;
width: 550px;
background-repeat: no-repeat;
}
This will render the background image starting from the upper-left corner of the content box.
Here are some important notes and best practices when using the background-origin property:
Here is a visual comparison of different background-origin values to help you understand their impact: