• Stars
    star
    27,948
  • Rank 638 (Top 0.02 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 10 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Transforming styles with JS plugins

PostCSS

Philosopher’s stone, logo of PostCSS

PostCSS is a tool for transforming styles with JS plugins. These plugins can lint your CSS, support variables and mixins, transpile future CSS syntax, inline images, and more.

PostCSS is used by industry leaders including Wikipedia, Twitter, Alibaba, and JetBrains. The Autoprefixer and Stylelint PostCSS plugins is one of the most popular CSS tools.


  Made in Evil Martians, product consulting for developer tools.


Sponsorship

PostCSS needs your support. We are accepting donations at Open Collective.

Sponsored by Tailwind CSS        Sponsored by ThemeIsle

Plugins

PostCSS takes a CSS file and provides an API to analyze and modify its rules (by transforming them into an Abstract Syntax Tree). This API can then be used by plugins to do a lot of useful things, e.g., to find errors automatically, or to insert vendor prefixes.

Currently, PostCSS has more than 200 plugins. You can find all of the plugins in the plugins list or in the searchable catalog. Below is a list of our favorite plugins — the best demonstrations of what can be built on top of PostCSS.

If you have any new ideas, PostCSS plugin development is really easy.

Solve Global CSS Problem

  • postcss-use allows you to explicitly set PostCSS plugins within CSS and execute them only for the current file.
  • postcss-modules and react-css-modules automatically isolate selectors within components.
  • postcss-autoreset is an alternative to using a global reset that is better for isolatable components.
  • postcss-initial adds all: initial support, which resets all inherited styles.
  • cq-prolyfill adds container query support, allowing styles that respond to the width of the parent.

Use Future CSS, Today

Better CSS Readability

Images and Fonts

Linters

  • stylelint is a modular stylesheet linter.
  • stylefmt is a tool that automatically formats CSS according stylelint rules.
  • doiuse lints CSS for browser support, using data from Can I Use.
  • colorguard helps you maintain a consistent color palette.

Other

  • cssnano is a modular CSS minifier.
  • lost is a feature-rich calc() grid system.
  • rtlcss mirrors styles for right-to-left locales.

Syntaxes

PostCSS can transform styles in any syntax, not just CSS. If there is not yet support for your favorite syntax, you can write a parser and/or stringifier to extend PostCSS.

  • sugarss is a indent-based syntax like Sass or Stylus.
  • postcss-syntax switch syntax automatically by file extensions.
  • postcss-html parsing styles in <style> tags of HTML-like files.
  • postcss-markdown parsing styles in code blocks of Markdown files.
  • postcss-styled-syntax parses styles in template literals CSS-in-JS like styled-components.
  • postcss-jsx parsing CSS in template / object literals of source files.
  • postcss-styled parsing CSS in template literals of source files.
  • postcss-scss allows you to work with SCSS (but does not compile SCSS to CSS).
  • postcss-sass allows you to work with Sass (but does not compile Sass to CSS).
  • postcss-less allows you to work with Less (but does not compile LESS to CSS).
  • postcss-less-engine allows you to work with Less (and DOES compile LESS to CSS using true Less.js evaluation).
  • postcss-js allows you to write styles in JS or transform React Inline Styles, Radium or JSS.
  • postcss-safe-parser finds and fixes CSS syntax errors.
  • midas converts a CSS string to highlighted HTML.

Articles

More articles and videos you can find on awesome-postcss list.

Books

Usage

You can start using PostCSS in just two steps:

  1. Find and add PostCSS extensions for your build tool.
  2. Select plugins and add them to your PostCSS process.

CSS-in-JS

The best way to use PostCSS with CSS-in-JS is astroturf. Add its loader to your webpack.config.js:

module.exports = {
  module: {
    rules: [
      {
        test: /\.css$/,
        use: ['style-loader', 'postcss-loader'],
      },
      {
        test: /\.jsx?$/,
        use: ['babel-loader', 'astroturf/loader'],
      }
    ]
  }
}

Then create postcss.config.js:

/** @type {import('postcss-load-config').Config} */
const config = {
  plugins: [
    require('autoprefixer'),
    require('postcss-nested')
  ]
}

module.exports = config

Parcel

Parcel has built-in PostCSS support. It already uses Autoprefixer and cssnano. If you want to change plugins, create postcss.config.js in project’s root:

/** @type {import('postcss-load-config').Config} */
const config = {
  plugins: [
    require('autoprefixer'),
    require('postcss-nested')
  ]
}

module.exports = config

Parcel will even automatically install these plugins for you.

Please, be aware of the several issues in Version 1. Notice, Version 2 may resolve the issues via issue #2157.

Webpack

Use postcss-loader in webpack.config.js:

module.exports = {
  module: {
    rules: [
      {
        test: /\.css$/,
        exclude: /node_modules/,
        use: [
          {
            loader: 'style-loader',
          },
          {
            loader: 'css-loader',
            options: {
              importLoaders: 1,
            }
          },
          {
            loader: 'postcss-loader'
          }
        ]
      }
    ]
  }
}

Then create postcss.config.js:

/** @type {import('postcss-load-config').Config} */
const config = {
  plugins: [
    require('autoprefixer'),
    require('postcss-nested')
  ]
}

module.exports = config

Gulp

Use gulp-postcss and gulp-sourcemaps.

gulp.task('css', () => {
  const postcss    = require('gulp-postcss')
  const sourcemaps = require('gulp-sourcemaps')

  return gulp.src('src/**/*.css')
    .pipe( sourcemaps.init() )
    .pipe( postcss([ require('autoprefixer'), require('postcss-nested') ]) )
    .pipe( sourcemaps.write('.') )
    .pipe( gulp.dest('build/') )
})

npm Scripts

To use PostCSS from your command-line interface or with npm scripts there is postcss-cli.

postcss --use autoprefixer -o main.css css/*.css

Browser

If you want to compile CSS string in browser (for instance, in live edit tools like CodePen), just use Browserify or webpack. They will pack PostCSS and plugins files into a single file.

To apply PostCSS plugins to React Inline Styles, JSS, Radium and other CSS-in-JS, you can use postcss-js and transforms style objects.

const postcss  = require('postcss-js')
const prefixer = postcss.sync([ require('autoprefixer') ])

prefixer({ display: 'flex' }) //=> { display: ['-webkit-box', '-webkit-flex', '-ms-flexbox', 'flex'] }

Runners

JS API

For other environments, you can use the JS API:

const autoprefixer = require('autoprefixer')
const postcss = require('postcss')
const postcssNested = require('postcss-nested')
const fs = require('fs')

fs.readFile('src/app.css', (err, css) => {
  postcss([autoprefixer, postcssNested])
    .process(css, { from: 'src/app.css', to: 'dest/app.css' })
    .then(result => {
      fs.writeFile('dest/app.css', result.css, () => true)
      if ( result.map ) {
        fs.writeFile('dest/app.css.map', result.map.toString(), () => true)
      }
    })
})

Read the PostCSS API documentation for more details about the JS API.

All PostCSS runners should pass PostCSS Runner Guidelines.

Options

Most PostCSS runners accept two parameters:

  • An array of plugins.
  • An object of options.

Common options:

  • syntax: an object providing a syntax parser and a stringifier.
  • parser: a special syntax parser (for example, SCSS).
  • stringifier: a special syntax output generator (for example, Midas).
  • map: source map options.
  • from: the input file name (most runners set it automatically).
  • to: the output file name (most runners set it automatically).

Treat Warnings as Errors

In some situations it might be helpful to fail the build on any warning from PostCSS or one of its plugins. This guarantees that no warnings go unnoticed, and helps to avoid bugs. While there is no option to enable treating warnings as errors, it can easily be done by adding postcss-fail-on-warn plugin in the end of PostCSS plugins:

module.exports = {
  plugins: [
    require('autoprefixer'),
    require('postcss-fail-on-warn')
  ]
}

Editors & IDE Integration

VS Code

Sublime Text

Vim

WebStorm

To get support for PostCSS in WebStorm and other JetBrains IDEs you need to install this plugin.

Security Contact

To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.

For Enterprise

Available as part of the Tidelift Subscription.

The maintainers of postcss and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more.

More Repositories

1

autoprefixer

Parse CSS and add vendor prefixes to rules by Can I Use
JavaScript
21,437
star
2

postcss-import

PostCSS plugin to inline at-import rules content
JavaScript
1,352
star
3

postcss-nested

PostCSS plugin to unwrap nested rules like how Sass does it.
JavaScript
1,108
star
4

postcss-100vh-fix

PostCSS plugin to fix height/min-height: 100vh on iOS
JavaScript
911
star
5

postcss-cli

CLI for postcss
JavaScript
808
star
6

gulp-postcss

Pipe CSS through PostCSS processors with a single parse
JavaScript
771
star
7

sugarss

Indent-based CSS syntax for PostCSS
JavaScript
698
star
8

postcss-js

PostCSS for React Inline Styles, Free Style and other CSS-in-JS
JavaScript
643
star
9

postcss-scss

SCSS parser for PostCSS.
JavaScript
635
star
10

postcss-load-config

Autoload Config for PostCSS
JavaScript
618
star
11

postcss-custom-properties

Use Custom Properties in CSS
JavaScript
598
star
12

postcss-bem-linter

A BEM linter for postcss
JavaScript
561
star
13

postcss-mixins

PostCSS plugin for mixins
JavaScript
446
star
14

postcss-simple-vars

PostCSS plugin for Sass-like variables
JavaScript
409
star
15

postcss-url

PostCSS plugin to rebase url(), inline or copy asset.
JavaScript
373
star
16

postcss-color-function

PostCSS plugin to transform W3C CSS color function to more compatible CSS
JavaScript
323
star
17

postcss-media-minmax

Writing simple and graceful Media Queries!
JavaScript
290
star
18

postcss-plugin-boilerplate

PostCSS Plugin Boilerplate
JavaScript
220
star
19

postcss-calc

PostCSS plugin to reduce calc()
JavaScript
208
star
20

postcss-selector-parser

A CSS selector parser, integrates with postcss but does not require it.
JavaScript
196
star
21

postcss-reporter

Log PostCSS messages in the console
JavaScript
154
star
22

postcss-use

Enable PostCSS plugins directly in your stylesheet.
JavaScript
148
star
23

postcss-dark-theme-class

PostCSS plugin to make dark/light theme switcher by copying styles from media query to special class
JavaScript
147
star
24

postcss-easings

PostCSS plugin to replace easing names to cubic-bezier()
JavaScript
144
star
25

postcss-focus

PostCSS plugin to add :focus selector to every :hover for keyboard accessibility
JavaScript
116
star
26

postcss-safe-parser

Fault tolerant CSS parser for PostCSS
JavaScript
115
star
27

benchmark

PostCSS benchmarks
JavaScript
114
star
28

postcss-devtools

Log execution time for each plugin in a PostCSS instance.
JavaScript
92
star
29

postcss.org

Official website for PostCSS
JavaScript
81
star
30

postcss-browser-reporter

Plugin to display warning messages right in your browser
JavaScript
75
star
31

postcss-color-rebeccapurple

PostCSS plugin to transform rebeccapurple color to rgb()
JavaScript
62
star
32

postcss-deno

Postcss for Deno
JavaScript
56
star
33

postcss-brand-colors

PostCSS plugin to insert branding colors of all the major companies
JavaScript
54
star
34

postcss-size

PostCSS plugin for size shortcut
JavaScript
52
star
35

postcss-will-change

PostCSS plugin to insert 3D hack before will-change property
JavaScript
51
star
36

postcss-color-rgba-fallback

PostCSS plugin to transform rgba() to hexadecimal.
JavaScript
50
star
37

postcss-selector-matches

PostCSS plugin to transform :matches() W3C CSS pseudo class to more compatible CSS (simpler selectors)
JavaScript
44
star
38

postcss-plugin-context

Limit a PostCSS processor to a local stylesheet context.
JavaScript
33
star
39

postcss-color-hex-alpha

Use 4 & 8 character hex color notation in CSS
JavaScript
28
star
40

postcss-color-gray

Use the gray() color function in CSS
JavaScript
27
star
41

postcss-font-variant

PostCSS plugin to transform W3C CSS font variant properties to more compatible CSS (font-feature-settings)
JavaScript
25
star
42

brand

PostCSS branding files
JavaScript
24
star
43

postcss-filter-plugins

Exclude/warn on duplicated PostCSS plugins.
JavaScript
23
star
44

postcss-parser-tests

Base tests for every PostCSS CSS parser
JavaScript
21
star
45

postcss-color-hwb

PostCSS plugin to transform W3C CSS hwb() function to more compatible CSS (rgb() or rgba()).
JavaScript
21
star
46

eslint-config-postcss

An ESLint shareable config for postcss and plugins
JavaScript
18
star
47

postcss-plugin-suggestion-box

Suggestion box for PostCSS plugins
14
star
48

postcss-relative-opacity

PostCSS plugin to add opacity to any colors with Relative Color Syntax
JavaScript
11
star
49

fly-postcss

UNMAINTAINED
10
star
50

postcss-fail-on-warn

PostCSS plugin throws a error on any warning from previous PostCSS plugins.
JavaScript
8
star
51

postcss-sharec-config

Best parctices and configs from PostCSS plugin
7
star
52

postcss-at-rule-parser

A modern CSS at rule parser in PostCSS.
TypeScript
5
star
53

postcss-color

DEPRECATED - PostCSS plugin to transform latest W3C CSS color module syntax to more compatible CSS
JavaScript
5
star
54

postcss-deno-import

postcss-import plugin for Deno
JavaScript
4
star