background-origin

Term side of the Card.
Topic: Background
Unit title: background-origin
Categories: Property
Explanation side of the Card

Explanation: The property that defines where the background image or color is positioned within an element. It can be positioned from the 'border-box', 'padding-box' or 'content-box', affecting how the background is aligned relative to the element's boundaries.

Browser compatibility:
  • The chrome browser supports this feature starting from version 1.
  • The edge browser supports this feature starting from version 12.
  • The firefox browser supports this feature starting from version 4.
  • The safari browser supports this feature starting from version 3.
Availability: 

Example

css
.note {
  padding: 24px;
  background: url("pin.svg") no-repeat left top;
  background-origin: content-box;
}

'content-box' positions the image from the content edge, so the pin sits inside the 24px padding, aligned with the text.

Common mistakes

  • Changing background-origin seems to do nothing because the element has no padding or border.
  • The background color still fills the border area, because background-origin only moves images, not where they are painted.

Key points

  • Background images that should start inside the padding use background-origin, not extra margins on the image.
  • Code doesn't rely on background-origin for backgrounds that use background-attachment: fixed, where it is ignored.

Interview questions

What is the default value of background-origin?

The default is padding-box, so images are positioned from the inner edge of the border.

Does background-origin affect background-color?

No. It only affects background images; use background-clip to control where the color is painted.

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy