# @csstools/postcss-gradients-interpolation-method 中文文档

规范地址：https://npmcn.com/package/%40csstools/postcss-gradients-interpolation-method/

版本：6\.0\.16
npm 元数据更新时间：2026-10-29T17:05:55.565Z
中文译文更新时间：2026-11-11T03:27:24.336Z
软件包许可：MIT-0

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40csstools/postcss-gradients-interpolation-method
原文对照：https://npmcn.com/package/%40csstools/postcss-gradients-interpolation-method/?lang=original
项目主页：https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-gradients-interpolation-method#readme
源码仓库：https://github.com/csstools/postcss-plugins

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：14.9 kB，6 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- Node.js 要求：\>=20\.19\.0
- 运行依赖：5 个
- peer 依赖：postcss ^8\.4
- 首次发布：2022-03-04T21:14:36.197Z
- 最新版本发布：2026-10-29T17:05:55.372Z
- 近 30 天下载量：14972730（2026-10-11 至 2026-11-09，npm 官方下载统计）
- 已发布版本数：62（近 12 个月 17 个）

## 中文简介

CSS 梯度函数中使用插值方法

## 安装

```sh
npm install @csstools/postcss-gradients-interpolation-method
```

也可以使用：`pnpm add @csstools/postcss-gradients-interpolation-method`、`yarn add @csstools/postcss-gradients-interpolation-method`、`bun add @csstools/postcss-gradients-interpolation-method`

## README

# 后CSS 梯度内嵌法 [![PostCSS Logo](https://postcss.github.io/postcss/logo.svg)](https://github.com/postcss/postcss)

`npm install @csstools/postcss-gradients-interpolation-method --save-dev`

[后CSS 梯度内嵌法](https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-gradients-interpolation-method) 允许您在 CSS 梯度函数中使用不同的插值方法 [CSS 图像单元 4](https://drafts.csswg.org/css-images-4/#linear-gradients)。 。 。 .

```css
.example {
    background-image: linear-gradient(in oklch, hsl(0deg 85% 75%) 0%, hsl(180deg 80% 65%) 100%);
}

:root {
    --background-image: linear-gradient(in oklab, hsl(96, 42%, 24%) 0%, hsl(302, 67%, 25%) 100%);
}

/* becomes */

.example {
    background-image: linear-gradient(rgb(245, 137, 137) 0%, rgb(248, 146, 114), rgb(244, 158, 94), rgb(235, 171, 82), rgb(220, 185, 81), rgb(201, 199, 95), rgb(177, 211, 118), rgb(151, 221, 146), rgb(125, 229, 177), rgb(103, 235, 208), rgb(94, 237, 237) 100%);
    background-image: linear-gradient(in oklch, hsl(0deg 85% 75%) 0%, hsl(180deg 80% 65%) 100%);
}

:root {
    --background-image: linear-gradient(rgb(56, 87, 35) 0%, rgb(64, 83, 46), rgb(70, 79, 54), rgb(76, 74, 62), rgb(82, 69, 68), rgb(86, 64, 75), rgb(91, 58, 81), rgb(95, 51, 87), rgb(99, 44, 93), rgb(103, 34, 98), rgb(106, 21, 104) 100%);
}

@supports (background: linear-gradient(in oklch, red 0%, red 0% 1%, red 2%)) {
:root {
    --background-image: linear-gradient(in oklab, hsl(96, 42%, 24%) 0%, hsl(302, 67%, 25%) 100%);
}
}
```

## 缺点

> \[叹息\] 不支持只用颜色或插入提示的颜色停止 .

为了取得最佳效果,您应该至少为每个颜色站提供颜色和位置. 双位颜色站被支持 .

```css
.foo {
    /* Only a color: can't transform */
    background-image: linear-gradient(in oklch, black 0%, green, blue 100%);

    /* Only an interpolation hint: can't transform */
    background-image: linear-gradient(in oklch, black 0%, 25%, blue 100%);
}
```

> \[叹息\] 不支持可变颜色 . 当颜色是变量时, 我们无法混合颜色 .

```css
.foo {
    --red: red;
    /* Color stop variable : can't transform */
    background-image: linear-gradient(in oklch, black 0%, var(--red), blue 100%);
}
```

## 使用量

添加 [后CSS 梯度内嵌法](https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-gradients-interpolation-method) 对您的项目:

```bash
npm install postcss @csstools/postcss-gradients-interpolation-method --save-dev
```

当作一个 [邮政](https://github.com/postcss/postcss) 插件 :

```js
const postcss = require('postcss');
const postcssGradientsInterpolationMethod = require('@csstools/postcss-gradients-interpolation-method');

postcss([
    postcssGradientsInterpolationMethod(/* pluginOptions */)
]).process(YOUR_CSS /*, processOptions */);
```

## 选项

### 保护

这个 `preserve` 选项决定原始标记是否 被保存下来。 默认保存.

```js
postcssGradientsInterpolationMethod({ preserve: false })
```

```css
.example {
    background-image: linear-gradient(in oklch, hsl(0deg 85% 75%) 0%, hsl(180deg 80% 65%) 100%);
}

:root {
    --background-image: linear-gradient(in oklab, hsl(96, 42%, 24%) 0%, hsl(302, 67%, 25%) 100%);
}

/* becomes */

.example {
    background-image: linear-gradient(rgb(245, 137, 137) 0%, rgb(248, 146, 114), rgb(244, 158, 94), rgb(235, 171, 82), rgb(220, 185, 81), rgb(201, 199, 95), rgb(177, 211, 118), rgb(151, 221, 146), rgb(125, 229, 177), rgb(103, 235, 208), rgb(94, 237, 237) 100%);
}

:root {
    --background-image: linear-gradient(rgb(56, 87, 35) 0%, rgb(64, 83, 46), rgb(70, 79, 54), rgb(76, 74, 62), rgb(82, 69, 68), rgb(86, 64, 75), rgb(91, 58, 81), rgb(95, 51, 87), rgb(99, 44, 93), rgb(103, 34, 98), rgb(106, 21, 104) 100%);
}
```

### 启用渐进自定义收益

这个 `enableProgressiveCustomProperties` 选项决定原始标记是否 包裹着 `@supports` 当使用自定义属性时。 默认情况下, 它被启用 .

> \[注\] 我们只建议在您准备好的时候取消这个 `preserve` 改为 `false` 或者你带了自定义属性的修补.  
> 查看插件在其中的用途 [读取](https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-progressive-custom-properties#readme)。 。 。 .

```js
postcssGradientsInterpolationMethod({ enableProgressiveCustomProperties: false })
```

```css
.example {
    background-image: linear-gradient(in oklch, hsl(0deg 85% 75%) 0%, hsl(180deg 80% 65%) 100%);
}

:root {
    --background-image: linear-gradient(in oklab, hsl(96, 42%, 24%) 0%, hsl(302, 67%, 25%) 100%);
}

/* becomes */

.example {
    background-image: linear-gradient(rgb(245, 137, 137) 0%, rgb(248, 146, 114), rgb(244, 158, 94), rgb(235, 171, 82), rgb(220, 185, 81), rgb(201, 199, 95), rgb(177, 211, 118), rgb(151, 221, 146), rgb(125, 229, 177), rgb(103, 235, 208), rgb(94, 237, 237) 100%);
    background-image: linear-gradient(in oklch, hsl(0deg 85% 75%) 0%, hsl(180deg 80% 65%) 100%);
}

:root {
    --background-image: linear-gradient(rgb(56, 87, 35) 0%, rgb(64, 83, 46), rgb(70, 79, 54), rgb(76, 74, 62), rgb(82, 69, 68), rgb(86, 64, 75), rgb(91, 58, 81), rgb(95, 51, 87), rgb(99, 44, 93), rgb(103, 34, 98), rgb(106, 21, 104) 100%);
    --background-image: linear-gradient(in oklab, hsl(96, 42%, 24%) 0%, hsl(302, 67%, 25%) 100%);
}
```

_自定义属性不回落到上一个声明_

## 常见问题

### @csstools/postcss-gradients-interpolation-method 是什么？

CSS 梯度函数中使用插值方法

### 如何安装 @csstools/postcss-gradients-interpolation-method？

在项目目录运行 npm install @csstools/postcss-gradients-interpolation-method，或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖：postcss（^8\.4）。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖，Yarn 需要手动安装。

### @csstools/postcss-gradients-interpolation-method 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @csstools/postcss-gradients-interpolation-method 支持 ESM 还是 CommonJS？

以 ES Module 形式发布，推荐使用 import 引入。在 CommonJS 项目中，Node\.js 20\.19\+ / 22\.12\+ 可以直接 require\(\) 不含顶层 await 的 ES Module，更早的版本需要改用动态 import\(\)。

### @csstools/postcss-gradients-interpolation-method 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js \>=20\.19\.0。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### @csstools/postcss-gradients-interpolation-method 可以商用吗？

@csstools/postcss-gradients-interpolation-method 使用 MIT-0 许可。声明的许可为 MIT-0，请阅读许可原文确认使用条件。以上为概括，不构成法律意见，请以许可原文为准。

### @csstools/postcss-gradients-interpolation-method 的下载量有多少？

根据 npm 官方下载统计，@csstools/postcss-gradients-interpolation-method 在 2026/10/11 – 2026/11/09 这 30 天内被下载 14,972,730 次，日均约 499,091 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @csstools/postcss-gradients-interpolation-method 还在维护吗？

最新版本 6\.0\.16 发布于 2026/10/30（12 天前）；至今共发布 62 个版本，其中过去 12 个月发布了 17 个；首次发布于 2022/03/05。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

