18d067119c20fcbb4c1fcb938d2a89bf96cb57519908722f9a272602d73b19fbadf1d2d68ac74430672abb51b0917ca594286b1367447c531670053471bf0b 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. # PostCSS [![Travis Build Status][travis-img]][travis] [![AppVeyor Build Status][appveyor-img]][appveyor] [![Gitter][chat-img]][chat]
  2. <img align="right" width="95" height="95"
  3. title="Philosopher’s stone, logo of PostCSS"
  4. src="http://postcss.github.io/postcss/logo.svg">
  5. [appveyor-img]: https://img.shields.io/appveyor/ci/ai/postcss.svg?label=windows
  6. [travis-img]: https://img.shields.io/travis/postcss/postcss.svg?label=unix
  7. [chat-img]: https://img.shields.io/badge/Gitter-Join_the_PostCSS_chat-brightgreen.svg
  8. [appveyor]: https://ci.appveyor.com/project/ai/postcss
  9. [travis]: https://travis-ci.org/postcss/postcss
  10. [chat]: https://gitter.im/postcss/postcss
  11. PostCSS is a tool for transforming styles with JS plugins.
  12. These plugins can lint your CSS, support variables and mixins,
  13. transpile future CSS syntax, inline images, and more.
  14. PostCSS is used by industry leaders including Wikipedia, Twitter, Alibaba,
  15. and JetBrains. The [Autoprefixer] PostCSS plugin is one of the most popular
  16. CSS processors.
  17. Twitter account: [@postcss](https://twitter.com/postcss).
  18. VK.com page: [postcss](https://vk.com/postcss).
  19. Support / Discussion: [Gitter](https://gitter.im/postcss/postcss).
  20. For PostCSS commercial support (consulting, improving the front-end culture
  21. of your company, PostCSS plugins), contact [Evil Martians](https://evilmartians.com/?utm_source=postcss)
  22. at <surrender@evilmartians.com>.
  23. [Autoprefixer]: https://github.com/postcss/autoprefixer
  24. <a href="https://evilmartians.com/?utm_source=postcss">
  25. <img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg"
  26. alt="Sponsored by Evil Martians" width="236" height="54">
  27. </a>
  28. ## Plugins
  29. Currently, PostCSS has more than 200 plugins. You can find all of the plugins
  30. in the [plugins list] or in the [searchable catalog]. Below is a list
  31. of our favorite plugins — the best demonstrations of what can be built
  32. on top of PostCSS.
  33. If you have any new ideas, [PostCSS plugin development] is really easy.
  34. [searchable catalog]: http://postcss.parts
  35. [plugins list]: https://github.com/postcss/postcss/blob/master/docs/plugins.md
  36. ### Solve Global CSS Problem
  37. * [`postcss-use`] allows you to explicitly set PostCSS plugins within CSS
  38. and execute them only for the current file.
  39. * [`postcss-modules`] and [`react-css-modules`] automatically isolate
  40. selectors within components.
  41. * [`postcss-autoreset`] is an alternative to using a global reset
  42. that is better for isolatable components.
  43. * [`postcss-initial`] adds `all: initial` support, which resets
  44. all inherited styles.
  45. * [`cq-prolyfill`] adds container query support, allowing styles that respond
  46. to the width of the parent.
  47. ### Use Future CSS, Today
  48. * [`autoprefixer`] adds vendor prefixes, using data from Can I Use.
  49. * [`postcss-cssnext`] allows you to use future CSS features today
  50. (includes `autoprefixer`).
  51. * [`postcss-image-set-polyfill`] emulates [`image-set`] function logic for all browsers
  52. ### Better CSS Readability
  53. * [`precss`] contains plugins for Sass-like features, like variables, nesting,
  54. and mixins.
  55. * [`postcss-sorting`] sorts the content of rules and at-rules.
  56. * [`postcss-utilities`] includes the most commonly used shortcuts and helpers.
  57. * [`short`] adds and extends numerous shorthand properties.
  58. ### Images and Fonts
  59. * [`postcss-assets`] inserts image dimensions and inlines files.
  60. * [`postcss-sprites`] generates image sprites.
  61. * [`font-magician`] generates all the `@font-face` rules needed in CSS.
  62. * [`postcss-inline-svg`] allows you to inline SVG and customize its styles.
  63. * [`postcss-write-svg`] allows you to write simple SVG directly in your CSS.
  64. ### Linters
  65. * [`stylelint`] is a modular stylesheet linter.
  66. * [`stylefmt`] is a tool that automatically formats CSS
  67. according `stylelint` rules.
  68. * [`doiuse`] lints CSS for browser support, using data from Can I Use.
  69. * [`colorguard`] helps you maintain a consistent color palette.
  70. ### Other
  71. * [`postcss-rtl`] combines both-directional (left-to-right and right-to-left) styles in one CSS file.
  72. * [`cssnano`] is a modular CSS minifier.
  73. * [`lost`] is a feature-rich `calc()` grid system.
  74. * [`rtlcss`] mirrors styles for right-to-left locales.
  75. [PostCSS plugin development]: https://github.com/postcss/postcss/blob/master/docs/writing-a-plugin.md
  76. [`postcss-inline-svg`]: https://github.com/TrySound/postcss-inline-svg
  77. [`react-css-modules`]: https://github.com/gajus/react-css-modules
  78. [`postcss-autoreset`]: https://github.com/maximkoretskiy/postcss-autoreset
  79. [`postcss-write-svg`]: https://github.com/jonathantneal/postcss-write-svg
  80. [`postcss-utilities`]: https://github.com/ismamz/postcss-utilities
  81. [`postcss-initial`]: https://github.com/maximkoretskiy/postcss-initial
  82. [`postcss-sprites`]: https://github.com/2createStudio/postcss-sprites
  83. [`postcss-modules`]: https://github.com/outpunk/postcss-modules
  84. [`postcss-sorting`]: https://github.com/hudochenkov/postcss-sorting
  85. [`postcss-cssnext`]: http://cssnext.io
  86. [`postcss-image-set-polyfill`]: https://github.com/SuperOl3g/postcss-image-set-polyfill
  87. [`postcss-assets`]: https://github.com/assetsjs/postcss-assets
  88. [`font-magician`]: https://github.com/jonathantneal/postcss-font-magician
  89. [`autoprefixer`]: https://github.com/postcss/autoprefixer
  90. [`cq-prolyfill`]: https://github.com/ausi/cq-prolyfill
  91. [`postcss-rtl`]: https://github.com/vkalinichev/postcss-rtl
  92. [`postcss-use`]: https://github.com/postcss/postcss-use
  93. [`css-modules`]: https://github.com/css-modules/css-modules
  94. [`colorguard`]: https://github.com/SlexAxton/css-colorguard
  95. [`stylelint`]: https://github.com/stylelint/stylelint
  96. [`stylefmt`]: https://github.com/morishitter/stylefmt
  97. [`cssnano`]: http://cssnano.co
  98. [`precss`]: https://github.com/jonathantneal/precss
  99. [`doiuse`]: https://github.com/anandthakker/doiuse
  100. [`rtlcss`]: https://github.com/MohammadYounes/rtlcss
  101. [`short`]: https://github.com/jonathantneal/postcss-short
  102. [`lost`]: https://github.com/peterramsing/lost
  103. [`image-set`]: https://drafts.csswg.org/css-images-3/#image-set-notation
  104. ## Syntaxes
  105. PostCSS can transform styles in any syntax, not just CSS.
  106. If there is not yet support for your favorite syntax,
  107. you can write a parser and/or stringifier to extend PostCSS.
  108. * [`sugarss`] is a indent-based syntax like Sass or Stylus.
  109. * [`postcss-scss`] allows you to work with SCSS
  110. *(but does not compile SCSS to CSS)*.
  111. * [`postcss-sass`] allows you to work with Sass
  112. *(but does not compile Sass to CSS)*.
  113. * [`postcss-less`] allows you to work with Less
  114. *(but does not compile LESS to CSS)*.
  115. * [`postcss-less-engine`] allows you to work with Less
  116. *(and DOES compile LESS to CSS using true Less.js evaluation)*.
  117. * [`postcss-js`] allows you to write styles in JS or transform
  118. React Inline Styles, Radium or JSS.
  119. * [`postcss-safe-parser`] finds and fixes CSS syntax errors.
  120. * [`midas`] converts a CSS string to highlighted HTML.
  121. [`sugarss`]: https://github.com/postcss/sugarss
  122. [`postcss-scss`]: https://github.com/postcss/postcss-scss
  123. [`postcss-sass`]: https://github.com/AleshaOleg/postcss-sass
  124. [`postcss-less`]: https://github.com/webschik/postcss-less
  125. [`postcss-less-engine`]: https://github.com/Crunch/postcss-less
  126. [`postcss-js`]: https://github.com/postcss/postcss-js
  127. [`postcss-safe-parser`]: https://github.com/postcss/postcss-safe-parser
  128. [`midas`]: https://github.com/ben-eb/midas
  129. ## Articles
  130. * [Some things you may think about PostCSS… and you might be wrong](http://julian.io/some-things-you-may-think-about-postcss-and-you-might-be-wrong)
  131. * [What PostCSS Really Is; What It Really Does](http://davidtheclark.com/its-time-for-everyone-to-learn-about-postcss)
  132. * [PostCSS Guides](http://webdesign.tutsplus.com/series/postcss-deep-dive--cms-889)
  133. More articles and videos you can find on [awesome-postcss](https://github.com/jjaderg/awesome-postcss) list.
  134. ## Books
  135. * [Mastering PostCSS for Web Design](https://www.packtpub.com/web-development/mastering-postcss-web-design) by Alex Libby, Packt. (June 2016)
  136. ## Usage
  137. You can start using PostCSS in just two steps:
  138. 1. Find and add PostCSS extensions for your build tool.
  139. 2. [Select plugins] and add them to your PostCSS process.
  140. [Select plugins]: http://postcss.parts
  141. ### Webpack
  142. Use [`postcss-loader`] in `webpack.config.js`:
  143. ```js
  144. module.exports = {
  145. module: {
  146. loaders: [
  147. {
  148. test: /\.css$/,
  149. exclude: /node_modules/,
  150. use: [
  151. {
  152. loader: 'style-loader',
  153. },
  154. {
  155. loader: 'css-loader',
  156. options: {
  157. sourceMap: true,
  158. importLoaders: 1,
  159. }
  160. },
  161. {
  162. loader: 'postcss-loader',
  163. options: {
  164. sourceMap: 'inline',
  165. }
  166. }
  167. ]
  168. }
  169. ]
  170. }
  171. }
  172. ```
  173. Then create `postcss.config.js`:
  174. ```js
  175. module.exports = {
  176. plugins: [
  177. require('precss'),
  178. require('autoprefixer')
  179. ]
  180. }
  181. ```
  182. [`postcss-loader`]: https://github.com/postcss/postcss-loader
  183. ### Gulp
  184. Use [`gulp-postcss`] and [`gulp-sourcemaps`].
  185. ```js
  186. gulp.task('css', function () {
  187. var postcss = require('gulp-postcss');
  188. var sourcemaps = require('gulp-sourcemaps');
  189. return gulp.src('src/**/*.css')
  190. .pipe( sourcemaps.init() )
  191. .pipe( postcss([ require('precss'), require('autoprefixer') ]) )
  192. .pipe( sourcemaps.write('.') )
  193. .pipe( gulp.dest('build/') );
  194. });
  195. ```
  196. [`gulp-sourcemaps`]: https://github.com/floridoo/gulp-sourcemaps
  197. [`gulp-postcss`]: https://github.com/postcss/gulp-postcss
  198. ### npm run / CLI
  199. To use PostCSS from your command-line interface or with npm scripts
  200. there is [`postcss-cli`].
  201. ```sh
  202. postcss --use autoprefixer -c options.json -o main.css css/*.css
  203. ```
  204. [`postcss-cli`]: https://github.com/postcss/postcss-cli
  205. ### Browser
  206. If you want to compile CSS string in browser (for instance, in live edit
  207. tools like CodePen), just use [Browserify] or [webpack]. They will pack
  208. PostCSS and plugins files into a single file.
  209. To apply PostCSS plugins to React Inline Styles, JSS, Radium
  210. and other [CSS-in-JS], you can use [`postcss-js`] and transforms style objects.
  211. ```js
  212. var postcss = require('postcss-js');
  213. var prefixer = postcss.sync([ require('autoprefixer') ]);
  214. prefixer({ display: 'flex' }); //=> { display: ['-webkit-box', '-webkit-flex', '-ms-flexbox', 'flex'] }
  215. ```
  216. [`postcss-js`]: https://github.com/postcss/postcss-js
  217. [Browserify]: http://browserify.org/
  218. [webpack]: https://webpack.github.io/
  219. [CSS-in-JS]: https://github.com/MicheleBertoli/css-in-js
  220. ### Runners
  221. * **Grunt**: [`grunt-postcss`](https://github.com/nDmitry/grunt-postcss)
  222. * **HTML**: [`posthtml-postcss`](https://github.com/posthtml/posthtml-postcss)
  223. * **Stylus**: [`poststylus`](https://github.com/seaneking/poststylus)
  224. * **Rollup**: [`rollup-plugin-postcss`](https://github.com/egoist/rollup-plugin-postcss)
  225. * **Brunch**: [`postcss-brunch`](https://github.com/brunch/postcss-brunch)
  226. * **Broccoli**: [`broccoli-postcss`](https://github.com/jeffjewiss/broccoli-postcss)
  227. * **Meteor**: [`postcss`](https://atmospherejs.com/juliancwirko/postcss)
  228. * **ENB**: [`enb-postcss`](https://github.com/awinogradov/enb-postcss)
  229. * **Fly**: [`fly-postcss`](https://github.com/postcss/fly-postcss)
  230. * **Start**: [`start-postcss`](https://github.com/start-runner/postcss)
  231. * **Connect/Express**: [`postcss-middleware`](https://github.com/jedmao/postcss-middleware)
  232. ### JS API
  233. For other environments, you can use the JS API:
  234. ```js
  235. const fs = require('fs');
  236. const postcss = require('postcss');
  237. const precss = require('precss');
  238. const autoprefixer = require('autoprefixer');
  239. fs.readFile('src/app.css', (err, css) => {
  240. postcss([precss, autoprefixer])
  241. .process(css, { from: 'src/app.css', to: 'dest/app.css' })
  242. .then(result => {
  243. fs.writeFile('dest/app.css', result.css);
  244. if ( result.map ) fs.writeFile('dest/app.css.map', result.map);
  245. });
  246. });
  247. ```
  248. Read the [PostCSS API documentation] for more details about the JS API.
  249. All PostCSS runners should pass [PostCSS Runner Guidelines].
  250. [PostCSS Runner Guidelines]: https://github.com/postcss/postcss/blob/master/docs/guidelines/runner.md
  251. [PostCSS API documentation]: http://api.postcss.org/postcss.html
  252. ### Options
  253. Most PostCSS runners accept two parameters:
  254. * An array of plugins.
  255. * An object of options.
  256. Common options:
  257. * `syntax`: an object providing a syntax parser and a stringifier.
  258. * `parser`: a special syntax parser (for example, [SCSS]).
  259. * `stringifier`: a special syntax output generator (for example, [Midas]).
  260. * `map`: [source map options].
  261. * `from`: the input file name (most runners set it automatically).
  262. * `to`: the output file name (most runners set it automatically).
  263. [source map options]: https://github.com/postcss/postcss/blob/master/docs/source-maps.md
  264. [Midas]: https://github.com/ben-eb/midas
  265. [SCSS]: https://github.com/postcss/postcss-scss
  266. ### Node.js 0.10 and the Promise API
  267. If you want to run PostCSS in Node.js 0.10, add the [Promise polyfill]:
  268. ```js
  269. require('es6-promise').polyfill();
  270. var postcss = require('postcss');
  271. ```
  272. [Promise polyfill]: https://github.com/jakearchibald/es6-promise
  273. ## Editors & IDE Integration
  274. ### Atom
  275. * [`language-postcss`] adds PostCSS and [SugarSS] highlight.
  276. * [`source-preview-postcss`] previews your output CSS in a separate, live pane.
  277. [SugarSS]: https://github.com/postcss/sugarss
  278. ### Sublime Text
  279. * [`Syntax-highlighting-for-PostCSS`] adds PostCSS highlight.
  280. [`Syntax-highlighting-for-PostCSS`]: https://github.com/hudochenkov/Syntax-highlighting-for-PostCSS
  281. [`source-preview-postcss`]: https://atom.io/packages/source-preview-postcss
  282. [`language-postcss`]: https://atom.io/packages/language-postcss
  283. ### Vim
  284. * [`postcss.vim`] adds PostCSS highlight.
  285. [`postcss.vim`]: https://github.com/stephenway/postcss.vim
  286. ### WebStorm
  287. WebStorm 2016.3 [has] built-in PostCSS support.
  288. [has]: https://blog.jetbrains.com/webstorm/2016/08/webstorm-2016-3-early-access-preview/