π Introduction
The background-origin property controls **the reference box** from which the background image is positioned. It determines whether the background starts at the border edge, padding edge, or content edge. This property helps improve accuracy when aligning background images inside components. π¨β¨
π― What Does background-origin Do?
- Defines the origin (starting point) for background-position
- Works with images and gradients
- Useful for precise design control
- Often used with
background-clip
β¨ Syntax
Syntax
selector {
background-origin: border-box | padding-box | content-box;
}Note
π§© Available Values
1. border-box
The background image is positioned relative to the **outer edge** of the border.
border-box
background-origin: border-box;2. padding-box (default)
The background starts at the **inner edge of the border**, matching padding.
padding-box
background-origin: padding-box;3. content-box
Content area becomes the origin β background starts inside padding.
content-box
background-origin: content-box;π§© How It Interacts with background-clip
background-origin sets where the image is placed.background-clip sets how far the image is allowed to paint.
origin + clip
background-origin: content-box;
background-clip: padding-box;π₯ Practical Examples
1. Image Positioned From Border
Border Origin
.box {
border: 10px solid #000;
background-image: url("pattern.png");
background-origin: border-box;
background-position: top right;
}2. Default Padding-Origin Background
Padding Origin
.card {
padding: 20px;
background-image: url("texture.png");
background-origin: padding-box; /* default */
}3. Content-Based Background Positioning
Content Box Example
.content {
padding: 30px;
background-image: url("shape.png");
background-origin: content-box;
background-position: 0 0;
}4. Sprite Precise Alignment Using content-box
Sprite Alignment
.icon {
background-image: url("sprite.png");
background-origin: content-box;
background-size: auto;
background-position: -40px -80px;
}5. Using Multiple Background Layers
Multiple Origins
.multi {
background-image: url("pattern.png"), url("shape.png");
background-origin: padding-box, border-box;
}π Summary Table
| Value | Background Positioned From | Use Case |
|---|---|---|
| border-box | Outer border edge | Decorative borders, outer alignment |
| padding-box | Inner border edge (default) | General layouts |
| content-box | Content area | Precise alignment, sprite usage |
πΌοΈ Visual Illustration
Further details can be found at: https://dummyimage.com/900x260/eeeeee/000000&text=background-origin+demo
π Conclusion
The background-origin property provides precise control over how background images are positioned within an elementβs box model. Whether you want alignment relative to borders, padding, or content, this property ensures your designs stay consistent and pixel-perfect. Master it alongside background-clip and background-position to elevate your background designs. π