1 # PostCSS [![Gitter][chat-img]][chat]
3 <img align="right" width="95" height="95"
4 alt="Philosopher’s stone, logo of PostCSS"
5 src="http://postcss.github.io/postcss/logo.svg">
7 [chat-img]: https://img.shields.io/badge/Gitter-Join_the_PostCSS_chat-brightgreen.svg
8 [chat]: https://gitter.im/postcss/postcss
10 PostCSS is a tool for transforming styles with JS plugins.
11 These plugins can lint your CSS, support variables and mixins,
12 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
18 PostCSS takes a CSS file and provides an API to analyze and modify its rules
19 (by transforming them into an [Abstract Syntax Tree]).
20 This API can then be used by [plugins] to do a lot of useful things,
21 e.g. to find errors automatically insert vendor prefixes.
23 **Support / Discussion:** [Gitter](https://gitter.im/postcss/postcss)<br>
24 **Twitter account:** [@postcss](https://twitter.com/postcss)<br>
25 **VK.com page:** [postcss](https://vk.com/postcss)<br>
26 **中文翻译**: [`README-cn.md`](./README-cn.md)
28 For PostCSS commercial support (consulting, improving the front-end culture
29 of your company, PostCSS plugins), contact [Evil Martians](https://evilmartians.com/?utm_source=postcss)
30 at <surrender@evilmartians.com>.
32 [Abstract Syntax Tree]: https://en.wikipedia.org/wiki/Abstract_syntax_tree
33 [Autoprefixer]: https://github.com/postcss/autoprefixer
34 [plugins]: https://github.com/postcss/postcss#plugins
36 <a href="https://evilmartians.com/?utm_source=postcss">
37 <img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg"
38 alt="Sponsored by Evil Martians" width="236" height="54">
43 Currently, PostCSS has more than 200 plugins. You can find all of the plugins
44 in the [plugins list] or in the [searchable catalog]. Below is a list
45 of our favorite plugins — the best demonstrations of what can be built
48 If you have any new ideas, [PostCSS plugin development] is really easy.
50 [searchable catalog]: http://postcss.parts
51 [plugins list]: https://github.com/postcss/postcss/blob/master/docs/plugins.md
53 ### Solve Global CSS Problem
55 * [`postcss-use`] allows you to explicitly set PostCSS plugins within CSS
56 and execute them only for the current file.
57 * [`postcss-modules`] and [`react-css-modules`] automatically isolate
58 selectors within components.
59 * [`postcss-autoreset`] is an alternative to using a global reset
60 that is better for isolatable components.
61 * [`postcss-initial`] adds `all: initial` support, which resets
63 * [`cq-prolyfill`] adds container query support, allowing styles that respond
64 to the width of the parent.
66 ### Use Future CSS, Today
68 * [`autoprefixer`] adds vendor prefixes, using data from Can I Use.
69 * [`postcss-preset-env`] allows you to use future CSS features today.
71 ### Better CSS Readability
73 * [`precss`] contains plugins for Sass-like features, like variables, nesting,
75 * [`postcss-sorting`] sorts the content of rules and at-rules.
76 * [`postcss-utilities`] includes the most commonly used shortcuts and helpers.
77 * [`short`] adds and extends numerous shorthand properties.
81 * [`postcss-assets`] inserts image dimensions and inlines files.
82 * [`postcss-sprites`] generates image sprites.
83 * [`font-magician`] generates all the `@font-face` rules needed in CSS.
84 * [`postcss-inline-svg`] allows you to inline SVG and customize its styles.
85 * [`postcss-write-svg`] allows you to write simple SVG directly in your CSS.
89 * [`stylelint`] is a modular stylesheet linter.
90 * [`stylefmt`] is a tool that automatically formats CSS
91 according `stylelint` rules.
92 * [`doiuse`] lints CSS for browser support, using data from Can I Use.
93 * [`colorguard`] helps you maintain a consistent color palette.
97 * [`postcss-rtl`] combines both-directional (left-to-right and right-to-left) styles in one CSS file.
98 * [`cssnano`] is a modular CSS minifier.
99 * [`lost`] is a feature-rich `calc()` grid system.
100 * [`rtlcss`] mirrors styles for right-to-left locales.
102 [PostCSS plugin development]: https://github.com/postcss/postcss/blob/master/docs/writing-a-plugin.md
103 [`postcss-inline-svg`]: https://github.com/TrySound/postcss-inline-svg
104 [`postcss-preset-env`]: https://github.com/jonathantneal/postcss-preset-env
105 [`react-css-modules`]: https://github.com/gajus/react-css-modules
106 [`postcss-autoreset`]: https://github.com/maximkoretskiy/postcss-autoreset
107 [`postcss-write-svg`]: https://github.com/jonathantneal/postcss-write-svg
108 [`postcss-utilities`]: https://github.com/ismamz/postcss-utilities
109 [`postcss-initial`]: https://github.com/maximkoretskiy/postcss-initial
110 [`postcss-sprites`]: https://github.com/2createStudio/postcss-sprites
111 [`postcss-modules`]: https://github.com/outpunk/postcss-modules
112 [`postcss-sorting`]: https://github.com/hudochenkov/postcss-sorting
113 [`postcss-assets`]: https://github.com/assetsjs/postcss-assets
114 [`font-magician`]: https://github.com/jonathantneal/postcss-font-magician
115 [`autoprefixer`]: https://github.com/postcss/autoprefixer
116 [`cq-prolyfill`]: https://github.com/ausi/cq-prolyfill
117 [`postcss-rtl`]: https://github.com/vkalinichev/postcss-rtl
118 [`postcss-use`]: https://github.com/postcss/postcss-use
119 [`css-modules`]: https://github.com/css-modules/css-modules
120 [`colorguard`]: https://github.com/SlexAxton/css-colorguard
121 [`stylelint`]: https://github.com/stylelint/stylelint
122 [`stylefmt`]: https://github.com/morishitter/stylefmt
123 [`cssnano`]: http://cssnano.co
124 [`precss`]: https://github.com/jonathantneal/precss
125 [`doiuse`]: https://github.com/anandthakker/doiuse
126 [`rtlcss`]: https://github.com/MohammadYounes/rtlcss
127 [`short`]: https://github.com/jonathantneal/postcss-short
128 [`lost`]: https://github.com/peterramsing/lost
132 PostCSS can transform styles in any syntax, not just CSS.
133 If there is not yet support for your favorite syntax,
134 you can write a parser and/or stringifier to extend PostCSS.
136 * [`sugarss`] is a indent-based syntax like Sass or Stylus.
137 * [`postcss-syntax`] switch syntax automatically by file extensions.
138 * [`postcss-html`] parsing styles in `<style>` tags of HTML-like files.
139 * [`postcss-markdown`] parsing styles in code blocks of Markdown files.
140 * [`postcss-jsx`] parsing CSS in template / object literals of source files.
141 * [`postcss-styled`] parsing CSS in template literals of source files.
142 * [`postcss-scss`] allows you to work with SCSS
143 *(but does not compile SCSS to CSS)*.
144 * [`postcss-sass`] allows you to work with Sass
145 *(but does not compile Sass to CSS)*.
146 * [`postcss-less`] allows you to work with Less
147 *(but does not compile LESS to CSS)*.
148 * [`postcss-less-engine`] allows you to work with Less
149 *(and DOES compile LESS to CSS using true Less.js evaluation)*.
150 * [`postcss-js`] allows you to write styles in JS or transform
151 React Inline Styles, Radium or JSS.
152 * [`postcss-safe-parser`] finds and fixes CSS syntax errors.
153 * [`midas`] converts a CSS string to highlighted HTML.
155 [`postcss-less-engine`]: https://github.com/Crunch/postcss-less
156 [`postcss-safe-parser`]: https://github.com/postcss/postcss-safe-parser
157 [`postcss-syntax`]: https://github.com/gucong3000/postcss-syntax
158 [`postcss-html`]: https://github.com/gucong3000/postcss-html
159 [`postcss-markdown`]: https://github.com/gucong3000/postcss-markdown
160 [`postcss-jsx`]: https://github.com/gucong3000/postcss-jsx
161 [`postcss-styled`]: https://github.com/gucong3000/postcss-styled
162 [`postcss-scss`]: https://github.com/postcss/postcss-scss
163 [`postcss-sass`]: https://github.com/AleshaOleg/postcss-sass
164 [`postcss-less`]: https://github.com/webschik/postcss-less
165 [`postcss-js`]: https://github.com/postcss/postcss-js
166 [`sugarss`]: https://github.com/postcss/sugarss
167 [`midas`]: https://github.com/ben-eb/midas
171 * [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)
172 * [What PostCSS Really Is; What It Really Does](http://davidtheclark.com/its-time-for-everyone-to-learn-about-postcss)
173 * [PostCSS Guides](http://webdesign.tutsplus.com/series/postcss-deep-dive--cms-889)
175 More articles and videos you can find on [awesome-postcss](https://github.com/jjaderg/awesome-postcss) list.
179 * [Mastering PostCSS for Web Design](https://www.packtpub.com/web-development/mastering-postcss-web-design) by Alex Libby, Packt. (June 2016)
183 You can start using PostCSS in just two steps:
185 1. Find and add PostCSS extensions for your build tool.
186 2. [Select plugins] and add them to your PostCSS process.
188 [Select plugins]: http://postcss.parts
192 Use [`postcss-loader`] in `webpack.config.js`:
200 exclude: /node_modules/,
203 loader: 'style-loader',
206 loader: 'css-loader',
212 loader: 'postcss-loader'
221 Then create `postcss.config.js`:
227 require('autoprefixer')
232 [`postcss-loader`]: https://github.com/postcss/postcss-loader
236 Use [`gulp-postcss`] and [`gulp-sourcemaps`].
239 gulp.task('css', function () {
240 var postcss = require('gulp-postcss');
241 var sourcemaps = require('gulp-sourcemaps');
243 return gulp.src('src/**/*.css')
244 .pipe( sourcemaps.init() )
245 .pipe( postcss([ require('precss'), require('autoprefixer') ]) )
246 .pipe( sourcemaps.write('.') )
247 .pipe( gulp.dest('build/') );
251 [`gulp-sourcemaps`]: https://github.com/floridoo/gulp-sourcemaps
252 [`gulp-postcss`]: https://github.com/postcss/gulp-postcss
256 To use PostCSS from your command-line interface or with npm scripts
257 there is [`postcss-cli`].
260 postcss --use autoprefixer -c options.json -o main.css css/*.css
263 [`postcss-cli`]: https://github.com/postcss/postcss-cli
267 If you want to compile CSS string in browser (for instance, in live edit
268 tools like CodePen), just use [Browserify] or [webpack]. They will pack
269 PostCSS and plugins files into a single file.
271 To apply PostCSS plugins to React Inline Styles, JSS, Radium
272 and other [CSS-in-JS], you can use [`postcss-js`] and transforms style objects.
275 var postcss = require('postcss-js');
276 var prefixer = postcss.sync([ require('autoprefixer') ]);
278 prefixer({ display: 'flex' }); //=> { display: ['-webkit-box', '-webkit-flex', '-ms-flexbox', 'flex'] }
281 [`postcss-js`]: https://github.com/postcss/postcss-js
282 [Browserify]: http://browserify.org/
283 [CSS-in-JS]: https://github.com/MicheleBertoli/css-in-js
284 [webpack]: https://webpack.github.io/
288 * **Grunt**: [`grunt-postcss`](https://github.com/nDmitry/grunt-postcss)
289 * **HTML**: [`posthtml-postcss`](https://github.com/posthtml/posthtml-postcss)
290 * **Stylus**: [`poststylus`](https://github.com/seaneking/poststylus)
291 * **Rollup**: [`rollup-plugin-postcss`](https://github.com/egoist/rollup-plugin-postcss)
292 * **Brunch**: [`postcss-brunch`](https://github.com/brunch/postcss-brunch)
293 * **Broccoli**: [`broccoli-postcss`](https://github.com/jeffjewiss/broccoli-postcss)
294 * **Meteor**: [`postcss`](https://atmospherejs.com/juliancwirko/postcss)
295 * **ENB**: [`enb-postcss`](https://github.com/awinogradov/enb-postcss)
296 * **Taskr**: [`taskr-postcss`](https://github.com/lukeed/taskr/tree/master/packages/postcss)
297 * **Start**: [`start-postcss`](https://github.com/start-runner/postcss)
298 * **Connect/Express**: [`postcss-middleware`](https://github.com/jedmao/postcss-middleware)
302 For other environments, you can use the JS API:
305 const fs = require('fs');
306 const postcss = require('postcss');
307 const precss = require('precss');
308 const autoprefixer = require('autoprefixer');
310 fs.readFile('src/app.css', (err, css) => {
311 postcss([precss, autoprefixer])
312 .process(css, { from: 'src/app.css', to: 'dest/app.css' })
314 fs.writeFile('dest/app.css', result.css, () => true);
316 fs.writeFile('dest/app.css.map', result.map, () => true);
322 Read the [PostCSS API documentation] for more details about the JS API.
324 All PostCSS runners should pass [PostCSS Runner Guidelines].
326 [PostCSS Runner Guidelines]: https://github.com/postcss/postcss/blob/master/docs/guidelines/runner.md
327 [PostCSS API documentation]: http://api.postcss.org/postcss.html
331 Most PostCSS runners accept two parameters:
333 * An array of plugins.
334 * An object of options.
338 * `syntax`: an object providing a syntax parser and a stringifier.
339 * `parser`: a special syntax parser (for example, [SCSS]).
340 * `stringifier`: a special syntax output generator (for example, [Midas]).
341 * `map`: [source map options].
342 * `from`: the input file name (most runners set it automatically).
343 * `to`: the output file name (most runners set it automatically).
345 [source map options]: https://github.com/postcss/postcss/blob/master/docs/source-maps.md
346 [Midas]: https://github.com/ben-eb/midas
347 [SCSS]: https://github.com/postcss/postcss-scss
349 ## Editors & IDE Integration
353 * [`language-postcss`] adds PostCSS and [SugarSS] highlight.
354 * [`source-preview-postcss`] previews your output CSS in a separate, live pane.
356 [SugarSS]: https://github.com/postcss/sugarss
360 * [`Syntax-highlighting-for-PostCSS`] adds PostCSS highlight.
362 [`Syntax-highlighting-for-PostCSS`]: https://github.com/hudochenkov/Syntax-highlighting-for-PostCSS
363 [`source-preview-postcss`]: https://atom.io/packages/source-preview-postcss
364 [`language-postcss`]: https://atom.io/packages/language-postcss
368 * [`postcss.vim`] adds PostCSS highlight.
370 [`postcss.vim`]: https://github.com/stephenway/postcss.vim
374 WebStorm 2016.3 [has] built-in PostCSS support.
376 [has]: https://blog.jetbrains.com/webstorm/2016/08/webstorm-2016-3-early-access-preview/