background-clip

Baseline 廣泛可用 *

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

* 此特性的某些部分可能存在不同級別的支援。

background-clip CSS 屬性設定元素的背景是否延伸到其邊框盒、內邊距盒或內容盒的下方。

試一試

background-clip: border-box;
background-clip: padding-box;
background-clip: content-box;
background-clip: text;
color: transparent;
text-shadow: none;
<section id="default-example">
  <div id="example-element">This is the content of the element.</div>
</section>
#example-element {
  background-image: url("/shared-assets/images/examples/leopard.jpg");
  color: white;
  text-shadow: 2px 2px black;
  padding: 20px;
  border: 10px dashed #333333;
  font-size: 2em;
  font-weight: bold;
}

背景始終繪製在邊框後面,因此 background-clip: border-box 只有當邊框部分不透明或具有透明或部分不透明區域時才具有視覺效果。此外,如果文字完全或部分不透明,background-clip: text 屬性幾乎沒有視覺效果。

注意:由於根元素具有不同的背景繪製區域,因此在其上指定 background-clip 屬性無效。請參閱“特殊元素的背景。

注意:對於其根元素為 HTML 元素的文件:如果根元素上background-image的計算值為 none 且其background-colortransparent,則使用者代理必須轉而傳播該元素的第一個 HTML <body> 子元素的 background 屬性的計算值。該 <body> 元素的 background 屬性的已用值是其初始值,傳播的值被視為在根元素上指定。建議 HTML 文件的作者為 <body> 元素而非 HTML 元素指定畫布背景。

語法

css
/* Keyword values */
background-clip: border-box;
background-clip: padding-box;
background-clip: content-box;
background-clip: text;
background-clip: border-area;

/* Global values */
background-clip: inherit;
background-clip: initial;
background-clip: revert;
background-clip: revert-layer;
background-clip: unset;

border-box

背景延伸到邊框的外邊緣(但在 z 軸上位於邊框下方)。

padding-box

背景延伸到內邊距的外邊緣。邊框下方不繪製背景。

content-box

背景在內容盒內部繪製(裁剪到內容盒)。

文字

背景在前景文字內部繪製(裁剪到前景文字)。

邊框區域

背景在邊框繪製區域內部繪製(裁剪到邊框繪製區域),考慮了border-widthborder-style,但忽略了border-color引入的任何透明度。

無障礙

使用 background-clip: text 時,請檢查背景顏色與覆蓋在其上的文字顏色之間的對比度是否足夠高,以便視力低下的人能夠閱讀頁面內容。

如果背景圖片未載入,也可能導致文字無法閱讀。新增一個備用background-color以防止這種情況發生,並在沒有圖片的情況下進行測試。

考慮使用帶有@supports的特性查詢來測試 background-clip: text 的支援情況,並在不支援時提供可訪問的替代方案。

正式定義

初始值border-box
應用於所有元素。也適用於 ::first-letter::first-line
繼承性
計算值同指定值
動畫型別一個可重複的列表

正式語法

background-clip = 
<bg-clip>#

<bg-clip> =
<visual-box> |
[ border-area || text ]

<visual-box> =
content-box |
padding-box |
border-box

示例

HTML

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>
<p class="border-area">
  The background is clipped to the area painted by the border.
</p>

CSS

css
p {
  border: 0.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: rgb(0 0 0 / 20%);
}

.border-area {
  background-clip: border-area;
  border-color: transparent;
}

結果

規範

規範
CSS Backgrounds and Borders Module Level 3
# background-clip
CSS 背景模組第 4 級
# background-clip

瀏覽器相容性

另見