<color>

Baseline 广泛可用 *

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

* 此特性的某些部分可能存在不同级别的支持。

<color> CSS 数据类型表示一种颜色。<color> 还可以包含Alpha 通道透明度值,表示颜色应如何与其背景混合

备注:尽管 <color> 值有精确的定义,但它们在不同设备上的实际外观可能会有所不同(有时会非常明显)。这是因为大多数设备没有经过校准,而且一些浏览器不支持输出设备的颜色配置文件

语法

css
/* Named colors */
rebeccapurple
aliceblue

/* RGB Hexadecimal */
#f09
#ff0099

/* RGB (Red, Green, Blue) */
rgb(255 0 153)
rgb(255 0 153 / 80%)

/* HSL (Hue, Saturation, Lightness) */
hsl(150 30% 60%)
hsl(150 30% 60% / 80%)

/* HWB (Hue, Whiteness, Blackness) */
hwb(12 50% 0%)
hwb(194 0% 0% / 0.5)

/* Lab (Lightness, A-axis, B-axis) */
lab(50% 40 59.5)
lab(50% 40 59.5 / 0.5)

/* LCH (Lightness, Chroma, Hue) */
lch(52.2% 72.2 50)
lch(52.2% 72.2 50 / 0.5)

/* Oklab (Lightness, A-axis, B-axis) */
oklab(59% 0.1 0.1)
oklab(59% 0.1 0.1 / 0.5)

/* OkLCh (Lightness, Chroma, Hue) */
oklch(60% 0.15 50)
oklch(60% 0.15 50 / 0.5)

/* Relative CSS colors */
/* HSL hue change */
hsl(from red 240deg s l)
/* HWB alpha channel change */
hwb(from green h w b / 0.5)
/* LCH lightness change */
lch(from blue calc(l + 20) c h)

/* light-dark */
light-dark(white, black)
light-dark(rgb(255 255 255), rgb(0 0 0))

<color> 值可以使用下面列出的方法之一来指定:

currentColor 关键字

currentColor 关键字代表元素 color 属性的值。这允许你在默认情况下不接收 color 值的属性上使用该值。

如果 currentColor 被用作 color 属性的值,它将取自 color 属性的继承值。

html
<div class="container">
  The color of this text is blue.
  <div class="child"></div>
  This block is surrounded by a blue border.
</div>
css
.container {
  color: blue;
  border: 1px dashed currentColor;
}
.child {
  background: currentColor;
  height: 9px;
}

缺失的颜色分量

除了使用旧式逗号分隔语法的颜色函数外,任何 CSS 颜色函数的每个分量都可以指定为关键字 none,表示该分量缺失。

颜色插值中显式指定缺失的分量对于你希望插值某些颜色分量而不插值其他分量的情况很有用。对于所有其他目的,缺失分量将有效地具有一个适当单位的零值:00%0deg。例如,在插值之外使用时,以下颜色是等效的:

css
/* These are equivalent */
color: oklab(50% none -0.25);
color: oklab(50% 0 -0.25);

/* These are equivalent */
background-color: hsl(none 100% 50%);
background-color: hsl(0deg 100% 50%);

插值

颜色插值发生在渐变过渡动画中。

在插值 <color> 值时,它们首先被转换到给定的颜色空间,然后计算值的每个分量进行线性插值,插值的速度由过渡和动画中的缓动函数决定。插值颜色空间默认为 Oklab,但可以通过某些颜色相关的函数表示法中的 <color-interpolation-method> 来覆盖。

带缺失分量的插值

在同一空间中插值颜色

当插值的颜色正好在插值颜色空间中时,一种颜色中缺失的分量将被另一种颜色中相同分量的现有值替换。例如,以下两个表达式是等效的:

css
color-mix(in oklch, oklch(none 0.2 10), oklch(60% none 30))
color-mix(in oklch, oklch(60% 0.2 10), oklch(60% 0.2 30))

备注:如果一个分量在两种颜色中都缺失,那么该分量在插值后也将缺失。

插值不同空间的颜色:相似分量

如果要插值的任何颜色不在插值颜色空间中,其缺失的分量将根据下表中描述的同一类别的相似分量转移到转换后的颜色中:

类别 相似分量
红色 R, X
绿色 G, Y
蓝色 B, Z
亮度 L
色彩度 C, S
色相 H
a a
b b

例如

  • color(xyz 0.2 0.1 0.6) 中的 X (0.2) 类似于 rgb(50% 70% 30%) 中的 R (50%)。
  • hsl(0deg 100% 80%) 中的 H (0deg) 类似于 oklch(80% 0.1 140) 中的 H (140)。

以 OkLCh 作为插值颜色空间,并以下面两种颜色为例:

css
lch(80% 30 none)
color(display-p3 0.7 0.5 none)

预处理过程是:

  1. 将两种颜色中缺失的分量替换为零值。

    css
    lch(80% 30 0)
    color(display-p3 0.7 0.5 0)
    
  2. 将两种颜色都转换为插值颜色空间。

    css
    oklch(83.915% 0.0902 0.28)
    oklch(63.612% 0.1522 78.748)
    
  3. 如果转换后颜色的任何分量与相应原始颜色中的缺失分量相似,则将其重置为缺失分量。

    css
    oklch(83.915% 0.0902 none)
    oklch(63.612% 0.1522 78.748)
    
  4. 将任何缺失的分量替换为另一个转换后颜色中的相同分量。

    css
    oklch(83.915% 0.0902 78.748)
    oklch(63.612% 0.1522 78.748)
    

无障碍

有些人难以区分颜色。WCAG 2.2 建议强烈反对仅使用颜色来传达特定消息、操作或结果。更多信息请参见颜色与颜色对比度

正式语法

<color> = 
<color-base> |
currentColor |
<system-color> |
<contrast-color()> |
<device-cmyk()> |
<light-dark()>

<color-base> =
<hex-color> |
<color-function> |
<named-color> |
<color-mix()> |
transparent

<contrast-color()> =
contrast-color( [ [ <color> && [ tbd-fg | tbd-bg ] && <target-contrast>? ] | [ <color> && [ tbd-fg | tbd-bg ] && <target-contrast> , <color># ] ] )

<device-cmyk()> =
<legacy-device-cmyk-syntax> |
<modern-device-cmyk-syntax>

<light-dark()> =
light-dark( <color> , <color> )

<color-function> =
<rgb()> |
<rgba()> |
<hsl()> |
<hsla()> |
<hwb()> |
<lab()> |
<lch()> |
<oklab()> |
<oklch()> |
<ictcp()> |
<jzazbz()> |
<jzczhz()> |
<alpha()> |
<color()>

<color-mix()> =
color-mix( <color-interpolation-method>? , [ <color> && <percentage [0,100]>? ]# )

<target-contrast> =
<wcag2>

<legacy-device-cmyk-syntax> =
device-cmyk( <number>#{4} )

<modern-device-cmyk-syntax> =
device-cmyk( <cmyk-component>{4} [ / [ <alpha-value> | none ] ]? )

<rgb()> =
<legacy-rgb-syntax> |
<modern-rgb-syntax>

<rgba()> =
<legacy-rgba-syntax> |
<modern-rgba-syntax>

<hsl()> =
<legacy-hsl-syntax> |
<modern-hsl-syntax>

<hsla()> =
<legacy-hsla-syntax> |
<modern-hsla-syntax>

<hwb()> =
hwb( [ from <color> ]? [ <hue> | none ] [ <percentage> | <number> | none ] [ <percentage> | <number> | none ] [ / [ <alpha-value> | none ] ]? )

<lab()> =
lab( [ from <color> ]? [ <percentage> | <number> | none ] [ <percentage> | <number> | none ] [ <percentage> | <number> | none ] [ / [ <alpha-value> | none ] ]? )

<lch()> =
lch( [ from <color> ]? [ <percentage> | <number> | none ] [ <percentage> | <number> | none ] [ <hue> | none ] [ / [ <alpha-value> | none ] ]? )

<oklab()> =
oklab( [ from <color> ]? [ <percentage> | <number> | none ] [ <percentage> | <number> | none ] [ <percentage> | <number> | none ] [ / [ <alpha-value> | none ] ]? )

<oklch()> =
oklch( [ from <color> ]? [ <percentage> | <number> | none ] [ <percentage> | <number> | none ] [ <hue> | none ] [ / [ <alpha-value> | none ] ]? )

<ictcp()> =
ictcp( [ from <color> ]? [ <percentage> | <number> | none ] [ <percentage> | <number> | none ] [ <percentage> | <number> | none ] [ / [ <alpha-value> | none ] ]? )

<jzazbz()> =
jzazbz( [ from <color> ]? [ <percentage> | <number> | none ] [ <percentage> | <number> | none ] [ <percentage> | <number> | none ] [ / [ <alpha-value> | none ] ]? )

<jzczhz()> =
jzczhz( [ from <color> ]? [ <percentage> | <number> | none ] [ <percentage> | <number> | none ] [ <hue> | none ] [ / [ <alpha-value> | none ] ]? )

<alpha()> =
alpha( [ from <color> ] [ / [ <alpha-value> | none ] ]? )

<color()> =
color( [ from <color> ]? <colorspace-params> [ / [ <alpha-value> | none ] ]? )

<color-interpolation-method> =
in [ <rectangular-color-space> | <polar-color-space> <hue-interpolation-method>? | <custom-color-space> ]

<wcag2> =
wcag2 |
wcag2( [ <number> | [ aa | aaa ] && large? ] )

<cmyk-component> =
<number> |
<percentage> |
none

<alpha-value> =
<number> |
<percentage>

<legacy-rgb-syntax> =
rgb( <percentage>#{3} , <alpha-value>? ) |
rgb( <number>#{3} , <alpha-value>? )

<modern-rgb-syntax> =
rgb( [ from <color> ]? [ <number> | <percentage> | none ]{3} [ / [ <alpha-value> | none ] ]? )

<legacy-rgba-syntax> =
rgba( <percentage>#{3} , <alpha-value>? ) |
rgba( <number>#{3} , <alpha-value>? )

<modern-rgba-syntax> =
rgba( [ from <color> ]? [ <number> | <percentage> | none ]{3} [ / [ <alpha-value> | none ] ]? )

<legacy-hsl-syntax> =
hsl( <hue> , <percentage> , <percentage> , <alpha-value>? )

<modern-hsl-syntax> =
hsl( [ from <color> ]? [ <hue> | none ] [ <percentage> | <number> | none ] [ <percentage> | <number> | none ] [ / [ <alpha-value> | none ] ]? )

<legacy-hsla-syntax> =
hsla( <hue> , <percentage> , <percentage> , <alpha-value>? )

<modern-hsla-syntax> =
hsla( [ from <color> ]? [ <hue> | none ] [ <percentage> | <number> | none ] [ <percentage> | <number> | none ] [ / [ <alpha-value> | none ] ]? )

<hue> =
<number> |
<angle>

<colorspace-params> =
<custom-params> |
<predefined-rgb-params> |
<xyz-params>

<rectangular-color-space> =
srgb |
srgb-linear |
display-p3 |
display-p3-linear |
a98-rgb |
prophoto-rgb |
rec2020 |
lab |
oklab |
<xyz-space>

<polar-color-space> =
hsl |
hwb |
lch |
oklch

<hue-interpolation-method> =
[ shorter | longer | increasing | decreasing ] hue

<custom-color-space> =
<dashed-ident>

<custom-params> =
<dashed-ident> [ <number> | <percentage> | none ]+

<predefined-rgb-params> =
<predefined-rgb> [ <number> | <percentage> | none ]{3}

<xyz-params> =
<xyz-space> [ <number> | <percentage> | none ]{3}

<xyz-space> =
xyz |
xyz-d50 |
xyz-d65

<predefined-rgb> =
srgb |
srgb-linear |
display-p3 |
display-p3-linear |
a98-rgb |
prophoto-rgb |
rec2020 |
rec2100-pq |
rec2100-hlg |
rec2100-linear

示例

探索颜色值

在此示例中,我们提供了一个 <div> 和一个文本输入框。在输入框中输入一个有效的颜色值,<div> 就会采用该颜色,从而让你测试我们的颜色值。

HTML

html
<div></div>
<hr />
<label for="color">Enter a valid color value:</label>
<input type="text" id="color" />

结果

生成完全饱和的 sRGB 颜色

此示例展示了 sRGB 颜色空间中完全饱和的 sRGB 颜色。

HTML

html
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>

CSS

css
div:nth-child(1) {
  background-color: hsl(0 100% 50%);
}
div:nth-child(2) {
  background-color: hsl(30 100% 50%);
}
div:nth-child(3) {
  background-color: hsl(60 100% 50%);
}
div:nth-child(4) {
  background-color: hsl(90 100% 50%);
}
div:nth-child(5) {
  background-color: hsl(120 100% 50%);
}
div:nth-child(6) {
  background-color: hsl(150 100% 50%);
}
div:nth-child(7) {
  background-color: hsl(180 100% 50%);
}
div:nth-child(8) {
  background-color: hsl(210 100% 50%);
}
div:nth-child(9) {
  background-color: hsl(240 100% 50%);
}
div:nth-child(10) {
  background-color: hsl(270 100% 50%);
}
div:nth-child(11) {
  background-color: hsl(300 100% 50%);
}
div:nth-child(12) {
  background-color: hsl(330 100% 50%);
}

结果

创建不同色调的红色

此示例展示了 sRGB 颜色空间中不同色调的红色。

HTML

html
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>

CSS

css
div:nth-child(1) {
  background-color: hsl(0 100% 0%);
}
div:nth-child(2) {
  background-color: hsl(0 100% 20%);
}
div:nth-child(3) {
  background-color: hsl(0 100% 40%);
}
div:nth-child(4) {
  background-color: hsl(0 100% 60%);
}
div:nth-child(5) {
  background-color: hsl(0 100% 80%);
}
div:nth-child(6) {
  background-color: hsl(0 100% 100%);
  border: solid;
}

结果

创建不同饱和度的红色

此示例展示了 sRGB 颜色空间中不同饱和度的红色。

HTML

html
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>

CSS

css
div:nth-child(1) {
  background-color: hsl(0 0% 50%);
}
div:nth-child(2) {
  background-color: hsl(0 20% 50%);
}
div:nth-child(3) {
  background-color: hsl(0 40% 50%);
}
div:nth-child(4) {
  background-color: hsl(0 60% 50%);
}
div:nth-child(5) {
  background-color: hsl(0 80% 50%);
}
div:nth-child(6) {
  background-color: hsl(0 100% 50%);
}

结果

规范

规范
CSS 颜色模块第四版
# color-syntax

浏览器兼容性

另见