Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

background-origin: the box used to position an image

Choose the reference box for background images without confusing positioning with clipping.

Version: CSS Modules

Syntax

background-origin: content-box;

background-origin defines the positioning area for each background image. Its initial padding-box starts at the padding edge; border-box includes the border, while content-box starts at the content edge. These choices affect background-position and the area used by percentage background-size. They do not change the element’s content, padding or border dimensions. A comma-separated list follows the background-image layers; shorter lists repeat, and the first image is painted above later images.

Positioning is different from painting. background-clip determines where the background remains visible, so an image can be positioned partly outside its permitted painting area and then be clipped. Changing origin alone does not force the image to stay inside that box. With background-attachment: fixed, background-origin has no effect: the positioning area is the initial containing block. The element’s clipping still matters. A later background shorthand can reset an origin you set earlier.

The example creates four boxes, each with 100×60px of content, 10px padding and a transparent 10px border: their outer size is 140×100px. A non-repeating red 20×20px image at 0 0 starts at outer coordinates 0,0 with border-box, 10,10 with padding-box and 20,20 with content-box. The fourth box keeps padding-box positioning but clips to content-box: only a 10×10px red part beginning at 20,20 remains. Console output reports the actual box size and computed choices; the verification separately checks the painted pixel rectangles.

Examples

const cases = [['border-box','border-box'],['padding-box','border-box'],['content-box','border-box'],['padding-box','content-box']];
cases.forEach(([origin,clip],i)=>{
  const box=document.createElement('div');box.id='origin-'+i;
  box.style.cssText='position:absolute;top:0;width:100px;height:60px;padding:10px;border:10px solid transparent;background-image:linear-gradient(red,red);background-size:20px 20px;background-repeat:no-repeat;background-position:0 0';
  box.style.left=(i*160)+'px';box.style.backgroundOrigin=origin;box.style.backgroundClip=clip;document.body.append(box);
  const s=getComputedStyle(box),r=box.getBoundingClientRect();
  console.log(origin+'/'+clip+': '+r.width+'x'+r.height+'; '+s.backgroundOrigin+'/'+s.backgroundClip);
});

Result

border-box/border-box: 140x100; border-box/border-box
padding-box/border-box: 140x100; padding-box/border-box
content-box/border-box: 140x100; content-box/border-box
padding-box/content-box: 140x100; padding-box/content-box

Executed in Chromium 157.0.8103.51 with local gradient images and exact PNG pixel checks at device pixel ratio 1. The example covers single unfragmented boxes, not fixed backgrounds, rounded clipping or fragmented inline elements.

Sources