Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

postcss-calc

postcss33.7mMIT10.1.0TypeScript support: included

PostCSS plugin to reduce calc()

css, postcss, postcss-plugin, calculation, calc

readme

PostCSS Calc PostCSS

NPM Version Support Chat

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.

Installation

npm install postcss-calc

Usage

// 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)
  .css

Using 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.

Options

precision (default: 5)

Allow you to define the precision for decimal numbers.

var out = postcss()
  .use(calc({precision: 10}))
  .process(css)
  .css

preserve (default: false)

Allow you to preserve calc() usage in output so browsers will handle decimal precision themselves.

var out = postcss()
  .use(calc({preserve: true}))
  .process(css)
  .css

warnWhenCannotResolve (default: false)

Adds warnings when calc() are not reduced to a single value.

var out = postcss()
  .use(calc({warnWhenCannotResolve: true}))
  .process(css)
  .css

mediaQueries (default: false)

Allows calc() usage as part of media query declarations.

var out = postcss()
  .use(calc({mediaQueries: true}))
  .process(css)
  .css

selectors (default: false)

Allows calc() usage as part of selectors.

var out = postcss()
  .use(calc({selectors: true}))
  .process(css)
  .css

Example:

div[data-size="calc(3*3)"] {
  width: 100px;
}

To replace the value of CSS custom properties at build time, try PostCSS Custom Properties.

Contributing

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

Changelog

License

changelog

Changelog

All notable changes to this project will be documented in this file. See commit-and-tag-version for commit guidelines.

10.1.0 (2025-01-07)

Features

  • add support for lh & rlh units (9b6d7a1)

Bug Fixes

10.0.2 (2024-08-16)

Bug Fixes

  • add support for svh, lvh, dvh, svw, lvw, dvw, svmin, lvmin, dvmin, svmax, lvmax, dvmax units (f5eaea1)

11.0.1 (2024-08-05)

Bug Fixes

  • add support for container query units (#214) (3f2b43a), closes #199
  • update postcss-selector-parser (3606777)
  • update postcss-selector-parser (a03fb73)

10.0.0 (2024-04-26)

Breaking Changes

  • drop support for Node.js 14, 16, 19 and 21

9.0.1 (2023-05-05)

Bug Fixes

  • fix disappearing calc expression when source contains extra brackets (18bb47bd0)

9.0.0 (2023-04-29)

Breaking Changes

  • drop support for Node.js versions before 14

8.2.4 (2022-02-05)

Patch Changes

  • convert source to CommonJS and publish untranspiled code (b55adcb)

8.2.3 (2022-01-28)

Bug Fixes

8.2.2 (2022-01-12)

Bug Fixes

  • respect CSS var when reducing (99d9fa5)

8.2.1 (2022-01-11)

Bug Fixes

8.2.0 (2022-01-07)

Features

8.1.0 (2022-01-03)

Features

8.0.0

  • Breaking: Updated PostCSS from v7.x to v8.x (#125)

7.0.5

  • Fixed: reduction

7.0.4

  • Fixed: strips away important factors from multiplications in calc() (#107)

7.0.3

  • Fixed: substracted css-variable from zero (#111)

7.0.2

  • Fixed: incorrect reduction of subtraction from zero (#88)
  • Fixed: doesn't remove calc for single function
  • Fixed: relax parser on unknown units (#76)
  • Fixed: handle numbers with exponen composed (#83)
  • Fixed: handle plus sign before value (#79)
  • Fixed: better handle precision for nested calc (#75)
  • Fixed: properly handle nested add and sub expression inside sub expression (#64)
  • Fixed: handle uppercase units and functions (#71)
  • Fixed: do not break calc with single var (cssnano/cssnano#725)
  • Updated: postcss to 7.0.27 (patch)
  • Updated: postcss-selector-parser to 6.0.2
  • Updated: postcss-value-parser to 4.0.2

7.0.1

  • Updated: postcss to 7.0.2 (patch)
  • Updated: postcss-selector-parser to 5.0.0-rc.4 (patch)
  • Updated: postcss-value-parser to 3.3.1 (patch)

7.0.0

  • Changed: Updated postcss-selector-parser to version 5.0.0-rc.3
  • Changed: Dropped reduce-css-calc as a dependency
  • Fixed: Support constant() and env() (#42, #48)
  • Fixed: Support custom properties with "calc" in its name (#50)
  • Fixed: Remove unnecessary whitespace around * and / (cssnano#625)
  • Fixed: Arithmetic bugs around subtraction (#49)
  • Fixed: Handling of nested calc statements (reduce-css-calc#49)
  • Fixed: Bugs regarding complex calculations (reduce-cs-calc#45)
  • Fixed: 100% incorrectly being transformed to 1 (reduce-css-calc#44)
  • Added: support for case-insensitive calc statements

6.0.2 - 2018-09-25

  • Fixed: use PostCSS 7 (thanks to @douglasduteil)

6.0.1 - 2017-10-10

  • Fixed: throwing error for attribute selectors without a value

6.0.0 - 2017-05-08

  • Breaking: Updated PostCSS from v5.x to v6.x, and reduce-css-calc from v1.x to v2.x (thanks to @andyjansson).

5.3.1 - 2016-08-22

  • Fixed: avoid security issue related to reduce-css-calc@< 1.2.4.

5.3.0 - 2016-07-11

  • Added: support for selector transformation via selectors option. (#29 - @uniquegestaltung)

5.2.1 - 2016-04-10

  • Fixed: support for multiline value (#27)

5.2.0 - 2016-01-08

  • Added: "mediaQueries" option for @media support (#22)

5.1.0 - 2016-01-07

  • Added: "warnWhenCannotResolve" option to warn when calc() are not reduced to a single value (#20)

5.0.0 - 2015-08-25

  • Removed: compatibility with postcss v4.x
  • Added: compatibility with postcss v5.x

4.1.0 - 2015-04-09

  • Added: compatibility with postcss v4.1.x (#12)

4.0.1 - 2015-04-09

  • Fixed: preserve option does not create duplicated values (#7)

4.0.0 - 2015-01-26

  • Added: compatibility with postcss v4.x
  • Changed: partial compatiblity with postcss v3.x (stack traces have lost filename)

3.0.0 - 2014-11-24

  • Added: GNU like exceptions (#4)
  • Added: precision option (#5)
  • Added: preserve option (#6)

2.1.0 - 2014-10-15

  • Added: source of the error (gnu like message) (fix #3)

2.0.1 - 2014-08-10

  • Fixed: correctly ignore unrecognized values (fix #2)

2.0.0 - 2014-08-06

  • Changed: Plugin now return a function to have a consistent api. (ref 1, ref 2)

1.0.0 - 2014-08-04

✨ First release based on rework-calc v1.1.0 (code mainly exported to reduce-css-calc)