override-colors

Baseline 已廣泛支援

此功能已成熟,可跨多種裝置和瀏覽器版本使用。自 ⁨2022 年 11 月⁩起,所有瀏覽器均支援此功能。

override-colors CSS 描述符用於覆蓋所選base-palette中彩色字型的顏色。

語法

css
/* basic syntax */
override-colors: <index of color> <color>;

/* using color names */
override-colors: 0 red;

/* using hex-color */
override-colors: 0 #ff0000;

/* using rgb */
override-colors: 0 rgb(255 0 0);

/* overriding multiple colors */
override-colors:
  0 red,
  1 green,
  2 blue;

override-colors 描述符接受一個由顏色索引和新顏色值組成的逗號分隔列表。

顏色索引是零基的,並且可以使用任何顏色值

對於每個索引和顏色的鍵值對,指定base-palette中具有該索引的顏色將被覆蓋。如果彩色字型在指定索引處沒有顏色,則會被忽略。

[ <integer [0,∞]> <absolute-color-base> ]

指定base-palette中顏色的索引以及要覆蓋它的顏色。

正式定義

相關的 at-rule@font-palette-values
初始值n/a(必需)
計算值同指定值

正式語法

override-colors = 
[ <integer [0,∞]> <color> ]#

示例

更改表情符號的顏色

此示例演示如何覆蓋 Noto Color Emoji 彩色字型中的顏色,以匹配您網站的品牌。

HTML

html
<section class="hats">
  <div class="hat">
    <h2>Original Hat</h2>
    <div class="emoji">🎩</div>
  </div>
  <div class="hat">
    <h2>Red Hat</h2>
    <div class="emoji red-hat">🎩</div>
  </div>
</section>

CSS

css
@font-face {
  font-family: "Noto Color Emoji";
  font-style: normal;
  font-weight: normal;
  src: url("https://fonts.gstatic.com/l/font?kit=Yq6P-KqIXTD0t4D9z1ESnKM3-HpFabts6diywYkdG3gjD0U&skey=a373f7129eaba270&v=v24")
    format("woff2");
}

.emoji {
  font-family: "Noto Color Emoji", emoji;
  font-size: 3rem;
}
@font-palette-values --red {
  font-family: "Noto Color Emoji";
  override-colors:
    0 rgb(74 11 0),
    1 rgb(149 22 1),
    2 rgb(183 27 1),
    3 rgb(193 28 1),
    4 rgb(230 34 1);
}
.red-hat {
  font-palette: --red;
}

結果

更改備用基調色盤中的顏色

使用 Rocher Color Font,此示例演示如何覆蓋字型中的一種顏色。

HTML

html
<h2 class="normal-palette">Normal Palette</h2>
<h2 class="override-palette">Override Palette</h2>

CSS

css
@font-face {
  font-family: "Rocher";
  src: url("[path-to-font]/RocherColorGX.woff2") format("woff2");
}
h2 {
  font-family: "Rocher", fantasy;
}
@font-palette-values --override-palette {
  font-family: "Rocher";
  base-palette: 3;
}
@font-palette-values --override-palette {
  font-family: "Rocher";
  base-palette: 3;
  override-colors: 0 rebeccapurple;
}
.normal-palette {
  font-palette: --normal-palette;
}
.override-palette {
  font-palette: --override-palette;
}

結果

此示例顯示,在 base-palette 3 中,索引 0 處的顏色被 rebeccapurple 覆蓋。

Example showing base-palette and base-palette with 1 color overridden

規範

規範
CSS 字型模組第 4 級
# override-color

瀏覽器相容性

另見