You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

466 lines
16 KiB

4 years ago
  1. # PostCSS [![Gitter][chat-img]][chat]
  2. <img align="right" width="95" height="95"
  3. alt="Philosopher’s stone, logo of PostCSS"
  4. src="http://postcss.github.io/postcss/logo.svg">
  5. [chat-img]: https://img.shields.io/badge/Gitter-Join_the_PostCSS_chat-brightgreen.svg
  6. [chat]: https://gitter.im/postcss/postcss
  7. PostCSS is a tool for transforming styles with JS plugins.
  8. These plugins can lint your CSS, support variables and mixins,
  9. transpile future CSS syntax, inline images, and more.
  10. PostCSS is used by industry leaders including Wikipedia, Twitter, Alibaba,
  11. and JetBrains. The [Autoprefixer] PostCSS plugin is one of the most popular
  12. CSS processors.
  13. PostCSS takes a CSS file and provides an API to analyze and modify its rules
  14. (by transforming them into an [Abstract Syntax Tree]).
  15. This API can then be used by [plugins] to do a lot of useful things,
  16. e.g. to find errors automatically insert vendor prefixes.
  17. **Support / Discussion:** [Gitter](https://gitter.im/postcss/postcss)<br>
  18. **Twitter account:** [@postcss](https://twitter.com/postcss)<br>
  19. **VK.com page:** [postcss](https://vk.com/postcss)<br>
  20. **中文翻译**: [`README-cn.md`](./README-cn.md)
  21. For PostCSS commercial support (consulting, improving the front-end culture
  22. of your company, PostCSS plugins), contact [Evil Martians]
  23. at <surrender@evilmartians.com>.
  24. [Abstract Syntax Tree]: https://en.wikipedia.org/wiki/Abstract_syntax_tree
  25. [Evil Martians]: https://evilmartians.com/?utm_source=postcss
  26. [Autoprefixer]: https://github.com/postcss/autoprefixer
  27. [plugins]: https://github.com/postcss/postcss#plugins
  28. <a href="https://evilmartians.com/?utm_source=postcss">
  29. <img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg"
  30. alt="Sponsored by Evil Martians" width="236" height="54">
  31. </a>
  32. ## Plugins
  33. Currently, PostCSS has more than 200 plugins. You can find all of the plugins
  34. in the [plugins list] or in the [searchable catalog]. Below is a list
  35. of our favorite plugins — the best demonstrations of what can be built
  36. on top of PostCSS.
  37. If you have any new ideas, [PostCSS plugin development] is really easy.
  38. [searchable catalog]: http://postcss.parts
  39. [plugins list]: https://github.com/postcss/postcss/blob/master/docs/plugins.md
  40. ### Solve Global CSS Problem
  41. * [`postcss-use`] allows you to explicitly set PostCSS plugins within CSS
  42. and execute them only for the current file.
  43. * [`postcss-modules`] and [`react-css-modules`] automatically isolate
  44. selectors within components.
  45. * [`postcss-autoreset`] is an alternative to using a global reset
  46. that is better for isolatable components.
  47. * [`postcss-initial`] adds `all: initial` support, which resets
  48. all inherited styles.
  49. * [`cq-prolyfill`] adds container query support, allowing styles that respond
  50. to the width of the parent.
  51. ### Use Future CSS, Today
  52. * [`autoprefixer`] adds vendor prefixes, using data from Can I Use.
  53. * [`postcss-preset-env`] allows you to use future CSS features today.
  54. ### Better CSS Readability
  55. * [`precss`] contains plugins for Sass-like features, like variables, nesting,
  56. and mixins.
  57. * [`postcss-sorting`] sorts the content of rules and at-rules.
  58. * [`postcss-utilities`] includes the most commonly used shortcuts and helpers.
  59. * [`short`] adds and extends numerous shorthand properties.
  60. ### Images and Fonts
  61. * [`postcss-assets`] inserts image dimensions and inlines files.
  62. * [`postcss-sprites`] generates image sprites.
  63. * [`font-magician`] generates all the `@font-face` rules needed in CSS.
  64. * [`postcss-inline-svg`] allows you to inline SVG and customize its styles.
  65. * [`postcss-write-svg`] allows you to write simple SVG directly in your CSS.
  66. ### Linters
  67. * [`stylelint`] is a modular stylesheet linter.
  68. * [`stylefmt`] is a tool that automatically formats CSS
  69. according `stylelint` rules.
  70. * [`doiuse`] lints CSS for browser support, using data from Can I Use.
  71. * [`colorguard`] helps you maintain a consistent color palette.
  72. ### Other
  73. * [`postcss-rtl`] combines both-directional (left-to-right and right-to-left) styles in one CSS file.
  74. * [`cssnano`] is a modular CSS minifier.
  75. * [`lost`] is a feature-rich `calc()` grid system.
  76. * [`rtlcss`] mirrors styles for right-to-left locales.
  77. [PostCSS plugin development]: https://github.com/postcss/postcss/blob/master/docs/writing-a-plugin.md
  78. [`postcss-inline-svg`]: https://github.com/TrySound/postcss-inline-svg
  79. [`postcss-preset-env`]: https://github.com/jonathantneal/postcss-preset-env
  80. [`react-css-modules`]: https://github.com/gajus/react-css-modules
  81. [`postcss-autoreset`]: https://github.com/maximkoretskiy/postcss-autoreset
  82. [`postcss-write-svg`]: https://github.com/jonathantneal/postcss-write-svg
  83. [`postcss-utilities`]: https://github.com/ismamz/postcss-utilities
  84. [`postcss-initial`]: https://github.com/maximkoretskiy/postcss-initial
  85. [`postcss-sprites`]: https://github.com/2createStudio/postcss-sprites
  86. [`postcss-modules`]: https://github.com/outpunk/postcss-modules
  87. [`postcss-sorting`]: https://github.com/hudochenkov/postcss-sorting
  88. [`postcss-assets`]: https://github.com/assetsjs/postcss-assets
  89. [`font-magician`]: https://github.com/jonathantneal/postcss-font-magician
  90. [`autoprefixer`]: https://github.com/postcss/autoprefixer
  91. [`cq-prolyfill`]: https://github.com/ausi/cq-prolyfill
  92. [`postcss-rtl`]: https://github.com/vkalinichev/postcss-rtl
  93. [`postcss-use`]: https://github.com/postcss/postcss-use
  94. [`css-modules`]: https://github.com/css-modules/css-modules
  95. [`colorguard`]: https://github.com/SlexAxton/css-colorguard
  96. [`stylelint`]: https://github.com/stylelint/stylelint
  97. [`stylefmt`]: https://github.com/morishitter/stylefmt
  98. [`cssnano`]: http://cssnano.co
  99. [`precss`]: https://github.com/jonathantneal/precss
  100. [`doiuse`]: https://github.com/anandthakker/doiuse
  101. [`rtlcss`]: https://github.com/MohammadYounes/rtlcss
  102. [`short`]: https://github.com/jonathantneal/postcss-short
  103. [`lost`]: https://github.com/peterramsing/lost
  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-syntax`] switch syntax automatically by file extensions.
  110. * [`postcss-html`] parsing styles in `<style>` tags of HTML-like files.
  111. * [`postcss-markdown`] parsing styles in code blocks of Markdown files.
  112. * [`postcss-jsx`] parsing CSS in template / object literals of source files.
  113. * [`postcss-styled`] parsing CSS in template literals of source files.
  114. * [`postcss-scss`] allows you to work with SCSS
  115. *(but does not compile SCSS to CSS)*.
  116. * [`postcss-sass`] allows you to work with Sass
  117. *(but does not compile Sass to CSS)*.
  118. * [`postcss-less`] allows you to work with Less
  119. *(but does not compile LESS to CSS)*.
  120. * [`postcss-less-engine`] allows you to work with Less
  121. *(and DOES compile LESS to CSS using true Less.js evaluation)*.
  122. * [`postcss-js`] allows you to write styles in JS or transform
  123. React Inline Styles, Radium or JSS.
  124. * [`postcss-safe-parser`] finds and fixes CSS syntax errors.
  125. * [`midas`] converts a CSS string to highlighted HTML.
  126. [`postcss-less-engine`]: https://github.com/Crunch/postcss-less
  127. [`postcss-safe-parser`]: https://github.com/postcss/postcss-safe-parser
  128. [`postcss-syntax`]: https://github.com/gucong3000/postcss-syntax
  129. [`postcss-html`]: https://github.com/gucong3000/postcss-html
  130. [`postcss-markdown`]: https://github.com/gucong3000/postcss-markdown
  131. [`postcss-jsx`]: https://github.com/gucong3000/postcss-jsx
  132. [`postcss-styled`]: https://github.com/gucong3000/postcss-styled
  133. [`postcss-scss`]: https://github.com/postcss/postcss-scss
  134. [`postcss-sass`]: https://github.com/AleshaOleg/postcss-sass
  135. [`postcss-less`]: https://github.com/webschik/postcss-less
  136. [`postcss-js`]: https://github.com/postcss/postcss-js
  137. [`sugarss`]: https://github.com/postcss/sugarss
  138. [`midas`]: https://github.com/ben-eb/midas
  139. ## Articles
  140. * [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)
  141. * [What PostCSS Really Is; What It Really Does](http://davidtheclark.com/its-time-for-everyone-to-learn-about-postcss)
  142. * [PostCSS Guides](http://webdesign.tutsplus.com/series/postcss-deep-dive--cms-889)
  143. More articles and videos you can find on [awesome-postcss](https://github.com/jjaderg/awesome-postcss) list.
  144. ## Books
  145. * [Mastering PostCSS for Web Design](https://www.packtpub.com/web-development/mastering-postcss-web-design) by Alex Libby, Packt. (June 2016)
  146. ## Usage
  147. You can start using PostCSS in just two steps:
  148. 1. Find and add PostCSS extensions for your build tool.
  149. 2. [Select plugins] and add them to your PostCSS process.
  150. [Select plugins]: http://postcss.parts
  151. ### CSS-in-JS
  152. The best way to use PostCSS with CSS-in-JS is [`astroturf`].
  153. Add its loader to your `webpack.config.js`:
  154. ```js
  155. module.exports = {
  156. module: {
  157. rules: [
  158. {
  159. test: /\.css$/,
  160. use: ['style-loader', 'postcss-loader'],
  161. },
  162. {
  163. test: /\.jsx?$/,
  164. use: ['babel-loader', 'astroturf/loader'],
  165. }
  166. ]
  167. }
  168. }
  169. ```
  170. Then create `postcss.config.js`:
  171. ```js
  172. module.exports = {
  173. plugins: [
  174. require('autoprefixer'),
  175. require('postcss-nested')
  176. ]
  177. }
  178. ```
  179. [`astroturf`]: https://github.com/4Catalyzer/astroturf
  180. ### Parcel
  181. [Parcel] has built-in PostCSS support. It already uses Autoprefixer
  182. and cssnano. If you want to change plugins, create `postcss.config.js`
  183. in project’s root:
  184. ```js
  185. module.exports = {
  186. plugins: [
  187. require('autoprefixer'),
  188. require('postcss-nested')
  189. ]
  190. }
  191. ```
  192. Parcel will even automatically install these plugins for you.
  193. > 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).
  194. [Parcel]: https://parceljs.org
  195. ### Webpack
  196. Use [`postcss-loader`] in `webpack.config.js`:
  197. ```js
  198. module.exports = {
  199. module: {
  200. rules: [
  201. {
  202. test: /\.css$/,
  203. exclude: /node_modules/,
  204. use: [
  205. {
  206. loader: 'style-loader',
  207. },
  208. {
  209. loader: 'css-loader',
  210. options: {
  211. importLoaders: 1,
  212. }
  213. },
  214. {
  215. loader: 'postcss-loader'
  216. }
  217. ]
  218. }
  219. ]
  220. }
  221. }
  222. ```
  223. Then create `postcss.config.js`:
  224. ```js
  225. module.exports = {
  226. plugins: [
  227. require('precss'),
  228. require('autoprefixer')
  229. ]
  230. }
  231. ```
  232. [`postcss-loader`]: https://github.com/postcss/postcss-loader
  233. ### Gulp
  234. Use [`gulp-postcss`] and [`gulp-sourcemaps`].
  235. ```js
  236. gulp.task('css', () => {
  237. const postcss = require('gulp-postcss')
  238. const sourcemaps = require('gulp-sourcemaps')
  239. return gulp.src('src/**/*.css')
  240. .pipe( sourcemaps.init() )
  241. .pipe( postcss([ require('precss'), require('autoprefixer') ]) )
  242. .pipe( sourcemaps.write('.') )
  243. .pipe( gulp.dest('build/') )
  244. })
  245. ```
  246. [`gulp-sourcemaps`]: https://github.com/floridoo/gulp-sourcemaps
  247. [`gulp-postcss`]: https://github.com/postcss/gulp-postcss
  248. ### npm run / CLI
  249. To use PostCSS from your command-line interface or with npm scripts
  250. there is [`postcss-cli`].
  251. ```sh
  252. postcss --use autoprefixer -c options.json -o main.css css/*.css
  253. ```
  254. [`postcss-cli`]: https://github.com/postcss/postcss-cli
  255. ### Browser
  256. If you want to compile CSS string in browser (for instance, in live edit
  257. tools like CodePen), just use [Browserify] or [webpack]. They will pack
  258. PostCSS and plugins files into a single file.
  259. To apply PostCSS plugins to React Inline Styles, JSS, Radium
  260. and other [CSS-in-JS], you can use [`postcss-js`] and transforms style objects.
  261. ```js
  262. var postcss = require('postcss-js')
  263. var prefixer = postcss.sync([ require('autoprefixer') ])
  264. prefixer({ display: 'flex' }) //=> { display: ['-webkit-box', '-webkit-flex', '-ms-flexbox', 'flex'] }
  265. ```
  266. [`postcss-js`]: https://github.com/postcss/postcss-js
  267. [Browserify]: http://browserify.org/
  268. [CSS-in-JS]: https://github.com/MicheleBertoli/css-in-js
  269. [webpack]: https://webpack.github.io/
  270. ### Runners
  271. * **Grunt**: [`grunt-postcss`](https://github.com/nDmitry/grunt-postcss)
  272. * **HTML**: [`posthtml-postcss`](https://github.com/posthtml/posthtml-postcss)
  273. * **Stylus**: [`poststylus`](https://github.com/seaneking/poststylus)
  274. * **Rollup**: [`rollup-plugin-postcss`](https://github.com/egoist/rollup-plugin-postcss)
  275. * **Brunch**: [`postcss-brunch`](https://github.com/brunch/postcss-brunch)
  276. * **Broccoli**: [`broccoli-postcss`](https://github.com/jeffjewiss/broccoli-postcss)
  277. * **Meteor**: [`postcss`](https://atmospherejs.com/juliancwirko/postcss)
  278. * **ENB**: [`enb-postcss`](https://github.com/awinogradov/enb-postcss)
  279. * **Taskr**: [`taskr-postcss`](https://github.com/lukeed/taskr/tree/master/packages/postcss)
  280. * **Start**: [`start-postcss`](https://github.com/start-runner/postcss)
  281. * **Connect/Express**: [`postcss-middleware`](https://github.com/jedmao/postcss-middleware)
  282. ### JS API
  283. For other environments, you can use the JS API:
  284. ```js
  285. const autoprefixer = require('autoprefixer')
  286. const postcss = require('postcss')
  287. const precss = require('precss')
  288. const fs = require('fs')
  289. fs.readFile('src/app.css', (err, css) => {
  290. postcss([precss, autoprefixer])
  291. .process(css, { from: 'src/app.css', to: 'dest/app.css' })
  292. .then(result => {
  293. fs.writeFile('dest/app.css', result.css, () => true)
  294. if ( result.map ) {
  295. fs.writeFile('dest/app.css.map', result.map, () => true)
  296. }
  297. })
  298. })
  299. ```
  300. Read the [PostCSS API documentation] for more details about the JS API.
  301. All PostCSS runners should pass [PostCSS Runner Guidelines].
  302. [PostCSS Runner Guidelines]: https://github.com/postcss/postcss/blob/master/docs/guidelines/runner.md
  303. [PostCSS API documentation]: http://api.postcss.org/postcss.html
  304. ### Options
  305. Most PostCSS runners accept two parameters:
  306. * An array of plugins.
  307. * An object of options.
  308. Common options:
  309. * `syntax`: an object providing a syntax parser and a stringifier.
  310. * `parser`: a special syntax parser (for example, [SCSS]).
  311. * `stringifier`: a special syntax output generator (for example, [Midas]).
  312. * `map`: [source map options].
  313. * `from`: the input file name (most runners set it automatically).
  314. * `to`: the output file name (most runners set it automatically).
  315. [source map options]: https://github.com/postcss/postcss/blob/master/docs/source-maps.md
  316. [Midas]: https://github.com/ben-eb/midas
  317. [SCSS]: https://github.com/postcss/postcss-scss
  318. ### Treat Warnings as Errors
  319. In some situations it might be helpful to fail the build on any warning
  320. from PostCSS or one of its plugins. This guarantees that no warnings
  321. go unnoticed, and helps to avoid bugs. While there is no option to enable
  322. treating warnings as errors, it can easily be done
  323. by adding `postcss-fail-on-warn` plugin in the end of PostCSS plugins:
  324. ```js
  325. module.exports = {
  326. plugins: [
  327. require('autoprefixer'),
  328. require('postcss-fail-on-warn')
  329. ]
  330. }
  331. ```
  332. ## Editors & IDE Integration
  333. ### VS Code
  334. * [`csstools.postcss`] adds support for PostCSS, `postcss-preset-env`
  335. and CSS Modules.
  336. [`csstools.postcss`]: https://marketplace.visualstudio.com/items?itemName=csstools.postcss
  337. ### Atom
  338. * [`language-postcss`] adds PostCSS and [SugarSS] highlight.
  339. * [`source-preview-postcss`] previews your output CSS in a separate, live pane.
  340. [SugarSS]: https://github.com/postcss/sugarss
  341. ### Sublime Text
  342. * [`Syntax-highlighting-for-PostCSS`] adds PostCSS highlight.
  343. [`Syntax-highlighting-for-PostCSS`]: https://github.com/hudochenkov/Syntax-highlighting-for-PostCSS
  344. [`source-preview-postcss`]: https://atom.io/packages/source-preview-postcss
  345. [`language-postcss`]: https://atom.io/packages/language-postcss
  346. ### Vim
  347. * [`postcss.vim`] adds PostCSS highlight.
  348. [`postcss.vim`]: https://github.com/stephenway/postcss.vim
  349. ### WebStorm
  350. WebStorm 2016.3 [has] built-in PostCSS support.
  351. [has]: https://blog.jetbrains.com/webstorm/2016/08/webstorm-2016-3-early-access-preview/
  352. ## Security Contact
  353. To report a security vulnerability, please use the [Tidelift security contact].
  354. Tidelift will coordinate the fix and disclosure.
  355. [Tidelift security contact]: https://tidelift.com/security