CSS
π― What is background-clip?
The background-clip property defines the visible area of an elementβs background.
In simpler terms, it controls which parts of the CSS box model display the background.
π§© Simple Definition
background-clip = controls how far the background extends
π§© Basic Syntax
background-clip: value;
βοΈ Accepted Values
| Value | Description |
|---|---|
| border-box | Background extends beneath the border |
| padding-box | Background appears only inside padding and content |
| content-box | Background appears only inside content |
| initial | Resets to default value |
| inherit | Inherits value from parent element |
π§ Default Value
background-clip: border-box;
π This is the default browser behavior.
The background covers:
- content
- padding
- border areas
π§© Understanding the CSS Box Model
An HTML element consists of three main areas:
- 1. content-box β actual content area
- 2. padding-box β space around the content
- 1. border-box β outer border area
The background-clip property determines where the background is visible within these areas.
π‘ Visual Representation
+--------------------------------+ β border-box
| Padding Area |
| +------------------------+ |
| | Content Area | | β content-box
| +------------------------+ |
+--------------------------------+
πΉ border-box
background-clip: border-box;
π The background extends beneath the border.
The background is visible in:
- β content area
- β padding area
- β border area
Example
div {
background-color: lightblue;
border: 5px solid blue;
background-clip: border-box;
}
πΉ padding-box
background-clip: padding-box;
π The background does NOT appear beneath the border.
The background is visible only in:
- β content area
- β padding area
- β border area
Example
div {
background-color: lightblue;
border: 5px solid blue;
padding: 20px;
background-clip: padding-box;
}
πΉ content-box
background-clip: content-box;
π The background appears only inside the content area.
The background does NOT appear in:
- β padding area
- β border area
Example
div {
background-color: lightblue;
border: 5px solid blue;
padding: 20px;
background-clip: content-box;
}
π§ Explanation
In this example:
- the content area has a light blue background
- the padding remains transparent
- the border remains transparent
βοΈ Difference Between Values
| Value | Background Visibility |
|---|---|
| border-box | Content + Padding + Border |
| padding-box | Content + Padding |
| content-box | Content only |
π¨ Common Usage
background-clip is commonly used for:
- advanced UI styling
- transparent border effects
- gradient designs
- glassmorphism effects
- custom component styling
π§© Practical Example
.card {
background: rgba(255,255,255,0.7);
border: 10px solid rgba(255,255,255,0.3);
background-clip: padding-box;
}
π This technique is often used in modern glass-style UI designs.
β οΈ Important Note
The background-clip property only controls where the background is visible.
It does NOT control:
- image size
- image position
- background repetition
Those are handled by other background properties.
πΉ scroll (Default Value)
background-attachment: scroll;
π The background image moves together with the page content.
This is the default browser behavior.
π§© Example
div {
background-image: url("background.jpg");
background-attachment: scroll;
}
π Result
When the user scrolls the page, the background image also moves.
πΉ fixed
background-attachment: fixed;
π The background image stays fixed in one position.
Even when the page scrolls, the image does not move.
π§© Example
body {
background-image: url("wallpaper.jpg");
background-attachment: fixed;
}
π Result
The background behaves like wallpaper pinned to the screen.
This effect is commonly used in:
- parallax scrolling
- hero sections
- modern landing pages
πΉ local
background-attachment: local;
π The background scrolls together with the elementβs own content.
This is especially useful for scrollable containers.
π§© Example
div {
background-image: url("background.jpg");
background-attachment: local;
overflow: scroll;
}
π Result
The background moves only when the content inside the element is scrolled.
βοΈ Difference Between scroll, fixed, and local
| Value | Behavior |
|---|---|
| scroll | Background moves with the page |
| fixed | Background stays fixed |
| local | Background moves with the elementβs content |
π§© Practical Example
.hero {
background-image: url("hero.jpg");
background-size: cover;
background-position: center;
background-repeat: no-repeat;
background-attachment: fixed;
}
π§ Explanation
background-size: cover
Makes the image fill the entire area.
background-position: center
Centers the image.
background-repeat: no-repeat
Prevents repetition.
background-attachment: fixed
Keeps the image fixed during scrolling.
π¨ Common Use Cases
The background-attachment property is commonly used for:
- fullscreen backgrounds
- parallax effects
- hero sections
- modern landing pages
- scrollable containers
β οΈ Important Note
Using background-attachment: fixed; may affect performance on some mobile devices and older browsers.
For responsive websites, always test scrolling performance across devices.
