1 | # PostCSS Calc [<img src="https://postcss.github.io/postcss/logo.svg" alt="PostCSS" width="90" height="90" align="right">][PostCSS]
|
---|
2 |
|
---|
3 | [![NPM Version][npm-img]][npm-url]
|
---|
4 | [![Build Status][cli-img]][cli-url]
|
---|
5 | [![Support Chat][git-img]][git-url]
|
---|
6 |
|
---|
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 |
|
---|
10 | When multiple units are mixed together in the same expression, the `calc()`
|
---|
11 | statement is left as is, to fallback to the [W3C calc() implementation].
|
---|
12 |
|
---|
13 | ## Installation
|
---|
14 |
|
---|
15 | ```bash
|
---|
16 | npm install postcss-calc
|
---|
17 | ```
|
---|
18 |
|
---|
19 | ## Usage
|
---|
20 |
|
---|
21 | ```js
|
---|
22 | // dependencies
|
---|
23 | var fs = require("fs")
|
---|
24 | var postcss = require("postcss")
|
---|
25 | var calc = require("postcss-calc")
|
---|
26 |
|
---|
27 | // css to be processed
|
---|
28 | var css = fs.readFileSync("input.css", "utf8")
|
---|
29 |
|
---|
30 | // process css
|
---|
31 | var output = postcss()
|
---|
32 | .use(calc())
|
---|
33 | .process(css)
|
---|
34 | .css
|
---|
35 | ```
|
---|
36 |
|
---|
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 |
|
---|
57 | Using this `input.css`:
|
---|
58 |
|
---|
59 | ```css
|
---|
60 | :root {
|
---|
61 | --main-font-size: 16px;
|
---|
62 | }
|
---|
63 |
|
---|
64 | body {
|
---|
65 | font-size: var(--main-font-size);
|
---|
66 | }
|
---|
67 |
|
---|
68 | h1 {
|
---|
69 | font-size: calc(var(--main-font-size) * 2);
|
---|
70 | height: calc(100px - 2em);
|
---|
71 | margin-bottom: calc(
|
---|
72 | var(--main-font-size)
|
---|
73 | * 1.5
|
---|
74 | )
|
---|
75 | }
|
---|
76 | ```
|
---|
77 |
|
---|
78 | you will get:
|
---|
79 |
|
---|
80 | ```css
|
---|
81 | body {
|
---|
82 | font-size: 16px
|
---|
83 | }
|
---|
84 |
|
---|
85 | h1 {
|
---|
86 | font-size: 32px;
|
---|
87 | height: calc(100px - 2em);
|
---|
88 | margin-bottom: 24px
|
---|
89 | }
|
---|
90 | ```
|
---|
91 |
|
---|
92 | Checkout [tests] for more examples.
|
---|
93 |
|
---|
94 | ### Options
|
---|
95 |
|
---|
96 | #### `precision` (default: `5`)
|
---|
97 |
|
---|
98 | Allow you to define the precision for decimal numbers.
|
---|
99 |
|
---|
100 | ```js
|
---|
101 | var out = postcss()
|
---|
102 | .use(calc({precision: 10}))
|
---|
103 | .process(css)
|
---|
104 | .css
|
---|
105 | ```
|
---|
106 |
|
---|
107 | #### `preserve` (default: `false`)
|
---|
108 |
|
---|
109 | Allow you to preserve calc() usage in output so browsers will handle decimal
|
---|
110 | precision themselves.
|
---|
111 |
|
---|
112 | ```js
|
---|
113 | var out = postcss()
|
---|
114 | .use(calc({preserve: true}))
|
---|
115 | .process(css)
|
---|
116 | .css
|
---|
117 | ```
|
---|
118 |
|
---|
119 | #### `warnWhenCannotResolve` (default: `false`)
|
---|
120 |
|
---|
121 | Adds warnings when calc() are not reduced to a single value.
|
---|
122 |
|
---|
123 | ```js
|
---|
124 | var out = postcss()
|
---|
125 | .use(calc({warnWhenCannotResolve: true}))
|
---|
126 | .process(css)
|
---|
127 | .css
|
---|
128 | ```
|
---|
129 |
|
---|
130 | #### `mediaQueries` (default: `false`)
|
---|
131 |
|
---|
132 | Allows calc() usage as part of media query declarations.
|
---|
133 |
|
---|
134 | ```js
|
---|
135 | var out = postcss()
|
---|
136 | .use(calc({mediaQueries: true}))
|
---|
137 | .process(css)
|
---|
138 | .css
|
---|
139 | ```
|
---|
140 |
|
---|
141 | #### `selectors` (default: `false`)
|
---|
142 |
|
---|
143 | Allows calc() usage as part of selectors.
|
---|
144 |
|
---|
145 | ```js
|
---|
146 | var out = postcss()
|
---|
147 | .use(calc({selectors: true}))
|
---|
148 | .process(css)
|
---|
149 | .css
|
---|
150 | ```
|
---|
151 |
|
---|
152 | Example:
|
---|
153 |
|
---|
154 | ```css
|
---|
155 | div[data-size="calc(3*3)"] {
|
---|
156 | width: 100px;
|
---|
157 | }
|
---|
158 | ```
|
---|
159 |
|
---|
160 | ---
|
---|
161 |
|
---|
162 | ## Contributing
|
---|
163 |
|
---|
164 | Work on a branch, install dev-dependencies, respect coding style & run tests
|
---|
165 | before submitting a bug fix or a feature.
|
---|
166 |
|
---|
167 | ```bash
|
---|
168 | git clone git@github.com:postcss/postcss-calc.git
|
---|
169 | git checkout -b patch-1
|
---|
170 | npm install
|
---|
171 | npm test
|
---|
172 | ```
|
---|
173 |
|
---|
174 | ## [Changelog](CHANGELOG.md)
|
---|
175 |
|
---|
176 | ## [License](LICENSE)
|
---|
177 |
|
---|
178 | [cli-img]: https://img.shields.io/travis/postcss/postcss-calc/master.svg
|
---|
179 | [cli-url]: https://travis-ci.org/postcss/postcss-calc
|
---|
180 | [git-img]: https://img.shields.io/badge/support-chat-blue.svg
|
---|
181 | [git-url]: https://gitter.im/postcss/postcss
|
---|
182 | [npm-img]: https://img.shields.io/npm/v/postcss-calc.svg
|
---|
183 | [npm-url]: https://www.npmjs.com/package/postcss-calc
|
---|
184 |
|
---|
185 | [PostCSS]: https://github.com/postcss
|
---|
186 | [PostCSS Calc]: https://github.com/postcss/postcss-calc
|
---|
187 | [PostCSS Custom Properties]: https://github.com/postcss/postcss-custom-properties
|
---|
188 | [tests]: src/__tests__/index.js
|
---|
189 | [W3C calc() implementation]: https://www.w3.org/TR/css3-values/#calc-notation
|
---|