The background-clip
CSS property specifies if an element's background, whether a <color>
or an <image>
, extends underneath its border.
/* Keyword values */ background-clip: border-box; background-clip: padding-box; background-clip: content-box; background-clip: text; /* Global values */ background-clip: inherit; background-clip: initial; background-clip: unset;
If the element has no background-image
or background-color
, this property will only have a visual effect when the border has transparent regions or partially opaque regions (due to border-style
or border-image
); otherwise, the border masks the difference.
<div class="grid"> <div class="col"> <div class="cell"> <div class="container border-box">background-clip: border-box;</div> </div> <div class="cell"> <div class="container padding-box">background-clip: padding-box;</div> </div> <div class="cell"> <div class="container content-box">background-clip: content-box;</div> </div> <div class="cell"> <div class="container text">background-clip: text; color: transparent;</div> </div> </div> </div>
html,body { height: 100%; box-sizing: border-box; background: #EEE; } .grid { width: 100%; min-height: 100%; display: flex; font: 1em monospace; } .col { display: flex; flex: 1 auto; flex-direction: column; height: auto; } .cell { margin: .5em; padding: .5em; background-color: #FFF; overflow: hidden; text-align: center; flex: 1 auto; } .container { position: relative; background: linear-gradient(to right, #000 0%, transparent 50%, #FFF 100%), linear-gradient(to bottom, #FF0 0%, #F0F 50%, #0FF 100%); background-size: calc(100% + 1em) calc(100% + 1em); background-position: -.5em -.5em; color: white; padding: 1em; border: .5em double black; font:bold 1em sans-serif; } .border-box { background-clip: border-box; } .padding-box { background-clip: padding-box; } .content-box { background-clip: content-box; } .text { -webkit-background-clip: text; background-clip: text; color: transparent; }
Initial value | border-box |
---|---|
Applies to | all elements. It also applies to ::first-letter and ::first-line . |
Inherited | no |
Media | visual |
Computed value | as specified |
Animation type | discrete |
Canonical order | the unique non-ambiguous order defined by the formal grammar |
Syntax
Values
border-box
- The background extends to the outside edge of the border (but underneath the border in z-ordering).
padding-box
- The background extends to the outside edge of the padding. No background is drawn beneath the border.
content-box
- The background is painted within (clipped to) the content box.
text
- The background is painted within (clipped to) the foreground text.
Formal syntax
<box>#
Examples
HTML
<p class="border-box">The background extends behind the border.</p> <p class="padding-box">The background extends to the inside edge of the border.</p> <p class="content-box">The background extends only to the edge of the content box.</p> <p class="text">The background is clipped to the foreground text.</p>
CSS
p { border: .8em darkviolet; border-style: dotted double; margin: 1em 0; padding: 1.4em; background: linear-gradient(60deg, red, yellow, red, yellow, red); font: 900 1.2em sans-serif; text-decoration: underline; } .border-box { background-clip: border-box; } .padding-box { background-clip: padding-box; } .content-box { background-clip: content-box; } .text { background-clip: text; color: rgba(0,0,0,.2); }
Result
Specifications
Specification | Status | Comment |
---|---|---|
CSS Backgrounds and Borders Module Level 3 The definition of 'background-clip' in that specification. |
Candidate Recommendation | Initial definition. |
CSS Backgrounds and Borders Module Level 4 The definition of 'background-clip' in that specification. |
Editor's Draft | Add text value. |
Browser compatibility
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 11 | 12 | 42 | 93 | 10.5 | 31 |
content-box | 1 | 12 | 42 | 94 | 10.5 | 3 |
text | Yes -webkit- 5 | 15 | 496 | No | Yes -webkit- 5 | Yes -webkit- 5 |
Feature | Android webview | Chrome for Android | Edge mobile | Firefox for Android | IE mobile | Opera Android | iOS Safari |
---|---|---|---|---|---|---|---|
Basic support | 4.1 | Yes | Yes | 14 | 7.1 | 12.1 | Yes |
content-box | 4.1 | Yes | Yes | 14 | 7.1 | 12.1 | Yes |
text | ? | ? | Yes | 496 | No | Yes -webkit- 5 | Yes -webkit- 5 |
1. Webkit also supports the prefixed version of this property, and in that case, in addition to the current keywords, the alternative synonyms are: padding
, border
, and content
.
2. Firefox supported, from version 1 to 3.6 included, a different and prefixed syntax: -moz-background-clip: padding | border
.
3. In IE 7 and IE 8 of Internet Explorer, this property always behaved like background-clip: padding
when overflow
was hidden
, auto
, or scroll
.
4. In IE 7 and IE 9 of Internet Explorer, it always behaved like background-clip: padding
if overflow: hidden | auto | scroll
5. Support the prefixed version of the property only; according to the official blog, WebKit does not include text decorations or shadows in the clipping.
6. In Firefox 48, it was not activated by default and its support could be activated by setting layout.css.background-clip-text.enabled
pref to true
.
See also
- The
clip-path
property creates a clipping region that defines what part of an entire element should be displayed. - Background properties:
background
,background-color
,background-image
- Introduction to the CSS box model