11# PostCSS Calc [ <img src =" https://postcss.github.io/postcss/logo.svg " alt =" PostCSS " width =" 90 " height =" 90 " align =" right " >] [ PostCSS ]
22
33[ ![ NPM Version] [ npm-img ]] [ npm-url ]
4- [ ![ Build Status] [ cli-img ]] [ cli-url ]
54[ ![ Support Chat] [ git-img ]] [ git-url ]
65
7- [ PostCSS Calc] lets you reduce ` calc() ` references whenever it's possible. This
8- can be particularly useful with the [ PostCSS Custom Properties] plugin.
9-
6+ [ PostCSS Calc] lets you reduce ` calc() ` references whenever it's possible.
107When multiple units are mixed together in the same expression, the ` calc() `
118statement is left as is, to fallback to the [ W3C calc() implementation] .
129
@@ -34,61 +31,27 @@ var output = postcss()
3431 .css
3532```
3633
37- ** Example** (with [ PostCSS Custom Properties] enabled as well):
38-
39- ``` js
40- // dependencies
41- var fs = require (" fs" )
42- var postcss = require (" postcss" )
43- var customProperties = require (" postcss-custom-properties" )
44- var calc = require (" postcss-calc" )
45-
46- // css to be processed
47- var css = fs .readFileSync (" input.css" , " utf8" )
48-
49- // process css
50- var output = postcss ()
51- .use (customProperties ())
52- .use (calc ())
53- .process (css)
54- .css
55- ```
56-
5734Using this ` input.css ` :
5835
5936``` css
60- :root {
61- --main-font-size : 16px ;
62- }
63-
64- body {
65- font-size : var (--main-font-size );
66- }
67-
6837h1 {
69- font-size : calc (var ( --main-font-size ) * 2 );
38+ font-size : calc (16 px * 2 );
7039 height : calc (100px - 2em );
71- margin-bottom : calc (
72- var (--main-font-size )
73- * 1.5
74- )
40+ width : calc (2 * var (--base-width ));
41+ margin-bottom : calc (16px * 1.5 );
7542}
7643```
7744
7845you will get:
7946
8047``` css
81- body {
82- font-size : 16px
83- }
84-
8548h1 {
8649 font-size : 32px ;
8750 height : calc (100px - 2em );
51+ width : calc (2 * var (--base-width ));
8852 margin-bottom : 24px
8953}
9054```
91-
9255Checkout [ tests] for more examples.
9356
9457### Options
@@ -159,6 +122,9 @@ div[data-size="calc(3*3)"] {
159122
160123---
161124
125+ ## Related PostCSS plugins
126+ To replace the value of CSS custom properties at build time, try [ PostCSS Custom Properties] .
127+
162128## Contributing
163129
164130Work on a branch, install dev-dependencies, respect coding style & run tests
@@ -175,8 +141,6 @@ npm test
175141
176142## [ License] ( LICENSE )
177143
178- [ cli-img ] : https://img.shields.io/travis/postcss/postcss-calc/master.svg
179- [ cli-url ] : https://travis-ci.org/postcss/postcss-calc
180144[ git-img ] : https://img.shields.io/badge/support-chat-blue.svg
181145[ git-url ] : https://gitter.im/postcss/postcss
182146[ npm-img ] : https://img.shields.io/npm/v/postcss-calc.svg
0 commit comments