CSS background-clip Property
The background-clip property controls how far an element's background color or image extends within its box.
It determines whether the background is painted beneath the border, inside the padding area, within the content area, or through the shapes of text. The property is useful for controlling background boundaries, creating transparent borders, and applying gradient effects to text.
background-clip Syntax
selector {
background-clip: padding-box;
}
The property accepts keywords that identify the area in which the background is painted.
.border-background {
background-clip: border-box;
}
.padding-background {
background-clip: padding-box;
}
.content-background {
background-clip: content-box;
}
.text-background {
background-clip: text;
}
.border-area-background {
background-clip: border-area;
}
Multiple clipping values can be separated by commas when an element has multiple background image layers.
.layered-background {
background-clip: content-box, padding-box;
}
The property also accepts CSS-wide values such as inherit, initial, revert, revert-layer, and unset.
Property Values
| Value | Description |
|---|---|
border-box |
Extends the background beneath the border to the outer edge of the border box. This is the initial value. |
padding-box |
Clips the background to the outer edge of the padding box, excluding the border area. |
content-box |
Clips the background to the content box, excluding the padding and border areas. |
text |
Clips the background to the shapes of the element's foreground text. |
border-area |
Clips the background to the area occupied by the border, taking border width and style into account but not border-color transparency. This newer value has limited browser support. |
The property applies to all elements, is not inherited, and has an initial value of border-box. Its computed value is a list of clipping keywords, and its animation type is discrete.
The border-box, padding-box, and content-box values correspond to the main areas of the CSS box model.
The text and border-area values provide additional clipping effects that do not simply follow one of those rectangular box areas.
How background-clip Works
Every CSS element is represented by a box that includes content, padding, and borders. The background-clip property determines which portions of that box can display the background.
Consider an element with a colored background, padding, and a semi-transparent border.
.panel {
padding: 25px;
border: 12px solid rgb(0 0 0 / 25%);
background-color: #69b7e8;
background-clip: border-box;
}
With border-box, the blue background extends beneath the semi-transparent border.
Changing the clipping value to padding-box prevents the blue background from being painted beneath the border.
.panel {
background-clip: padding-box;
}
Changing it to content-box restricts the background to the content area, leaving the padding and border areas without that background.
Clipping Does Not Change Box Dimensions
The property affects background painting only. It does not change the element's width, height, padding, border thickness, or layout position.
It also does not clip the element's foreground content. To control content clipping, use properties such as overflow or clip-path where appropriate.
Using border-box
The border-box value is the default. It allows the background to extend beneath the element's border.
.border-box-panel {
padding: 20px;
border: 10px solid rgb(23 105 170 / 40%);
background: #e8f2fb;
background-clip: border-box;
}
Because the border is partially transparent, the background color can be visible through it.
If the border is fully opaque, the background beneath the border is covered and may not be noticeable.
Rounded Borders
.rounded-panel {
padding: 20px;
border: 8px solid rgb(23 105 170 / 40%);
border-radius: 16px;
background: #e8f2fb;
background-clip: border-box;
}
The background follows the outer rounded border shape. The clipping boundary is adjusted to account for the element's border radius.
Use border-box when the background should fill the entire border box, including the area behind the border.
Using padding-box
The padding-box value clips the background to the padding box, preventing it from extending beneath the border.
.padding-box-panel {
padding: 20px;
border: 10px solid rgb(23 105 170 / 40%);
background: #e8f2fb;
background-clip: padding-box;
}
The background fills the content and padding areas but stops at the inner edge of the border.
Creating a Translucent Border
.transparent-border {
padding: 20px;
border: 10px solid rgb(23 105 170 / 30%);
border-radius: 12px;
background-color: #fff;
background-clip: padding-box;
}
The white background does not appear beneath the translucent border, allowing the surrounding page background to show through the border area.
This technique is useful for creating subtle borders around cards, panels, and other interface elements.
Using content-box
The content-box value restricts background painting to the element's content box.
.content-box-panel {
padding: 25px;
border: 8px solid #1769aa;
background: #69b7e8;
background-clip: content-box;
}
The blue background is painted behind the content area but not in the padding or border areas.
Creating Space Around a Background
.inset-background {
padding: 20px;
background-color: #d8ebfa;
background-clip: content-box;
}
The padding creates transparent space between the background and the element's outer edges.
This can create an inset background effect without adding another wrapper element.
Remember that the element's content remains in its normal position. Only the painted background area changes.
Using text
The text value clips the background to the shapes of the element's text.
It is commonly used to create gradient-filled lettering and other decorative text effects.
.gradient-text {
background: linear-gradient(90deg, #1769aa, #8e44ad);
background-clip: text;
color: transparent;
}
The gradient is painted only through the text shapes. The transparent text color allows the background to remain visible.
Including the WebKit Prefix
.gradient-heading {
background: linear-gradient(90deg, #1769aa, #8e44ad);
-webkit-background-clip: text;
background-clip: text;
-webkit-text-fill-color: transparent;
}
The prefixed declaration provides compatibility with older WebKit-based browsers. The -webkit-text-fill-color declaration allows the clipped gradient to appear through the lettering.
Providing a Readable Fallback
.gradient-heading {
color: #1769aa;
}
@supports (background-clip: text) or (-webkit-background-clip: text) {
.gradient-heading {
background: linear-gradient(90deg, #1769aa, #8e44ad);
-webkit-background-clip: text;
background-clip: text;
-webkit-text-fill-color: transparent;
}
}
Without clipping support, the heading retains a readable solid text color.
Gradient text is best suited to short headings and decorative labels. For longer text, solid colors are generally easier to read.
When using background-clip: text, verify that the chosen colors provide sufficient contrast against the surrounding background.
Using border-area
The border-area value clips the background to the area occupied by the element's border.
Unlike border-box, it does not paint the background throughout the content and padding areas.
.gradient-border {
padding: 20px;
border: 8px solid transparent;
border-radius: 12px;
background: linear-gradient(135deg, #1769aa, #8e44ad);
background-clip: border-area;
}
The gradient is clipped to the border area, creating a decorative gradient border.
The border's width and style determine the clipping region, but the transparency of the border color is not used to calculate that region.
Browser Compatibility
The border-area value is a newer CSS feature and does not have the same broad browser support as border-box, padding-box, and content-box.
For broader compatibility, a gradient border can also be created with two background layers.
.gradient-border-fallback {
padding: 20px;
border: 8px solid transparent;
border-radius: 12px;
background: linear-gradient(#fff, #fff) padding-box, linear-gradient(135deg, #1769aa, #8e44ad) border-box;
}
The first background layer fills the padding box with white. The second layer fills the border box with a gradient, which remains visible through the transparent border.
This layered technique is useful when a gradient border is needed without relying on the newer border-area value.
background-clip vs. background-origin
The background-clip and background-origin properties both work with the CSS box model, but they control different aspects of background rendering.
| Property | Purpose |
|---|---|
background-clip |
Determines where the background is allowed to be painted. |
background-origin |
Determines the positioning area used to place background images. |
Using background-clip
.clip-example {
background-image: url("pattern.png");
background-clip: content-box;
}
The image is painted only within the content box.
Using background-origin
.origin-example {
background-image: url("pattern.png");
background-origin: content-box;
}
The image is positioned relative to the content box, but the background may still be painted outside that area according to background-clip.
Using Both Properties
.combined-example {
padding: 20px;
background-image: url("pattern.png");
background-position: center;
background-repeat: no-repeat;
background-origin: content-box;
background-clip: padding-box;
}
The image is positioned relative to the content box, while the background painting area extends through the padding box.
Use background-origin to control positioning and background-clip to control painting boundaries.
Multiple Background Layers
When an element has multiple background images, each layer can use a different clipping value.
.layered-panel {
padding: 20px;
border: 8px solid transparent;
background-image: linear-gradient(#fff, #fff), linear-gradient(135deg, #1769aa, #8e44ad);
background-clip: padding-box, border-box;
}
The first gradient is clipped to the padding box, while the second extends through the border box.
Because the first image is painted above the second, the outer gradient remains visible around the white inner background.
Matching Clipping Values to Layers
.three-backgrounds {
background-image: url("foreground.png"), url("pattern.png"), linear-gradient(#e8f2fb, #fff);
background-clip: content-box, padding-box, border-box;
}
Each clipping value corresponds to the background image at the same position in the comma-separated list.
If fewer clipping values are provided than background image layers, the browser repeats the list as necessary. Extra values are not used when there are fewer layers.
Background colors are painted beneath all image layers and use the clipping area of the final background layer.
Using the background Shorthand
The background-clip property can be specified as part of the background shorthand.
.panel {
background: #e8f2fb padding-box;
}
When one box keyword is included in the shorthand, it sets both background-origin and background-clip to that value.
Specifying Different Values
.panel {
background: url("pattern.png") content-box padding-box;
}
When two box keywords are supplied, the first sets background-origin and the second sets background-clip.
Shorthand Reset Behavior
.panel {
background-clip: content-box;
background: #e8f2fb;
}
The final background declaration resets background-clip to its initial value of border-box.
To preserve the clipping value, specify it after the shorthand.
.panel {
background: #e8f2fb;
background-clip: content-box;
}
Using the longhand property is often clearer when changing only the clipping area without modifying other background settings.
background-clip Example
The following example compares border-box, padding-box, and content-box using identical background colors, padding, and semi-transparent borders.
The different clipping values determine which parts of each box display the blue background.
<div class="clip-demo">
<div class="clip-box border-box">
<span>border-box</span>
</div>
<div class="clip-box padding-box">
<span>padding-box</span>
</div>
<div class="clip-box content-box">
<span>content-box</span>
</div>
</div>
.clip-demo {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
gap: 15px;
padding: 15px;
background-color: #f0f2f5;
}
.clip-box {
min-height: 140px;
padding: 20px;
box-sizing: border-box;
border: 12px solid rgb(23 105 170 / 35%);
background-color: #69b7e8;
color: #173b5e;
text-align: center;
}
.clip-box span {
display: block;
padding: 8px 0;
font-weight: bold;
}
.clip-box.border-box {
background-clip: border-box;
}
.clip-box.padding-box {
background-clip: padding-box;
}
.clip-box.content-box {
background-clip: content-box;
}
All three boxes use the same dimensions and styling. Only the background-clip value changes.
With border-box, the blue background extends beneath the translucent border.
With padding-box, the background stops at the inner border edge but continues through the padding area.
With content-box, the background is restricted to the content area, leaving the padding transparent.
The gray demonstration background makes the transparent areas easier to distinguish.
Important Considerations
Keep the following points in mind when using background-clip:
- Default value: The initial value is
border-box, allowing the background to extend beneath the border. - Padding clipping: The
padding-boxvalue excludes the border area from background painting. - Content clipping: The
content-boxvalue excludes both padding and border areas. - Text clipping: The
textvalue can create gradient-filled text when combined with transparent text fill. - Border-area clipping: The newer
border-areavalue restricts the background to the border region but has limited browser support. - Rounded corners: Box-based clipping boundaries account for
border-radius. - Background positioning: Use
background-originto control image positioning independently of clipping. - Multiple layers: Different background layers can have different clipping values.
- Shorthand resets: The
backgroundshorthand resets clipping toborder-boxwhen no clipping value is specified. - Element layout: Background clipping does not change element dimensions or remove content from the layout.
- Accessibility: Decorative text effects must maintain sufficient contrast and a readable fallback.
- Root elements: Background painting on the root element follows special rules and may not behave like ordinary element backgrounds.
Use background-clip when the background should be restricted to a specific box area or decorative shape without changing the element's layout.
Browser Support
The background-clip property has Baseline Widely available support for its established box-based values, including border-box, padding-box, and content-box.
The text value is supported by modern browsers, although older WebKit-based browsers may require the -webkit-background-clip prefix.
The newer border-area value has more limited availability and should be used with a compatible fallback.
Text Clipping Compatibility
.gradient-text {
color: #1769aa;
}
@supports (background-clip: text) or (-webkit-background-clip: text) {
.gradient-text {
background: linear-gradient(90deg, #1769aa, #8e44ad);
-webkit-background-clip: text;
background-clip: text;
-webkit-text-fill-color: transparent;
}
}
The heading uses a solid text color when background clipping is unavailable.
Visit Can I Use? to check support for background clipping options. Search separately for newer values such as border-area.
Specifications
The background-clip property is defined in the CSS Backgrounds and Borders specifications, with newer clipping values described in subsequent levels.
