| [9af201e] | 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 | [![Support Chat][git-img]][git-url]
|
|---|
| 5 |
|
|---|
| 6 | [PostCSS Calc] lets you reduce `calc()` references whenever it's possible.
|
|---|
| 7 | When multiple units are mixed together in the same expression, the `calc()`
|
|---|
| 8 | statement is left as is, to fallback to the [W3C calc() implementation].
|
|---|
| 9 |
|
|---|
| 10 | ## Installation
|
|---|
| 11 |
|
|---|
| 12 | ```bash
|
|---|
| 13 | npm install postcss-calc
|
|---|
| 14 | ```
|
|---|
| 15 |
|
|---|
| 16 | ## Usage
|
|---|
| 17 |
|
|---|
| 18 | ```js
|
|---|
| 19 | // dependencies
|
|---|
| 20 | var fs = require("fs")
|
|---|
| 21 | var postcss = require("postcss")
|
|---|
| 22 | var calc = require("postcss-calc")
|
|---|
| 23 |
|
|---|
| 24 | // css to be processed
|
|---|
| 25 | var css = fs.readFileSync("input.css", "utf8")
|
|---|
| 26 |
|
|---|
| 27 | // process css
|
|---|
| 28 | var output = postcss()
|
|---|
| 29 | .use(calc())
|
|---|
| 30 | .process(css)
|
|---|
| 31 | .css
|
|---|
| 32 | ```
|
|---|
| 33 |
|
|---|
| 34 | Using this `input.css`:
|
|---|
| 35 |
|
|---|
| 36 | ```css
|
|---|
| 37 | h1 {
|
|---|
| 38 | font-size: calc(16px * 2);
|
|---|
| 39 | height: calc(100px - 2em);
|
|---|
| 40 | width: calc(2*var(--base-width));
|
|---|
| 41 | margin-bottom: calc(16px * 1.5);
|
|---|
| 42 | }
|
|---|
| 43 | ```
|
|---|
| 44 |
|
|---|
| 45 | you will get:
|
|---|
| 46 |
|
|---|
| 47 | ```css
|
|---|
| 48 | h1 {
|
|---|
| 49 | font-size: 32px;
|
|---|
| 50 | height: calc(100px - 2em);
|
|---|
| 51 | width: calc(2*var(--base-width));
|
|---|
| 52 | margin-bottom: 24px
|
|---|
| 53 | }
|
|---|
| 54 | ```
|
|---|
| 55 | Checkout [tests] for more examples.
|
|---|
| 56 |
|
|---|
| 57 | ### Options
|
|---|
| 58 |
|
|---|
| 59 | #### `precision` (default: `5`)
|
|---|
| 60 |
|
|---|
| 61 | Allow you to define the precision for decimal numbers.
|
|---|
| 62 |
|
|---|
| 63 | ```js
|
|---|
| 64 | var out = postcss()
|
|---|
| 65 | .use(calc({precision: 10}))
|
|---|
| 66 | .process(css)
|
|---|
| 67 | .css
|
|---|
| 68 | ```
|
|---|
| 69 |
|
|---|
| 70 | #### `preserve` (default: `false`)
|
|---|
| 71 |
|
|---|
| 72 | Allow you to preserve calc() usage in output so browsers will handle decimal
|
|---|
| 73 | precision themselves.
|
|---|
| 74 |
|
|---|
| 75 | ```js
|
|---|
| 76 | var out = postcss()
|
|---|
| 77 | .use(calc({preserve: true}))
|
|---|
| 78 | .process(css)
|
|---|
| 79 | .css
|
|---|
| 80 | ```
|
|---|
| 81 |
|
|---|
| 82 | #### `warnWhenCannotResolve` (default: `false`)
|
|---|
| 83 |
|
|---|
| 84 | Adds warnings when calc() are not reduced to a single value.
|
|---|
| 85 |
|
|---|
| 86 | ```js
|
|---|
| 87 | var out = postcss()
|
|---|
| 88 | .use(calc({warnWhenCannotResolve: true}))
|
|---|
| 89 | .process(css)
|
|---|
| 90 | .css
|
|---|
| 91 | ```
|
|---|
| 92 |
|
|---|
| 93 | #### `mediaQueries` (default: `false`)
|
|---|
| 94 |
|
|---|
| 95 | Allows calc() usage as part of media query declarations.
|
|---|
| 96 |
|
|---|
| 97 | ```js
|
|---|
| 98 | var out = postcss()
|
|---|
| 99 | .use(calc({mediaQueries: true}))
|
|---|
| 100 | .process(css)
|
|---|
| 101 | .css
|
|---|
| 102 | ```
|
|---|
| 103 |
|
|---|
| 104 | #### `selectors` (default: `false`)
|
|---|
| 105 |
|
|---|
| 106 | Allows calc() usage as part of selectors.
|
|---|
| 107 |
|
|---|
| 108 | ```js
|
|---|
| 109 | var out = postcss()
|
|---|
| 110 | .use(calc({selectors: true}))
|
|---|
| 111 | .process(css)
|
|---|
| 112 | .css
|
|---|
| 113 | ```
|
|---|
| 114 |
|
|---|
| 115 | Example:
|
|---|
| 116 |
|
|---|
| 117 | ```css
|
|---|
| 118 | div[data-size="calc(3*3)"] {
|
|---|
| 119 | width: 100px;
|
|---|
| 120 | }
|
|---|
| 121 | ```
|
|---|
| 122 |
|
|---|
| 123 | ---
|
|---|
| 124 |
|
|---|
| 125 | ## Related PostCSS plugins
|
|---|
| 126 | To replace the value of CSS custom properties at build time, try [PostCSS Custom Properties].
|
|---|
| 127 |
|
|---|
| 128 | ## Contributing
|
|---|
| 129 |
|
|---|
| 130 | Work on a branch, install dev-dependencies, respect coding style & run tests
|
|---|
| 131 | before submitting a bug fix or a feature.
|
|---|
| 132 |
|
|---|
| 133 | ```bash
|
|---|
| 134 | git clone git@github.com:postcss/postcss-calc.git
|
|---|
| 135 | git checkout -b patch-1
|
|---|
| 136 | npm install
|
|---|
| 137 | npm test
|
|---|
| 138 | ```
|
|---|
| 139 |
|
|---|
| 140 | ## [Changelog](CHANGELOG.md)
|
|---|
| 141 |
|
|---|
| 142 | ## [License](LICENSE)
|
|---|
| 143 |
|
|---|
| 144 | [git-img]: https://img.shields.io/badge/support-chat-blue.svg
|
|---|
| 145 | [git-url]: https://gitter.im/postcss/postcss
|
|---|
| 146 | [npm-img]: https://img.shields.io/npm/v/postcss-calc.svg
|
|---|
| 147 | [npm-url]: https://www.npmjs.com/package/postcss-calc
|
|---|
| 148 |
|
|---|
| 149 | [PostCSS]: https://github.com/postcss
|
|---|
| 150 | [PostCSS Calc]: https://github.com/postcss/postcss-calc
|
|---|
| 151 | [PostCSS Custom Properties]: https://github.com/postcss/postcss-custom-properties
|
|---|
| 152 | [tests]: src/__tests__/index.js
|
|---|
| 153 | [W3C calc() implementation]: https://www.w3.org/TR/css3-values/#calc-notation
|
|---|