@csstools/postcss-is-pseudo-class

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

A pseudo-class for matching elements in a selector list

原始使用文档
文档目录9 节
  1. Usage
  2. Options
  3. preserve
  4. specificityMatchingName
  5. onComplexSelector
  6. onPseudoElement
  7. ⚠️ Known shortcomings
  8. Specificity
  9. Complex selectors

PostCSS Is Pseudo PostCSS

NPM Version Build Status Discord

Baseline Status CSS Standard Status

PostCSS Is Pseudo Class lets you use the :is pseudo class function, following the CSS Selector specification.

:is(input, button):is(:hover, :focus) {
    order: 1;
}

Becomes :

input:hover {
    order: 1;
}
input:focus {
    order: 1;
}
button:hover {
    order: 1;
}
button:focus {
    order: 1;
}

Usage

Add PostCSS Is Pseudo Class to your project:

npm install @csstools/postcss-is-pseudo-class --save-dev

Use PostCSS Is Pseudo Class as a PostCSS plugin:

import postcss from 'postcss';
import postcssIsPseudoClass from '@csstools/postcss-is-pseudo-class';

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

PostCSS Is Pseudo Class runs in all Node environments, with special instructions for:

NodeWebpackGulpGrunt

Options

preserve

The preserve option determines whether the original notation is preserved. By default, it is not preserved.

postcss([
  postcssIsPseudoClass({ preserve: true })
]).process(YOUR_CSS /*, processOptions */);
:is(input, button):is(:hover, :focus) {
    order: 1;
}

Becomes :

input:hover {
    order: 1;
}
input:focus {
    order: 1;
}
button:hover {
    order: 1;
}
button:focus {
    order: 1;
}
:is(input, button):is(:hover, :focus) {
    order: 1;
}

specificityMatchingName

The specificityMatchingName option allows you to change the selector used to adjust specificity. The default value is does-not-exist. If this is an actual class, id or tag name in your code, you will need to set a different option here.

See how :not is used to modify specificity.

postcss([
  postcssIsPseudoClass({ specificityMatchingName: 'something-random' })
]).process(YOUR_CSS /*, processOptions */);
:is(.button, button):hover {
    order: 7;
}

Becomes :

.button:hover {
    order: 7;
}

button:not(.something-random):hover {
    order: 7;
}

onComplexSelector

Warn on complex selectors in :is pseudo class functions.

postcss([
  postcssIsPseudoClass({ onComplexSelector: 'warning' })
]).process(YOUR_CSS /*, processOptions */);

onPseudoElement

Warn when pseudo elements are used in :is pseudo class functions.

⚠️ Pseudo elements are always invalid and will be transformed to ::-csstools-invalid-<pseudo-name>.

postcss([
  postcssIsPseudoClass({ onPseudoElement: 'warning' })
]).process(YOUR_CSS /*, processOptions */);
:is(::after):hover {
    order: 1.0;
}

/* becomes */

::-csstools-invalid-after:hover {
    order: 1.0;
}

⚠️ Known shortcomings

Specificity

:is takes the specificity of the most specific list item. We can increase specificity with :not selectors, but we can't decrease it.

Converted selectors are ensured to have the same specificity as :is for the most important bit. Less important bits can have higher specificity that :is.

Before :

specificity: 0, 2, 0

:is(:hover, :focus):is(.button, button) {
    order: 7;
}

After :

/* specificity: [0, 2, 0] */
.button:hover {
    order: 7;
}

/* specificity: [0, 2, 1] */
/* last bit is higher than it should be, but middle bit matches */
button:not(.does-not-exist):hover {
    order: 7;
}

/* specificity: [0, 2, 0] */
.button:focus {
    order: 7;
}

/* specificity: [0, 2, 1] */
/* last bit is higher than it should be, but middle bit matches */
button:not(.does-not-exist):focus {
    order: 7;
}

Complex selectors

Before :

:is(.alpha > .beta) ~ :is(:focus > .beta) {
    order: 2;
}

After :

.alpha > .beta ~ :focus > .beta {
    order: 2;
}

this is a different selector than expected as .beta ~ :focus matches .beta followed by :focus.
avoid these cases.
writing the selector without :is() is advised here

/* without is */
.alpha:focus > .beta ~ .beta {
    order: 2;
}

If you have a specific pattern you can open an issue to discuss it. We can detect and transform some cases but can't generalize them into a single solution that tackles all of them.

版本与发布记录

@csstools/postcss-is-pseudo-class 共发布 33 个版本,首次发布于 2022/01/14;过去 12 个月发布了 4 个版本。

版本发布日期距今
6.0.3latest2026/10/061 个月前
6.0.22026/10/021 个月前
6.0.12026/09/251 个月前
6.0.02026/01/149 个月前
5.0.32025/06/111.4 年前
5.0.22025/06/101.4 年前

关于 @csstools/postcss-is-pseudo-class 的常见问题

@csstools/postcss-is-pseudo-class 是什么?

A pseudo-class for matching elements in a selector list

如何安装 @csstools/postcss-is-pseudo-class?

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

@csstools/postcss-is-pseudo-class 支持 TypeScript 吗?

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

@csstools/postcss-is-pseudo-class 支持 ESM 还是 CommonJS?

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

@csstools/postcss-is-pseudo-class 需要什么版本的 Node.js?

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

@csstools/postcss-is-pseudo-class 可以商用吗?

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

@csstools/postcss-is-pseudo-class 的下载量有多少?

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

@csstools/postcss-is-pseudo-class 还在维护吗?

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