background-origin
Definition
The background-origin property in CSS specifies the origin of the background image or background position. It can be used to determine the position of the background image relative to the element's content, padding, or border.
Examples
Setting the origin of the background image to the element's padding box:
.padding {
width: 200px;
height: 200px;
background-image: url('image.jpg');
background-origin: padding-box;
}
Setting the origin of the background image to the element's border box:
.border {
width: 200px;
height: 200px;
background-image: url('image.jpg');
background-origin: border-box;
}
Setting the origin of the background image to the element's content box:
.content {
width: 200px;
height: 200px;
background-image: url('image.jpg');
background-origin: content-box;
}
Values
| Value | Description |
|---|---|
padding-box | The background is painted within the element's padding box. This is the default value. |
border-box | The background is painted within the element's border box. |
content-box | The background is painted within the element's content box. |
Best Practices
- Use
background-originto specify the origin of the background image or background position. - The default value for
background-originispadding-box, which means that the background is painted within the element's padding box. - Use
background-originin combination with other background properties, such asbackground-image,background-color, andbackground-position, to create more complex and customized backgrounds. - Keep in mind that the
background-originproperty only affects the positioning of the background image or gradient, not the size of the background. To specify the size of the background, use thebackground-sizeproperty. - Use the
background-originproperty in conjunction with thebackground-clipproperty to control the appearance of the background image or gradient.
Browser Compatibility
| Chrome | Firefox | Safari | Internet Explorer | Microsoft Edge | Opera |
|---|---|---|---|---|---|
| Yes | Yes | Yes | Yes | Yes | Yes |