Someone asked Andrey: why did you create PostCSS?
“because TJ Holowaychuk did not want to improve Rework API”
Currently, PostCSS has more than 200 plugins
It is recommended by Google and used in Twitter.
Total number of downloads last year,
between Oct 12 2015 and Oct 12 2016: 28.690.707
If you can imagin, you can do it
*But there are also plugin packages
Compare CSS processors for parsings, nested rules, mixins, variables and math:
PostCSS: 39 ms
Rework: 73 ms (1.9 times slower)
libsass: 77 ms (1.9 times slower)
Less: 179 ms (4.5 times slower)
Stylus: 269 ms (6.8 times slower)
Stylecow: 271 ms (6.9 times slower)
Ruby Sass: 1101 ms (28.0 times slower)
npm install gulp-postcss --save-dev
npm install postcss-import --save-dev
npm install postcss-custom-properties --save-dev
npm install postcss-nested --save-dev
npm install postcss-mixins --save-dev
...
var gulp = require('gulp'),
postcss = require('gulp-postcss'),
import = require('postcss-import'),
customproperties = require('postcss-custom-properties'),
mixins = require('postcss-mixins'),
nested = require('postcss-nested');
gulp.task('css', function() {
var processors = [
import,
cssnext,
mixins,
nested
];
return gulp.src('./src/*.css')
.pipe(postcss(processors))
.pipe(gulp.dest('./dest'));
});
gulp.task('watch', function() {
gulp.watch('src/**/*.css', ['css']);
});
Add vendor prefixes to CSS rules using values from Can I Use.
:fullscreen a {
display: flex
}
:-webkit-full-screen a {
display: -webkit-box;
display: flex
}
:-moz-full-screen a {
display: flex
}
:-ms-fullscreen a {
display: -ms-flexbox;
display: flex
}
:fullscreen a {
display: -webkit-box;
display: -ms-flexbox;
display: flex
}
:root{
--color:#f2b806;
}
.foo{
color:var(--color);
}
/*Using this input.css:*/
:root {
--color: pink;
}
div {
color: var(--color);
}
/*you will get:*/
div {
color: pink;
}
PostCSS plugin to inline @import rules content
@import "helpers/var.css";
PostCSS plugin to unwrap nested rules like how Sass does it.
.phone {
&_title {
width: 500px;
@media (max-width: 500px) {
width: auto;
}
body.is_dark & {
color: white;
}
}
img {
display: block;
}
}
.phone_title {
width: 500px;
}
@media (max-width: 500px) {
.phone_title {
width: auto;
}
}
body.is_dark .phone_title {
color: white;
}
.phone img {
display: block;
}
PostCSS plugin for mixins
@define-mixin icon $network, $color: blue {
.icon.is-$(network) {
color: $color;
@mixin-content;
}
.icon.is-$(network):hover {
color: white;
background: $color;
}
}
@mixin icon twitter {
background: url(twt.png);
}
@mixin icon youtube, red {
background: url(youtube.png);
}
.icon.is-twitter {
color: blue;
background: url(twt.png);
}
.icon.is-twitter:hover {
color: white;
background: blue;
}
.icon.is-youtube {
color: red;
background: url(youtube.png);
}
.icon.is-youtube:hover {
color: white;
background: red;
}
/* input */
:root {
--toolbar-theme: {
background-color: rebeccapurple;
color: white;
border: 1px solid green;
};
--toolbar-title-theme: {
color: green;
};
}
.toolbar {
@apply --toolbar-theme;
}
.toolbar-title {
@apply --toolbar-title-theme;
}
/* output */
.toolbar {
background-color: rebeccapurple;
color: white;
border: 1px solid green;
}
.toolbar-title {
color: green;
}
W3C Editor's Draft
W3C Editor's Draft
@custom-selector :--heading h1, h2, h3, h4, h5, h6;
:--heading { /* styles for all headings */ }
:--heading + p { /* more styles */ }
/* etc */
Compresses your css
@charset "utf-8";h1:before{margin:10px 20px;color:red;border-radius:16px;font-weight:normal;background-position:100% 100%}
PostCSS plugin to sort rules content with specified order.
Declarations. Example: { "sort-order": [ "margin", "padding" ] }
/* before */
p {
padding: 0;
margin: 0;
}
/* after */
p {
margin: 0;
padding: 0;
}
Grouping. Example: { "sort-order": [ [ "margin", "padding" ], [ "border", "background" ] ] }
/* before */
p {
background: none;
border: 0;
margin: 0;
padding: 0;
}
/* after */
p {
margin: 0;
padding: 0;
border: 0;
background: none;
}
A mighty, modern CSS linter that helps you enforce consistent conventions and avoid errors in your stylesheets.
var()
calc()
color()
function hwb()
function gray()
function #rrggbbaa
colors rgba
function
(rgb
fallback)
rebeccapurple
color font-variant
property filter
property
(svg fallback)
initial
value rem
unit
(px
fallback)
:any-link
pseudo-class :matches
pseudo-class :not
pseudo-class
(to l.3)
::
pseudo syntax
(:
fallback)
overflow-wrap
property
(word-wrap
fallback)
Use Sass-like markup in your CSS
It's possible