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-origin
to specify the origin of the background image or background position. - The default value for
background-origin
ispadding-box
, which means that the background is painted within the element's padding box. - Use
background-origin
in 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-origin
property 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-size
property. - Use the
background-origin
property in conjunction with thebackground-clip
property 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 |