background-clip
Sets which area of the element the background extends to, including the text background effect.
- Initial value
border-box- Inherited
- No
- Animatable
- no
- Applies to
- all elements
- JavaScript
element.style.backgroundClip = "padding-box"
Example
#.gradient-title {
background: linear-gradient(90deg, #a78bfa, #38bdf8);
background-clip: text;
color: transparent;
}<h2 class="gradient-title" style="margin: 0; font-size: 30px; background: linear-gradient(90deg, #a78bfa, #38bdf8); background-clip: text; -webkit-background-clip: text; color: transparent;">CSS documentation</h2>Definition and usage
#The background-clip property clips the background to the chosen area. border-box (the default) draws the background under the border too, padding-box ends it at the inner edge of the border, and content-box draws it only under the content.
The most striking value is text: the background is visible only in the shape of the letters. Combined with a gradient and a transparent text colour, it gives the popular coloured-heading effect.
The difference between border-box and padding-box is easiest to see with a dashed or semi-transparent border.
Syntax
#background-clip: border-box | padding-box | content-box | text;Values
#- The background extends under the border.
- The background ends at the border.
- The background only under the content, without the padding.
- The background is visible only in the letters. The text must have a transparent colour.
Values in practice
#border-box
.demo { background-clip: border-box; }padding-box
.demo { background-clip: padding-box; }content-box
.demo { background-clip: content-box; }text
.demo { background: linear-gradient(90deg, #f59e0b, #f43f5e, #7c3aed); background-clip: text; color: transparent; }Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- For text with a gradient background also set
color: transparent, otherwise the text colour will cover the background. - Older browsers required
-webkit-background-clip: text. It is worth adding that line alongside if backward compatibility matters to you.
Related entries
#- backgroundA shorthand that sets all background properties in one declaration.
- background-originSets which area of the element the background image position is measured from.
- background-imageSets an image or gradient as an elementโs background.
- colorSets the text colour and the default colour of borders, decorations and SVG icons that use currentcolor.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.