大网数据广告活动
Web 标准与前端开发

color-interpolation

· 52CE 我爱测

The **`color-interpolation`** attribute specifies the color space for gradient interpolations, color animations, and alpha compositing.

The color-interpolation attribute specifies the color space for gradient interpolations, color animations, and alpha compositing.

[!NOTE] For filter effects, the color-interpolation-filters property controls which color space is used.

The color-interpolation property chooses between color operations occurring in the sRGB color space or in a (light energy linear) linearized RGB color space. Having chosen the appropriate color space, component-wise linear interpolation is used.

When a child element is blended into a background, the value of the color-interpolation property on the child determines the type of blending, not the value of the color-interpolation on the parent. For gradients which make use of the href or the deprecated xlink:href attribute to reference another gradient, the gradient uses the property's value from the gradient element which is directly referenced by the fill or stroke property. When animating colors, color interpolation is performed according to the value of the color-interpolation property on the element being animated.

[!NOTE] As a presentation attribute, color-interpolation also has a CSS property counterpart: color-interpolation. When both are specified, the CSS property takes priority.

You can use this attribute with the following SVG elements:

  • a
  • animate
  • circle
  • clipPath
  • defs
  • ellipse
  • foreignObject
  • g
  • image
  • line
  • linearGradient
  • marker
  • mask
  • path
  • pattern
  • polygon
  • polyline
  • radialGradient
  • rect
  • svg
  • switch
  • symbol
  • text
  • textPath
  • tspan
  • use

Example

This example shows four SVGs, each with a rect element and a different gradient used as a fill for the <rect>. The first two SVGs use linearGradient and the second two use radialGradient elements. In unsupported browsers, the gradient looks the same.

In this first SVG, the color-interpolation attribute is not included on the <linearGradient> element, which defaults to sRGB.

svg {
  display: block;
}
<svg width="450" height="70">
  <title>
    Example of linearGradient excluding the color-interpolation attribute
  </title>
  <defs>
    <linearGradient id="gradientDefault">
      <stop offset="0%" stop-color="white" />
      <stop offset="25%" stop-color="blue" />
      <stop offset="50%" stop-color="white" />
      <stop offset="75%" stop-color="red" />
      <stop offset="100%" stop-color="white" />
    </linearGradient>
  </defs>
  <rect
    x="0"
    y="0"
    width="400"
    height="40"
    fill="url(#gradientDefault)"
    stroke="black" />
  <text x="0" y="60" font-family="courier" font-size="16">
    color-interpolation not set
  </text>
</svg>

In this second SVG, the color-interpolation attribute is included on the <linearGradient> element and set to linearRGB.

<svg width="450" height="70">
  <title>
    Example of linearGradient using the color-interpolation attribute
  </title>
  <defs>
    <linearGradient id="gradientLinearRGB" color-interpolation="linearRGB">
      <stop offset="0%" stop-color="white" />
      <stop offset="25%" stop-color="blue" />
      <stop offset="50%" stop-color="white" />
      <stop offset="75%" stop-color="red" />
      <stop offset="100%" stop-color="white" />
    </linearGradient>
  </defs>
  <rect
    x="0"
    y="0"
    width="400"
    height="40"
    fill="url(#gradientLinearRGB)"
    stroke="black" />
  <text x="0" y="60" font-family="courier" font-size="16">
    color-interpolation="linearRGB"
  </text>
</svg>

In this third SVG, the color-interpolation attribute is not included on the <radialGradient> element, which defaults to sRGB.

<svg width="450" height="70">
  <title>
    Example of radialGradient excluding the color-interpolation attribute
  </title>
  <defs>
    <radialGradient id="none">
      <stop offset="0%" stop-color="red" />
      <stop offset="100%" stop-color="gold" />
    </radialGradient>
  </defs>
  <rect x="0" y="0" width="400" height="40" fill="url(#none)" stroke="black" />
  <text x="0" y="60" font-family="courier" font-size="16">
    color-interpolation not set
  </text>
</svg>

In this fourth SVG, the color-interpolation attribute is included on the <radialGradient> element and set to linearRGB.

<svg width="450" height="70">
  <title>
    Example of radialGradient using the color-interpolation attribute
  </title>
  <defs>
    <radialGradient id="radLinearRGB" color-interpolation="linearRGB">
      <stop offset="0%" stop-color="red" />
      <stop offset="100%" stop-color="gold" />
    </radialGradient>
  </defs>
  <rect
    x="0"
    y="0"
    width="400"
    height="40"
    fill="url(#radLinearRGB)"
    stroke="black" />
  <text x="0" y="60" font-family="courier" font-size="16">
    color-interpolation="linearRGB" (SVG attr)
  </text>
</svg>

Example

Usage notes

<table class="properties"> <tbody> <tr> <th scope="row">Value</th> <td><code>auto</code> | <code>sRGB</code> | <code>linearRGB</code></td> </tr> <tr> <th scope="row">Default value</th> <td><code>sRGB</code></td> </tr> <tr> <th scope="row">Animatable</th> <td>discrete</td> </tr> </tbody> </table>

  • auto
    • : Indicates that the user agent can choose either the sRGB or linearRGB spaces for color interpolation. This option indicates that the author doesn't require that color interpolation occur in a particular color space.
  • sRGB
    • : Indicates that color interpolation should occur in the sRGB color space.
  • linearRGB
    • : Indicates that color interpolation should occur in the linearized RGB color space as described in the sRGB specification.

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。