<color>
Baseline 广泛可用 *
<color> CSS 数据类型表示一种颜色。<color> 还可以包含Alpha 通道的透明度值,表示颜色应如何与其背景混合。
备注:尽管 <color> 值有精确的定义,但它们在不同设备上的实际外观可能会有所不同(有时会非常明显)。这是因为大多数设备没有经过校准,而且一些浏览器不支持输出设备的颜色配置文件。
语法
/* 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> 值可以使用下面列出的方法之一来指定:
- 通过关键字:
<named-color>(例如blue或pink)、<system-color>和currentColor。 - 通过十六进制表示法:
<hex-color>(例如#ff0000)。 - 通过
<color-function>,使用函数表示法在颜色空间中指定参数: - 通过使用相对颜色语法,基于现有颜色输出新颜色。上述任何颜色函数都可以接受一个源颜色,其前有
from关键字,后跟新输出颜色的通道值定义。 - 通过混合两种颜色:
color-mix()。 - 通过指定一种颜色,为其返回一种对比色:
contrast-color()。 - 通过指定两种颜色,第一种用于浅色方案,第二种用于深色方案:
light-dark()。
currentColor 关键字
currentColor 关键字代表元素 color 属性的值。这允许你在默认情况下不接收 color 值的属性上使用该值。
如果 currentColor 被用作 color 属性的值,它将取自 color 属性的继承值。
<div class="container">
The color of this text is blue.
<div class="child"></div>
This block is surrounded by a blue border.
</div>
.container {
color: blue;
border: 1px dashed currentColor;
}
.child {
background: currentColor;
height: 9px;
}
缺失的颜色分量
除了使用旧式逗号分隔语法的颜色函数外,任何 CSS 颜色函数的每个分量都可以指定为关键字 none,表示该分量缺失。
在颜色插值中显式指定缺失的分量对于你希望插值某些颜色分量而不插值其他分量的情况很有用。对于所有其他目的,缺失分量将有效地具有一个适当单位的零值:0、0% 或 0deg。例如,在插值之外使用时,以下颜色是等效的:
/* 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> 来覆盖。
带缺失分量的插值
在同一空间中插值颜色
当插值的颜色正好在插值颜色空间中时,一种颜色中缺失的分量将被另一种颜色中相同分量的现有值替换。例如,以下两个表达式是等效的:
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 作为插值颜色空间,并以下面两种颜色为例:
lch(80% 30 none)
color(display-p3 0.7 0.5 none)
预处理过程是:
-
将两种颜色中缺失的分量替换为零值。
csslch(80% 30 0) color(display-p3 0.7 0.5 0) -
将两种颜色都转换为插值颜色空间。
cssoklch(83.915% 0.0902 0.28) oklch(63.612% 0.1522 78.748) -
如果转换后颜色的任何分量与相应原始颜色中的缺失分量相似,则将其重置为缺失分量。
cssoklch(83.915% 0.0902 none) oklch(63.612% 0.1522 78.748) -
将任何缺失的分量替换为另一个转换后颜色中的相同分量。
cssoklch(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
<div></div>
<hr />
<label for="color">Enter a valid color value:</label>
<input type="text" id="color" />
结果
生成完全饱和的 sRGB 颜色
此示例展示了 sRGB 颜色空间中完全饱和的 sRGB 颜色。
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
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
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
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
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
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 |
浏览器兼容性
加载中…
另见
opacity:在元素级别定义透明度的属性<hue>:表示颜色色相角的数据类型color、background-color、border-color、box-shadow、outline-color、text-shadow:使用<color>的常见属性color()函数- 使用 CSS 将颜色应用于 HTML 元素
- 使用相对颜色
- MDN 博客上的CSS 颜色(第 4 级)中的新函数、渐变和色相(2023)