<gradient>

Baseline 广泛可用 *

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

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

<gradient> CSS 数据类型 是一种特殊类型的 <image>,由两种或多种颜色之间的渐进式过渡组成。

试一试

background: linear-gradient(#f69d3c, #3f87a6);
background: radial-gradient(#f69d3c, #3f87a6);
background: repeating-linear-gradient(#f69d3c, #3f87a6 50px);
background: repeating-radial-gradient(#f69d3c, #3f87a6 50px);
background: conic-gradient(#f69d3c, #3f87a6);
<section class="display-block" id="default-example">
  <div id="example-element"></div>
</section>
#example-element {
  min-height: 100%;
}

CSS 渐变没有固有尺寸;也就是说,它没有自然或首选尺寸,也没有首选比例。它的具体尺寸将与它所应用的元素的尺寸相匹配。

语法

<gradient> 数据类型由下面列出的函数类型之一定义。

线性渐变

线性渐变沿一条假想线逐步过渡颜色。它们通过 linear-gradient() 函数生成。

径向渐变

径向渐变从中心点(原点)逐步过渡颜色。它们通过 radial-gradient() 函数生成。

锥形渐变

锥形渐变围绕圆形逐步过渡颜色。它们通过 conic-gradient() 函数生成。

重复渐变

重复渐变根据需要复制渐变以填充给定区域。它们通过 repeating-linear-gradient()repeating-radial-gradient()repeating-conic-gradient() 函数生成。

插值

与任何涉及颜色的插值一样,渐变是在预乘 alpha 颜色空间中计算的。这可以防止当颜色和不透明度都发生变化时出现意想不到的灰色阴影。(请注意,旧版浏览器在使用透明关键字时可能不使用此行为。)

正式语法

<gradient> = 
<linear-gradient()> |
<repeating-linear-gradient()> |
<radial-gradient()> |
<repeating-radial-gradient()> |
<conic-gradient()> |
<repeating-conic-gradient()>

<linear-gradient()> =
linear-gradient( [ <linear-gradient-syntax> ] )

<repeating-linear-gradient()> =
repeating-linear-gradient( [ <linear-gradient-syntax> ] )

<radial-gradient()> =
radial-gradient( [ <radial-gradient-syntax> ] )

<repeating-radial-gradient()> =
repeating-radial-gradient( [ <radial-gradient-syntax> ] )

<conic-gradient()> =
conic-gradient( [ <conic-gradient-syntax> ] )

<repeating-conic-gradient()> =
repeating-conic-gradient( [ <conic-gradient-syntax> ] )

<linear-gradient-syntax> =
[ [ <angle> | <zero> | to <side-or-corner> ] || <color-interpolation-method> ]? , <color-stop-list>

<radial-gradient-syntax> =
[ [ [ <radial-shape> || <radial-size> ]? [ at <position> ]? ] || <color-interpolation-method> ]? , <color-stop-list>

<conic-gradient-syntax> =
[ [ [ from [ <angle> | <zero> ] ]? [ at <position> ]? ] || <color-interpolation-method> ]? , <angular-color-stop-list>

<side-or-corner> =
[ left | right ] ||
[ top | bottom ]

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

<color-stop-list> =
<linear-color-stop> , [ <linear-color-hint>? , <linear-color-stop> ]#?

<radial-shape> =
circle |
ellipse

<radial-size> =
<radial-extent> |
<length [0,∞]> |
<length-percentage [0,∞]>{2}

<position> =
<position-one> |
<position-two> |
<position-four>

<angular-color-stop-list> =
<angular-color-stop> , [ <angular-color-hint>? , <angular-color-stop> ]#?

<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>

<linear-color-stop> =
<color> <color-stop-length>?

<linear-color-hint> =
<length-percentage>

<radial-extent> =
closest-corner |
closest-side |
farthest-corner |
farthest-side

<length-percentage> =
<length> |
<percentage>

<position-one> =
left |
center |
right |
top |
bottom |
x-start |
x-end |
y-start |
y-end |
block-start |
block-end |
inline-start |
inline-end |
<length-percentage>

<position-two> =
[ left | center | right | x-start | x-end ] && [ top | center | bottom | y-start | y-end ] |
[ left | center | right | x-start | x-end | <length-percentage> ] [ top | center | bottom | y-start | y-end | <length-percentage> ] |
[ block-start | center | block-end ] && [ inline-start | center | inline-end ] |
[ start | center | end ]{2}

<position-four> =
[ [ left | right | x-start | x-end ] <length-percentage> ] && [ [ top | bottom | y-start | y-end ] <length-percentage> ] |
[ [ block-start | block-end ] <length-percentage> ] && [ [ inline-start | inline-end ] <length-percentage> ] |
[ [ start | end ] <length-percentage> ]{2}

<angular-color-stop> =
<color> <color-stop-angle>?

<angular-color-hint> =
<angle-percentage> |
<zero>

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

<color-stop-length> =
<length-percentage>{1,2}

<color-stop-angle> =
[ <angle-percentage> | <zero> ]{1,2}

<angle-percentage> =
<angle> |
<percentage>

示例

线性渐变示例

一个线性渐变。

css
.linear-gradient {
  background: linear-gradient(
    to right,
    red,
    orange,
    yellow,
    green,
    blue,
    indigo,
    violet
  );
}

径向渐变示例

一个径向渐变。

css
.radial-gradient {
  background: radial-gradient(red, yellow, dodgerblue);
}

锥形渐变示例

一个锥形渐变示例。

css
.conic-gradient {
  background: conic-gradient(pink, coral, lime);
}

重复渐变示例

重复的线性渐变和径向渐变示例。

css
.linear-repeat {
  background: repeating-linear-gradient(
    to top left,
    pink,
    pink 5px,
    white 5px,
    white 10px
  );
}

.radial-repeat {
  background: repeating-radial-gradient(
    lime,
    lime 15px,
    white 15px,
    white 30px
  );
}

.conic-repeat {
  background: repeating-conic-gradient(lime, pink 30deg);
}

规范

规范
CSS 图像模块第 4 级
# 渐变

浏览器兼容性

另见