@csstools/postcss-cascade-layers

中文文档
6.0.2发布于 2026/10/06MIT-0TypeScriptES Module近 30 天下载 2819.3万

Use cascade layers in CSS

原始使用文档
文档目录7 节
  1. How it works
  2. Usage
  3. Options
  4. onRevertLayerKeyword
  5. onConditionalRulesChangingLayerOrder
  6. onImportLayerRule
  7. Contributors

PostCSS Cascade Layers PostCSS Logo

npm install @csstools/postcss-cascade-layers --save-dev

PostCSS Cascade Layers lets you use @layer following the Cascade Layers Specification. For more information on layers, checkout A Complete Guide to CSS Cascade Layers by Miriam Suzanne.


target {
    color: purple;
}

@layer {
    target {
        color: green;
    }
}


/* becomes */


target:not(#\#) {
    color: purple;
}

target {
        color: green;
    }

How it works

PostCSS Cascade Layers creates "layers" of specificity.

It applies extra specificity on all your styles based on :

  • the most specific selector found
  • the order in which layers are defined
@layer A, B;

@layer B {
    .a-less-specific-selector {
        /* styles */
    }
}

@layer A {
    #something #very-specific {
        /* styles */
    }
}

@layer C {
    .a-less-specific-selector {
        /* styles */
    }
}

most specific selector :

  • #something #very-specific
  • [2, 0, 0]
  • 2 + 1 -> 3 to ensure there is no overlap

the order in which layers are defined :

  • A
  • B
  • C
layerprevious adjustmentspecificity adjustmentselector
A00 + 0 = 0N/A
B00 + 3 = 3:not(#\#):not(#\#):not(#\#)
C33 + 3 = 6:not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#)

This approach lets more important (later) layers always override less important (earlier) layers.
And layers have enough room internally so that each selector works and overrides as expected.

More layers with more specificity will cause longer :not(...) selectors to be generated.

[!IMPORTANT] PostCSS Cascade Layers assumes to process your complete CSS bundle.
If your build tool processes files individually or processes files in parallel the output will be incorrect.
Using @csstools/postcss-bundler and @import statements is one way to make sure your CSS is bundled before it is processed by this plugin.

Usage

Add PostCSS Cascade Layers to your project:

npm install postcss @csstools/postcss-cascade-layers --save-dev

Use it as a PostCSS plugin:

const postcss = require('postcss');
const postcssCascadeLayers = require('@csstools/postcss-cascade-layers');

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

Options

onRevertLayerKeyword

The onRevertLayerKeyword option enables warnings if revert-layer is used. Transforming revert-layer for older browsers is not possible in this plugin.

Defaults to warn

postcssCascadeLayers({ onRevertLayerKeyword: 'warn' }) // 'warn' | false
/* [postcss-cascade-layers]: handling "revert-layer" is unsupported by this plugin and will cause style differences between browser versions. */
@layer {
    .foo {
        color: revert-layer;
    }
}

onConditionalRulesChangingLayerOrder

The onConditionalRulesChangingLayerOrder option enables warnings if layers are declared in multiple different orders in conditional rules. Transforming these layers correctly for older browsers is not possible in this plugin.

Defaults to warn

postcssCascadeLayers({ onConditionalRulesChangingLayerOrder: 'warn' }) // 'warn' | false
/* [postcss-cascade-layers]: handling different layer orders in conditional rules is unsupported by this plugin and will cause style differences between browser versions. */
@media (min-width: 10px) {
    @layer B {
        .foo {
            color: red;
        }
    }
}

@layer A {
    .foo {
        color: pink;
    }
}

@layer B {
    .foo {
        color: red;
    }
}

onImportLayerRule

The @import at-rule can also be used with cascade layers, specifically to create a new layer like so:

@import 'theme.css' layer(utilities);

If your CSS uses @import with layers, you will also need the postcss-import plugin. This plugin alone will not handle the @import at-rule.

This plugin will warn you when it detects that postcss-import did not transform@import at-rules.

postcssCascadeLayers({ onImportLayerRule: 'warn' }) // 'warn' | false

Contributors

The contributors to this plugin were Olu Niyi-Awosusi and Sana Javed from Oddbird and Romain Menke.

版本与发布记录

@csstools/postcss-cascade-layers 共发布 25 个版本,首次发布于 2022/05/12;过去 12 个月发布了 3 个版本。

版本发布日期距今
6.0.2latest2026/10/061 个月前
6.0.12026/09/251 个月前
6.0.02026/01/149 个月前
5.0.22025/06/261.4 年前
5.0.12024/10/242 年前
5.0.02024/08/042 年前

关于 @csstools/postcss-cascade-layers 的常见问题

@csstools/postcss-cascade-layers 是什么?

Use cascade layers in CSS

如何安装 @csstools/postcss-cascade-layers?

在项目目录运行 npm install @csstools/postcss-cascade-layers,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:postcss(^8.4)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@csstools/postcss-cascade-layers 支持 TypeScript 吗?

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

@csstools/postcss-cascade-layers 支持 ESM 还是 CommonJS?

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

@csstools/postcss-cascade-layers 需要什么版本的 Node.js?

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

@csstools/postcss-cascade-layers 可以商用吗?

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

@csstools/postcss-cascade-layers 的下载量有多少?

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

@csstools/postcss-cascade-layers 还在维护吗?

最新版本 6.0.2 发布于 2026/10/06(1 个月前);至今共发布 25 个版本,其中过去 12 个月发布了 3 个;首次发布于 2022/05/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。