Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Transforming CSS with JS plugins
JavaScript CSS

Merge pull request #326 from andyjansson/master

Add postcss-conditionals plugin
latest commit 3be8940729
@ai ai authored

README.md

PostCSS Build Status Gitter

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

PostCSS is used by Google, Twitter, Alibaba, and Shopify. Its plugin, Autoprefixer, is one of the most popular CSS processors.

PostCSS can do the same work as preprocessors like Sass, Less, and Stylus. But PostCSS is modular, 3-30 times faster, and much more powerful.

Twitter account: @postcss. Weibo account: postcss. VK.com page: postcss.

Examples Features Usage Plugins Write Own Plugin Options

Sponsored by Evil Martians

What is PostCSS

PostCSS itself is very small. It includes only a CSS parser, a CSS node tree API, a source map generator, and a node tree stringifier.

All CSS transformations are made by plugins. And these plugins are just small plain JS functions, which receive a CSS node tree, transform it, and return a modified tree.

You can use the cssnext plugin pack and write future CSS code right now:

:root {
    --mainColor: #ffbbaaff;
}
@custom-media    --mobile (width <= 640px);
@custom-selector --heading h1, h2, h3, h4, h5, h6;

.post-article --heading {
    color: color( var(--mainColor) blackness(+20%) );
}
@media (--mobile) {
    .post-article --heading {
        margin-top: 0;
    }
}

Or if you like the Sass syntax, you could combine postcss-nested and postcss-mixins:

@define-mixin social-icon $network $color {
    &.is-$network {
        background: $color;
    }
}

.social-icon {
    @mixin social-icon twitter  #55acee;
    @mixin social-icon facebook #3b5998;

    padding: 10px 5px;
    @media (max-width: 640px) {
        padding: 0;
    }
}

Features

Preprocessors are template languages, where you mix styles with code (like PHP does with HTML). In contrast, in PostCSS you write a custom subset of CSS. All code can only be in JS plugins.

As a result, PostCSS offers three main benefits:

  • Performance: PostCSS, written in JS, is 3 times faster than libsass, which is written in C++.
  • Future CSS: PostCSS plugins can read and rebuild an entire document, meaning that they can provide new language features. For example, cssnext transpiles the latest W3C drafts to current CSS syntax.
  • New abilities: PostCSS plugins can read and change every part of CSS. It makes many new classes of tools possible. Autoprefixer, doiuse or postcss-colorblind are good examples.

Usage

You just need to follow these two steps to use PostCSS:

  1. Add PostCSS to your build tool.
  2. Select plugins from the list below and add them to your PostCSS process.

There are plugins for Grunt, Gulp, webpackBroccoli and Brunch.

gulp.task('css', function () {
    var postcss = require('gulp-postcss');
    return gulp.src('src/**/*.css')
        .pipe( postcss([ require('cssnext'), require('cssgrace') ]) )
        .pipe( gulp.dest('build/') );
});

For other environments you can use the CLI tool or the JS API:

var postcss = require('postcss');
postcss([ require('cssnext'), require('cssgrace') ])
    .process(css, { from: 'src/app.css', to: 'app.css' })
    .then(function (result) {
        fs.writeFileSync('app.css', result.css);
        if ( result.map ) fs.writeFileSync('app.css.map', result.map);
    });

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

Plugins

Control

With postcss-plugin-context you can run different plugins on different parts of CSS.

Packs

  • cssnano contains plugins that optimise CSS size for use in production.
  • cssnext contains plugins that allow you to use future CSS features today.
  • atcss contains plugins that transform your CSS according to special annotation comments.

Future CSS Syntax

Fallbacks

Language Extensions

Optimizations

Shortcuts

Others

Analysis

  • postcss-bem-linter lints CSS for conformance to SUIT CSS methodology.
  • css2modernizr creates a Modernizr config file that requires only the tests that your CSS uses.
  • doiuse lints CSS for browser support, using data from Can I Use.
  • list-selectors lists and categorizes the selectors used in your CSS, for code review.

How to Develop PostCSS Plugin

Options

Source Map

PostCSS has great source maps support. It can read and interpret maps from previous transformation steps, autodetect the format that you expect, and output both external and inline maps.

To ensure that you generate an accurate source map, you must indicate the input and output CSS files paths — using the options from and to, respectively.

To generate a new source map with the default options, simply set map: true. This will generate an inline source map that contains the source content. If you don’t want the map inlined, you can use set map.inline: false.

processor
    .process(css, {
        from: 'app.sass.css',
        to:   'app.css',
        map: { inline: false },
    })
    .then(function (result) {
        result.map //=> '{ "version":3,
                   //      "file":"app.css",
                   //      "sources":["app.sass"],
                   //       "mappings":"AAAA,KAAI" }'
    });

If PostCSS finds source maps from a previous transformation, it will automatically update that source map with the same options.

If you want more control over source map generation, you can define the map option as an object with the following parameters:

  • inline boolean: indicates that the source map should be embedded in the output CSS as a Base64-encoded comment. By default it is true. But if all previous maps are external, not inline, PostCSS will not embed the map even if you do not set this option.

    If you have an inline source map, the result.map property will be empty, as the source map will be contained within the text of result.css.

  • prev string, object or boolean: source map content from a previous processing step (for example, Sass compilation). PostCSS will try to read the previous source map automatically (based on comments within the source CSS), but you can use this option to identify it manually. If desired, you can omit the previous map with prev: false.

  • sourcesContent boolean: indicates that PostCSS should set the origin content (for example, Sass source) of the source map. By default it is true. But if all previous maps do not contain sources content, PostCSS will also leave it out even if you do not set this option.

  • annotation boolean or string: indicates that PostCSS should add annotation comments to the CSS. By default, PostCSS will always add a comment with a path to the source map. But if the input CSS does not have any annotation comment, PostCSS will omit it, too, even if you do not set this option.

    By default, PostCSS presumes that you want to save the source map as opts.to + '.map' and will use this path in the annotation comment. But you can set another path by providing a string value for annotation.

    If you have set inline: true, annotation cannot be disabled.

Safe Mode

If you provide a safe: true option to the process or parse methods, PostCSS will try to correct any syntax errors that it finds in the CSS.

postcss.parse('a {');                 // will throw "Unclosed block"
postcss.parse('a {', { safe: true }); // will return CSS root for a {}

This is useful for legacy code filled with hacks. Another use-case is interactive tools with live input — for example, the Autoprefixer demo.

Something went wrong with that request. Please try again.