@csstools/css-calc

中文文档
3.4.3发布于 2026/10/02MITTypeScriptES Module零依赖近 30 天下载 4.1亿

Solve CSS math expressions

原始使用文档
文档目录4 节
  1. Usage
  2. With string values :
  3. With component values :
  4. Options

CSS Calc for CSS

npm version Build Status Discord

Implemented from : https://drafts.csswg.org/css-values-4/ on 2023-02-17

Usage

Add CSS calc to your project:

npm install @csstools/css-calc @csstools/css-parser-algorithms @csstools/css-tokenizer --save-dev

With string values :

import { calc } from '@csstools/css-calc';

// '20'
console.log(calc('calc(10 * 2)'));

With component values :

import { stringify, tokenizer } from '@csstools/css-tokenizer';
import { parseCommaSeparatedListOfComponentValues } from '@csstools/css-parser-algorithms';
import { calcFromComponentValues } from '@csstools/css-calc';

const t = tokenizer({
    css: 'calc(10 * 2)',
});

const tokens = [];

{
    while (!t.endOfFile()) {
        tokens.push(t.nextToken());
    }

    tokens.push(t.nextToken()); // EOF-token
}

const result = parseCommaSeparatedListOfComponentValues(tokens, {});

// filter or mutate the component values

const calcResult = calcFromComponentValues(result, { precision: 5, toCanonicalUnits: true });

// filter or mutate the component values even further

const calcResultStr = calcResult.map((componentValues) => {
    return componentValues.map((x) => stringify(...x.tokens())).join('');
}).join(',');

// '20'
console.log(calcResultStr);

Options

precision :

The precision option is a number of decimals. The default precision is fairly high. It aims to be high enough to make rounding unnoticeable in the browser.

Values that serialize in scientific notation are left untouched.

You can set it to a lower number to suit your needs.

import { calc } from '@csstools/css-calc';

// '0.3'
console.log(calc('calc(1 / 3)', { precision: 1 }));
// '0.33'
console.log(calc('calc(1 / 3)', { precision: 2 }));
// '1.0000000000000001e-20'
console.log(calc('calc(1e-10 * 1e-10)'));

globals :

Pass global values as a map of key value pairs.

Example : Relative color syntax (lch(from pink calc(l / 2) c h)) exposes color channel information as ident tokens. By passing globals for l, c and h it is possible to solve nested calc()'s.

import { calc } from '@csstools/css-calc';

const globals = new Map([
    ['a', '10px'],
    ['b', '2rem'],
]);

// '20px'
console.log(calc('calc(a * 2)', { globals: globals }));
// '6rem'
console.log(calc('calc(b * 3)', { globals: globals }));

toCanonicalUnits :

By default this package will try to preserve units. The heuristic to do this is very simplistic. We take the first unit we encounter and try to convert other dimensions to that unit.

This better matches what users expect from a CSS dev tool.

If you want to have outputs that are closes to CSS serialized values you can pass toCanonicalUnits: true.

import { calc } from '@csstools/css-calc';

// '20hz'
console.log(calc('calc(0.01khz + 10hz)', { toCanonicalUnits: true }));

// '20hz'
console.log(calc('calc(10hz + 0.01khz)', { toCanonicalUnits: true }));

// '0.02khz' !!!
console.log(calc('calc(0.01khz + 10hz)', { toCanonicalUnits: false }));

// '20hz'
console.log(calc('calc(10hz + 0.01khz)', { toCanonicalUnits: false }));

版本与发布记录

@csstools/css-calc 共发布 36 个版本,首次发布于 2023/02/22;过去 12 个月发布了 11 个版本。

版本发布日期距今
3.4.3latest2026/10/021 个月前
3.4.22026/10/011 个月前
3.4.12026/09/251 个月前
3.4.02026/09/131 个月前
3.3.02026/07/233 个月前
3.2.12026/05/135 个月前

关于 @csstools/css-calc 的常见问题

@csstools/css-calc 是什么?

Solve CSS math expressions

如何安装 @csstools/css-calc?

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

@csstools/css-calc 支持 TypeScript 吗?

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

@csstools/css-calc 支持 ESM 还是 CommonJS?

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

@csstools/css-calc 需要什么版本的 Node.js?

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

@csstools/css-calc 可以商用吗?

@csstools/css-calc 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@csstools/css-calc 的下载量有多少?

根据 npm 官方下载统计,@csstools/css-calc 在 2026/10/07 – 2026/11/05 这 30 天内被下载 405,713,201 次,日均约 13,523,773 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@csstools/css-calc 还在维护吗?

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