UNPKG

18 kBMarkdownView Raw
1# PostCSS [![Gitter][chat-img]][chat]
2
3<img align="right" width="95" height="95"
4 alt="Philosopher’s stone, logo of PostCSS"
5 src="https://postcss.org/logo.svg">
6
7[chat-img]: https://img.shields.io/badge/Gitter-Join_the_PostCSS_chat-brightgreen.svg
8[chat]: https://gitter.im/postcss/postcss
9
10PostCSS is a tool for transforming styles with JS plugins.
11These plugins can lint your CSS, support variables and mixins,
12transpile future CSS syntax, inline images, and more.
13
14PostCSS is used by industry leaders including Wikipedia, Twitter, Alibaba,
15and JetBrains. The [Autoprefixer] PostCSS plugin is one of the most popular
16CSS processors.
17
18PostCSS takes a CSS file and provides an API to analyze and modify its rules
19(by transforming them into an [Abstract Syntax Tree]).
20This API can then be used by [plugins] to do a lot of useful things,
21e.g., to find errors automatically, or to insert vendor prefixes.
22
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**中文翻译**: [`docs/README-cn.md`](./docs/README-cn.md)
27
28For PostCSS commercial support (consulting, improving the front-end culture
29of your company, PostCSS plugins), contact [Evil Martians]
30at <postcss@evilmartians.com>.
31
32[Abstract Syntax Tree]: https://en.wikipedia.org/wiki/Abstract_syntax_tree
33[Evil Martians]: https://evilmartians.com/?utm_source=postcss
34[Autoprefixer]: https://github.com/postcss/autoprefixer
35[plugins]: https://github.com/postcss/postcss#plugins
36
37<a href="https://evilmartians.com/?utm_source=postcss">
38 <img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg"
39 alt="Sponsored by Evil Martians" width="236" height="54">
40</a>
41
42
43## Sponsorship
44
45PostCSS needs your support. We are accepting donations
46[at Open Collective](https://opencollective.com/postcss/).
47
48<a href="https://tailwindcss.com/">
49 <img src="https://refactoringui.nyc3.cdn.digitaloceanspaces.com/tailwind-logo.svg"
50 alt="Sponsored by Tailwind CSS" width="273" height="64">
51</a>
52
53
54## Plugins
55
56Currently, PostCSS has more than 200 plugins. You can find all of the plugins
57in the [plugins list] or in the [searchable catalog]. Below is a list
58of our favorite plugins — the best demonstrations of what can be built
59on top of PostCSS.
60
61If you have any new ideas, [PostCSS plugin development] is really easy.
62
63[searchable catalog]: https://www.postcss.parts/
64[plugins list]: https://github.com/postcss/postcss/blob/master/docs/plugins.md
65
66
67### Solve Global CSS Problem
68
69* [`postcss-use`] allows you to explicitly set PostCSS plugins within CSS
70 and execute them only for the current file.
71* [`postcss-modules`] and [`react-css-modules`] automatically isolate
72 selectors within components.
73* [`postcss-autoreset`] is an alternative to using a global reset
74 that is better for isolatable components.
75* [`postcss-initial`] adds `all: initial` support, which resets
76 all inherited styles.
77* [`cq-prolyfill`] adds container query support, allowing styles that respond
78 to the width of the parent.
79
80
81### Use Future CSS, Today
82
83* [`autoprefixer`] adds vendor prefixes, using data from Can I Use.
84* [`postcss-preset-env`] allows you to use future CSS features today.
85
86
87### Better CSS Readability
88
89* [`precss`] contains plugins for Sass-like features, like variables, nesting,
90 and mixins.
91* [`postcss-sorting`] sorts the content of rules and at-rules.
92* [`postcss-utilities`] includes the most commonly used shortcuts and helpers.
93* [`short`] adds and extends numerous shorthand properties.
94
95
96### Images and Fonts
97
98* [`postcss-assets`] inserts image dimensions and inlines files.
99* [`postcss-sprites`] generates image sprites.
100* [`font-magician`] generates all the `@font-face` rules needed in CSS.
101* [`postcss-inline-svg`] allows you to inline SVG and customize its styles.
102* [`postcss-write-svg`] allows you to write simple SVG directly in your CSS.
103
104
105### Linters
106
107* [`stylelint`] is a modular stylesheet linter.
108* [`stylefmt`] is a tool that automatically formats CSS
109 according `stylelint` rules.
110* [`doiuse`] lints CSS for browser support, using data from Can I Use.
111* [`colorguard`] helps you maintain a consistent color palette.
112
113
114### Other
115
116* [`postcss-rtl`] combines both-directional (left-to-right and right-to-left) styles in one CSS file.
117* [`cssnano`] is a modular CSS minifier.
118* [`lost`] is a feature-rich `calc()` grid system.
119* [`rtlcss`] mirrors styles for right-to-left locales.
120
121[PostCSS plugin development]: https://github.com/postcss/postcss/blob/master/docs/writing-a-plugin.md
122[`postcss-inline-svg`]: https://github.com/TrySound/postcss-inline-svg
123[`postcss-preset-env`]: https://github.com/jonathantneal/postcss-preset-env
124[`react-css-modules`]: https://github.com/gajus/react-css-modules
125[`postcss-autoreset`]: https://github.com/maximkoretskiy/postcss-autoreset
126[`postcss-write-svg`]: https://github.com/jonathantneal/postcss-write-svg
127[`postcss-utilities`]: https://github.com/ismamz/postcss-utilities
128[`postcss-initial`]: https://github.com/maximkoretskiy/postcss-initial
129[`postcss-sprites`]: https://github.com/2createStudio/postcss-sprites
130[`postcss-modules`]: https://github.com/outpunk/postcss-modules
131[`postcss-sorting`]: https://github.com/hudochenkov/postcss-sorting
132[`postcss-assets`]: https://github.com/assetsjs/postcss-assets
133[`font-magician`]: https://github.com/jonathantneal/postcss-font-magician
134[`autoprefixer`]: https://github.com/postcss/autoprefixer
135[`cq-prolyfill`]: https://github.com/ausi/cq-prolyfill
136[`postcss-rtl`]: https://github.com/vkalinichev/postcss-rtl
137[`postcss-use`]: https://github.com/postcss/postcss-use
138[`css-modules`]: https://github.com/css-modules/css-modules
139[`colorguard`]: https://github.com/SlexAxton/css-colorguard
140[`stylelint`]: https://github.com/stylelint/stylelint
141[`stylefmt`]: https://github.com/morishitter/stylefmt
142[`cssnano`]: https://cssnano.co/
143[`precss`]: https://github.com/jonathantneal/precss
144[`doiuse`]: https://github.com/anandthakker/doiuse
145[`rtlcss`]: https://github.com/MohammadYounes/rtlcss
146[`short`]: https://github.com/jonathantneal/postcss-short
147[`lost`]: https://github.com/peterramsing/lost
148
149
150## Syntaxes
151
152PostCSS can transform styles in any syntax, not just CSS.
153If there is not yet support for your favorite syntax,
154you can write a parser and/or stringifier to extend PostCSS.
155
156* [`sugarss`] is a indent-based syntax like Sass or Stylus.
157* [`postcss-syntax`] switch syntax automatically by file extensions.
158* [`postcss-html`] parsing styles in `<style>` tags of HTML-like files.
159* [`postcss-markdown`] parsing styles in code blocks of Markdown files.
160* [`postcss-jsx`] parsing CSS in template / object literals of source files.
161* [`postcss-styled`] parsing CSS in template literals of source files.
162* [`postcss-scss`] allows you to work with SCSS
163 *(but does not compile SCSS to CSS)*.
164* [`postcss-sass`] allows you to work with Sass
165 *(but does not compile Sass to CSS)*.
166* [`postcss-less`] allows you to work with Less
167 *(but does not compile LESS to CSS)*.
168* [`postcss-less-engine`] allows you to work with Less
169 *(and DOES compile LESS to CSS using true Less.js evaluation)*.
170* [`postcss-js`] allows you to write styles in JS or transform
171 React Inline Styles, Radium or JSS.
172* [`postcss-safe-parser`] finds and fixes CSS syntax errors.
173* [`midas`] converts a CSS string to highlighted HTML.
174
175[`postcss-less-engine`]: https://github.com/Crunch/postcss-less
176[`postcss-safe-parser`]: https://github.com/postcss/postcss-safe-parser
177[`postcss-syntax`]: https://github.com/gucong3000/postcss-syntax
178[`postcss-html`]: https://github.com/gucong3000/postcss-html
179[`postcss-markdown`]: https://github.com/gucong3000/postcss-markdown
180[`postcss-jsx`]: https://github.com/gucong3000/postcss-jsx
181[`postcss-styled`]: https://github.com/gucong3000/postcss-styled
182[`postcss-scss`]: https://github.com/postcss/postcss-scss
183[`postcss-sass`]: https://github.com/AleshaOleg/postcss-sass
184[`postcss-less`]: https://github.com/webschik/postcss-less
185[`postcss-js`]: https://github.com/postcss/postcss-js
186[`sugarss`]: https://github.com/postcss/sugarss
187[`midas`]: https://github.com/ben-eb/midas
188
189
190## Articles
191
192* [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)
193* [What PostCSS Really Is; What It Really Does](https://davidtheclark.com/its-time-for-everyone-to-learn-about-postcss/)
194* [PostCSS Guides](https://webdesign.tutsplus.com/series/postcss-deep-dive--cms-889)
195
196More articles and videos you can find on [awesome-postcss](https://github.com/jjaderg/awesome-postcss) list.
197
198
199## Books
200
201* [Mastering PostCSS for Web Design](https://www.packtpub.com/web-development/mastering-postcss-web-design) by Alex Libby, Packt. (June 2016)
202
203
204## Usage
205
206You can start using PostCSS in just two steps:
207
2081. Find and add PostCSS extensions for your build tool.
2092. [Select plugins] and add them to your PostCSS process.
210
211[Select plugins]: https://www.postcss.parts/
212
213
214### CSS-in-JS
215
216The best way to use PostCSS with CSS-in-JS is [`astroturf`].
217Add its loader to your `webpack.config.js`:
218
219```js
220module.exports = {
221 module: {
222 rules: [
223 {
224 test: /\.css$/,
225 use: ['style-loader', 'postcss-loader'],
226 },
227 {
228 test: /\.jsx?$/,
229 use: ['babel-loader', 'astroturf/loader'],
230 }
231 ]
232 }
233}
234```
235
236Then create `postcss.config.js`:
237
238```js
239module.exports = {
240 plugins: [
241 require('autoprefixer'),
242 require('postcss-nested')
243 ]
244}
245```
246
247[`astroturf`]: https://github.com/4Catalyzer/astroturf
248
249
250### Parcel
251
252[Parcel] has built-in PostCSS support. It already uses Autoprefixer
253and cssnano. If you want to change plugins, create `postcss.config.js`
254in project’s root:
255
256```js
257module.exports = {
258 plugins: [
259 require('autoprefixer'),
260 require('postcss-nested')
261 ]
262}
263```
264
265Parcel will even automatically install these plugins for you.
266
267> Please, be aware of [the several issues in Version 1](https://github.com/parcel-bundler/parcel/labels/CSS%20Preprocessing). Notice, [Version 2](https://github.com/parcel-bundler/parcel/projects/5) may resolve the issues via [issue #2157](https://github.com/parcel-bundler/parcel/issues/2157).
268
269[Parcel]: https://parceljs.org
270
271
272### Webpack
273
274Use [`postcss-loader`] in `webpack.config.js`:
275
276```js
277module.exports = {
278 module: {
279 rules: [
280 {
281 test: /\.css$/,
282 exclude: /node_modules/,
283 use: [
284 {
285 loader: 'style-loader',
286 },
287 {
288 loader: 'css-loader',
289 options: {
290 importLoaders: 1,
291 }
292 },
293 {
294 loader: 'postcss-loader'
295 }
296 ]
297 }
298 ]
299 }
300}
301```
302
303Then create `postcss.config.js`:
304
305```js
306module.exports = {
307 plugins: [
308 require('precss'),
309 require('autoprefixer')
310 ]
311}
312```
313
314[`postcss-loader`]: https://github.com/postcss/postcss-loader
315
316
317### Gulp
318
319Use [`gulp-postcss`] and [`gulp-sourcemaps`].
320
321```js
322gulp.task('css', () => {
323 const postcss = require('gulp-postcss')
324 const sourcemaps = require('gulp-sourcemaps')
325
326 return gulp.src('src/**/*.css')
327 .pipe( sourcemaps.init() )
328 .pipe( postcss([ require('precss'), require('autoprefixer') ]) )
329 .pipe( sourcemaps.write('.') )
330 .pipe( gulp.dest('build/') )
331})
332```
333
334[`gulp-sourcemaps`]: https://github.com/floridoo/gulp-sourcemaps
335[`gulp-postcss`]: https://github.com/postcss/gulp-postcss
336
337
338### npm Scripts
339
340To use PostCSS from your command-line interface or with npm scripts
341there is [`postcss-cli`].
342
343```sh
344postcss --use autoprefixer -c options.json -o main.css css/*.css
345```
346
347[`postcss-cli`]: https://github.com/postcss/postcss-cli
348
349
350### Browser
351
352If you want to compile CSS string in browser (for instance, in live edit
353tools like CodePen), just use [Browserify] or [webpack]. They will pack
354PostCSS and plugins files into a single file.
355
356To apply PostCSS plugins to React Inline Styles, JSS, Radium
357and other [CSS-in-JS], you can use [`postcss-js`] and transforms style objects.
358
359```js
360const postcss = require('postcss-js')
361const prefixer = postcss.sync([ require('autoprefixer') ])
362
363prefixer({ display: 'flex' }) //=> { display: ['-webkit-box', '-webkit-flex', '-ms-flexbox', 'flex'] }
364```
365
366[`postcss-js`]: https://github.com/postcss/postcss-js
367[Browserify]: http://browserify.org/
368[CSS-in-JS]: https://github.com/MicheleBertoli/css-in-js
369[webpack]: https://webpack.github.io/
370
371
372### Deno
373
374PostCSS also supports [Deno]:
375
376```js
377import postcss from 'https://deno.land/x/postcss/mod.js'
378import autoprefixer from 'https://dev.jspm.io/autoprefixer'
379
380const result = await postcss([autoprefixer]).process(css)
381```
382
383[Deno]: https://deno.land/
384
385
386### Runners
387
388* **Grunt**: [`@lodder/grunt-postcss`](https://github.com/C-Lodder/grunt-postcss)
389* **HTML**: [`posthtml-postcss`](https://github.com/posthtml/posthtml-postcss)
390* **Stylus**: [`poststylus`](https://github.com/seaneking/poststylus)
391* **Rollup**: [`rollup-plugin-postcss`](https://github.com/egoist/rollup-plugin-postcss)
392* **Brunch**: [`postcss-brunch`](https://github.com/brunch/postcss-brunch)
393* **Broccoli**: [`broccoli-postcss`](https://github.com/jeffjewiss/broccoli-postcss)
394* **Meteor**: [`postcss`](https://atmospherejs.com/juliancwirko/postcss)
395* **ENB**: [`enb-postcss`](https://github.com/awinogradov/enb-postcss)
396* **Taskr**: [`taskr-postcss`](https://github.com/lukeed/taskr/tree/master/packages/postcss)
397* **Start**: [`start-postcss`](https://github.com/start-runner/postcss)
398* **Connect/Express**: [`postcss-middleware`](https://github.com/jedmao/postcss-middleware)
399
400
401### JS API
402
403For other environments, you can use the JS API:
404
405```js
406const autoprefixer = require('autoprefixer')
407const postcss = require('postcss')
408const precss = require('precss')
409const fs = require('fs')
410
411fs.readFile('src/app.css', (err, css) => {
412 postcss([precss, autoprefixer])
413 .process(css, { from: 'src/app.css', to: 'dest/app.css' })
414 .then(result => {
415 fs.writeFile('dest/app.css', result.css, () => true)
416 if ( result.map ) {
417 fs.writeFile('dest/app.css.map', result.map, () => true)
418 }
419 })
420})
421```
422
423Read the [PostCSS API documentation] for more details about the JS API.
424
425All PostCSS runners should pass [PostCSS Runner Guidelines].
426
427[PostCSS Runner Guidelines]: https://github.com/postcss/postcss/blob/master/docs/guidelines/runner.md
428[PostCSS API documentation]: https://postcss.org/api/
429
430
431### Options
432
433Most PostCSS runners accept two parameters:
434
435* An array of plugins.
436* An object of options.
437
438Common options:
439
440* `syntax`: an object providing a syntax parser and a stringifier.
441* `parser`: a special syntax parser (for example, [SCSS]).
442* `stringifier`: a special syntax output generator (for example, [Midas]).
443* `map`: [source map options].
444* `from`: the input file name (most runners set it automatically).
445* `to`: the output file name (most runners set it automatically).
446
447[source map options]: https://postcss.org/api/#sourcemapoptions
448[Midas]: https://github.com/ben-eb/midas
449[SCSS]: https://github.com/postcss/postcss-scss
450
451
452### Treat Warnings as Errors
453
454In some situations it might be helpful to fail the build on any warning
455from PostCSS or one of its plugins. This guarantees that no warnings
456go unnoticed, and helps to avoid bugs. While there is no option to enable
457treating warnings as errors, it can easily be done
458by adding `postcss-fail-on-warn` plugin in the end of PostCSS plugins:
459
460```js
461module.exports = {
462 plugins: [
463 require('autoprefixer'),
464 require('postcss-fail-on-warn')
465 ]
466}
467```
468
469
470## Editors & IDE Integration
471
472
473### VS Code
474
475* [`csstools.postcss`] adds support for PostCSS, `postcss-preset-env`
476 and CSS Modules.
477
478[`csstools.postcss`]: https://marketplace.visualstudio.com/items?itemName=csstools.postcss
479
480
481### Atom
482
483* [`language-postcss`] adds PostCSS and [SugarSS] highlight.
484* [`source-preview-postcss`] previews your output CSS in a separate, live pane.
485
486[SugarSS]: https://github.com/postcss/sugarss
487
488
489### Sublime Text
490
491* [`Syntax-highlighting-for-PostCSS`] adds PostCSS highlight.
492
493[`Syntax-highlighting-for-PostCSS`]: https://github.com/hudochenkov/Syntax-highlighting-for-PostCSS
494[`source-preview-postcss`]: https://atom.io/packages/source-preview-postcss
495[`language-postcss`]: https://atom.io/packages/language-postcss
496
497
498### Vim
499
500* [`postcss.vim`] adds PostCSS highlight.
501
502[`postcss.vim`]: https://github.com/stephenway/postcss.vim
503
504
505### WebStorm
506
507WebStorm 2016.3 [has] built-in PostCSS support.
508
509[has]: https://blog.jetbrains.com/webstorm/2016/08/webstorm-2016-3-early-access-preview/
510
511
512## Security Contact
513
514To report a security vulnerability, please use the [Tidelift security contact].
515Tidelift will coordinate the fix and disclosure.
516
517[Tidelift security contact]: https://tidelift.com/security
518
519
520## For Enterprise
521
522Available as part of the Tidelift Subscription.
523
524The maintainers of `postcss` and thousands of other packages are working
525with Tidelift to deliver commercial support and maintenance for the open source
526dependencies you use to build your applications. Save time, reduce risk,
527and improve code health, while paying the maintainers of the exact dependencies
528you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-postcss?utm_source=npm-postcss&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)