52113c074c2c6eeb5b9e1170e54e75554675bf71ba593da4b252da219b73dd193bde7b8022753047d2c0313fb020c90dfb1266596064151e2f761755b187c3-exec 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. # postcss-calc [![Build Status](https://travis-ci.org/postcss/postcss-calc.png)](https://travis-ci.org/postcss/postcss-calc)
  2. > [PostCSS](https://github.com/postcss/postcss) plugin to reduce `calc()`.
  3. This plugin reduce `calc()` references whenever it's possible.
  4. This can be particularly useful with the [postcss-custom-properties](https://github.com/postcss/postcss-custom-properties) plugin.
  5. **Note:** When multiple units are mixed together in the same expression, the `calc()` statement is left as is, to fallback to the [w3c calc() feature](http://www.w3.org/TR/css3-values/#calc).
  6. ## Installation
  7. ```console
  8. $ npm install postcss-calc
  9. ```
  10. ## Usage
  11. ```js
  12. // dependencies
  13. var fs = require("fs")
  14. var postcss = require("postcss")
  15. var calc = require("postcss-calc")
  16. // css to be processed
  17. var css = fs.readFileSync("input.css", "utf8")
  18. // process css
  19. var output = postcss()
  20. .use(calc())
  21. .process(css)
  22. .css
  23. ```
  24. **Example** (with [postcss-custom-properties](https://github.com/postcss/postcss-custom-properties) enabled as well):
  25. ```js
  26. // dependencies
  27. var fs = require("fs")
  28. var postcss = require("postcss")
  29. var customProperties = require("postcss-custom-properties")
  30. var calc = require("postcss-calc")
  31. // css to be processed
  32. var css = fs.readFileSync("input.css", "utf8")
  33. // process css
  34. var output = postcss()
  35. .use(customProperties())
  36. .use(calc())
  37. .process(css)
  38. .css
  39. ```
  40. Using this `input.css`:
  41. ```css
  42. :root {
  43. --main-font-size: 16px;
  44. }
  45. body {
  46. font-size: var(--main-font-size);
  47. }
  48. h1 {
  49. font-size: calc(var(--main-font-size) * 2);
  50. height: calc(100px - 2em);
  51. margin-bottom: calc(
  52. var(--main-font-size)
  53. * 1.5
  54. )
  55. }
  56. ```
  57. you will get:
  58. ```css
  59. body {
  60. font-size: 16px
  61. }
  62. h1 {
  63. font-size: 32px;
  64. height: calc(100px - 2em);
  65. margin-bottom: 24px
  66. }
  67. ```
  68. Checkout [tests](test) for more examples.
  69. ### Options
  70. #### `precision` (default: `5`)
  71. Allow you to definine the precision for decimal numbers.
  72. ```js
  73. var out = postcss()
  74. .use(calc({precision: 10}))
  75. .process(css)
  76. .css
  77. ```
  78. #### `preserve` (default: `false`)
  79. Allow you to preserve calc() usage in output so browsers will handle decimal precision themselves.
  80. ```js
  81. var out = postcss()
  82. .use(calc({preserve: true}))
  83. .process(css)
  84. .css
  85. ```
  86. #### `warnWhenCannotResolve` (default: `false`)
  87. Adds warnings when calc() are not reduced to a single value.
  88. ```js
  89. var out = postcss()
  90. .use(calc({warnWhenCannotResolve: true}))
  91. .process(css)
  92. .css
  93. ```
  94. #### `mediaQueries` (default: `false`)
  95. Allows calc() usage as part of media query declarations.
  96. ```js
  97. var out = postcss()
  98. .use(calc({mediaQueries: true}))
  99. .process(css)
  100. .css
  101. ```
  102. #### `selectors` (default: `false`)
  103. Allows calc() usage as part of selectors.
  104. ```js
  105. var out = postcss()
  106. .use(calc({selectors: true}))
  107. .process(css)
  108. .css
  109. ```
  110. Example:
  111. ```css
  112. div[data-size="calc(3*3)"] {
  113. width: 100px;
  114. }
  115. ```
  116. ---
  117. ## Contributing
  118. Work on a branch, install dev-dependencies, respect coding style & run tests before submitting a bug fix or a feature.
  119. ```console
  120. $ git clone https://github.com/postcss/postcss-calc.git
  121. $ git checkout -b patch-1
  122. $ npm install
  123. $ npm test
  124. ```
  125. ## [Changelog](CHANGELOG.md)
  126. ## [License](LICENSE)