UNPKG

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