CSS
π― What is background-origin?
The background-origin property defines the positioning area of a background image.
In simpler terms, it determines whether the background image starts from:
- the content area
- the padding area
- or the border area
π§© Simple Definition
background-origin = defines where a background image begins
π§© Basic Syntax
background-origin: value;
βοΈ Accepted Values
| Value | Description |
|---|---|
| padding-box | Background starts inside the padding area |
| border-box | Background starts from the border area |
| content-box | Background starts only inside the content area |
| initial | Resets to default value |
| inherit | Inherits value from parent element |
π§ Default Value
background-origin: padding-box;
π This is the default behavior.
The background image starts from the padding area.
π§© Understanding the CSS Box Model
An HTML element is made up of three main areas:
- 1. content-box β contains the actual content
- 2. padding-box β space around the content
- 3. border-box β outer border area
The background-origin property determines which of these areas the background image uses as its starting point.
π§© Visual Representation
+-------------------------------+ β border-box
| Padding Area |
| +-----------------------+ |
| | Content Area | | β content-box
| +-----------------------+ |
+-------------------------------+
πΉ padding-box
background-origin: padding-box;
π The background image starts inside the padding area.
The image does NOT extend beneath the border.
π§© Example
div {
background-image: url("bg.jpg");
background-origin: padding-box;
}
πΉ border-box
background-origin: border-box;
π The background image starts from the outer border edge.
This means the border becomes part of the background area.
π§© Example
div {
background-image: url("bg.jpg");
background-origin: border-box;
}
πΉ content-box
background-origin: content-box;
π The background image appears only inside the content area.
It does not extend into the padding area.
π§© Example
div {
background-image: url("bg.jpg");
background-origin: content-box;
}
βοΈ Difference Between Values
| Value | Starting Area | Behavior |
|---|---|---|
| padding-box | Padding area | Default behavior |
| border-box | Border area | Includes border in background |
| content-box | Content area only | Most restricted area |
π§© Practical Example
.box {
border: 10px solid black;
padding: 20px;
background-image: url("pattern.png");
background-origin: border-box;
}
π§ Explanation
border-box
The background image extends beneath the border area.
padding
Creates inner spacing around the content.
border
Defines the outer edge of the element.
π¨ Common Usage
background-origin is commonly used together with:
| Property | Purpose |
|---|---|
| background-image | Defines the image |
| background-size | Controls image size |
| background-position | Controls image placement |
| background-clip | Defines where the background is visible |
β οΈ Important Note
The background-origin property only affects the starting area of the background image.
It does NOT control:
- image size
- clipping area
- repetition
Those behaviors are controlled by other background properties.
