--- title: background-clip slug: Web/CSS/background-clip tags: - CSS - CSS Background - CSS Property - Clip - Reference translation_of: Web/CSS/background-clip ---
background-clip
设置元素的背景(背景图片或颜色)是否延伸到边框、内边距盒子、内容盒子下面。
此交互样例的源代码被储存在GitHub库中.如果你想要对此交互样例工程有代码贡献,请点击 https://github.com/mdn/interactive-examples并向我们发送拉取请求
如果没有设置背景图片({{cssxref("background-image")}})或背景颜色({{cssxref("background-color")}}),那么这个属性只有在边框( {{cssxref("border")}})被设置为非固实(soild)、透明或半透明时才能看到视觉效果(与 {{cssxref("border-style")}} 或 {{cssxref("border-image")}} 有关),否则,本属性产生的样式变化会被边框覆盖。
/* 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;
border-box
padding-box
content-box
text
{{experimental_inline}}<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>
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; -webkit-background-clip: text; color: rgba(0,0,0,.2); }
{{EmbedLiveSample('Examples', 600, 580)}}
规范 | 状态 | 备注 |
---|---|---|
{{SpecName('CSS3 Backgrounds', '#the-background-clip', 'background-clip')}} | {{Spec2('CSS3 Backgrounds')}} | Initial definition. |
{{SpecName('CSS4 Backgrounds', '#background-clip', 'background-clip')}} | {{Spec2('CSS4 Backgrounds')}} | Add text value. |
{{cssinfo}}
{{Compat("css.properties.background-clip")}}