PostCSS Calc lets you reduce calc() references whenever it's possible.
When multiple units are mixed together in the same expression, the calc()
statement is left as is, to fallback to the W3C calc() implementation.
npm install postcss-calc// dependencies
var fs = require("fs")
var postcss = require("postcss")
var calc = require("postcss-calc")
// css to be processed
var css = fs.readFileSync("input.css", "utf8")
// process css
var output = postcss()
.use(calc())
.process(css)
.cssUsing this input.css:
h1 {
font-size: calc(16px * 2);
height: calc(100px - 2em);
width: calc(2*var(--base-width));
margin-bottom: calc(16px * 1.5);
}you will get:
h1 {
font-size: 32px;
height: calc(100px - 2em);
width: calc(2*var(--base-width));
margin-bottom: 24px
}Checkout tests for more examples.
Allow you to define the precision for decimal numbers.
var out = postcss()
.use(calc({precision: 10}))
.process(css)
.cssAllow you to preserve calc() usage in output so browsers will handle decimal precision themselves.
var out = postcss()
.use(calc({preserve: true}))
.process(css)
.cssAdds warnings when calc() are not reduced to a single value.
var out = postcss()
.use(calc({warnWhenCannotResolve: true}))
.process(css)
.cssAllows calc() usage as part of media query declarations.
var out = postcss()
.use(calc({mediaQueries: true}))
.process(css)
.cssAllows calc() usage as part of selectors.
var out = postcss()
.use(calc({selectors: true}))
.process(css)
.cssExample:
div[data-size="calc(3*3)"] {
width: 100px;
}Callback invoked when a calc() body fails to parse or simplify. Matches
@csstools/css-calc's shape:
calc({
onParseError: (err, input) => {
throw err; // or log, route to a different channel, etc.
}
})When omitted, errors are reported via PostCSS result.warn() so the
plugin never throws at the postcss level.
The legacy jison-generated parser was replaced by a hand-written Pratt parser whose simplifier follows CSS Values 4. Most inputs reduce to identical output; the differences are spec-aligned or canonical-form decisions:
- Strict whitespace (§10.1).
calc(2px+3px)is invalid CSS (binary+/-require surrounding whitespace) and is preserved with a warning instead of reduced. - Canonical operand order. Commutative operands serialize
numeric-first, matching
@csstools/css-calc:calc(var(--foo) + 10px)→calc(10px + var(--foo)). - Zero buckets are kept.
calc(100px - (100px - 100%))→calc(0px + 100%), not100%— WPT calc-serialization-002 requires the zero term because it carries the length-percentage type. - Constant folding.
calc(43 + pi)now folds to46.14159(§10.7.1). Previouslypi/estayed symbolic. - Reciprocal conversion.
calc(var(--x) / 2)becomescalc(var(--x) * 0.5). The two are mathematically equivalent; previously the division shape was kept. - Distributive multiplication.
calc(0.5 * (100vw - 10px))becomescalc(50vw - 5px). - Unit case normalization.
2PXbecomes2px(CSS units are case- insensitive; lowercase is conventional). - Calc unwrap (§10.6).
calc(var(--foo))becomesvar(--foo)— acalc()containing a single value is replaced by that value. - Spec-style spaced operators.
2px*var(--x)is serialized as2px * var(--x). The tokenizer is unaffected; only output spacing differs. - Division by zero / by a unit.
calc(500px/0)reduces tocalc(infinity * 1px)(§10.13) instead of throwing. UseonParseErrorif you want validation behavior.
To replace the value of CSS custom properties at build time, try PostCSS Custom Properties.
Work on a branch, install dev-dependencies, respect coding style & run tests before submitting a bug fix or a feature.
git clone git@github.com:postcss/postcss-calc.git
git checkout -b patch-1
npm install
npm test