In this article we’ll be looking to a basic overview of PostCSS from the perspective of a developer whose current CSS development process includes use of CSS preprocessor, or in particular, Sass. If you’re a Sass user, there are a couple of approaches when starting out with PostCSS. You could make a complete switch and recreate your basic preprocessing environment with PostCSS, or you could start using it as a supplement.
Many of you will say that you still only rely on your favorite preprocessor, but then, it’s possible that you’re also using Autoprefixer for vendor prefixing, and guess what? In this case, you have already included PostCSS into your workflow.
What exactly are we talking about?
PostCSS is a tool, or basically, just an API which handles its plugins written in JavaScript.
Comparing to Sass, which has a bunch of features out of the box, PostCSS comes as a blank plate, ready to be filled with the ingredients you need.
Basic Setup
Including PostCSS into your project is not a complicated process, especially if you have a basic experience of using some of the task runners, such as Gulp or Grunt.
As a simple example, let’s take a look at the following gulpfile.js.
var gulp = require('gulp'),
postcss = require('gulp-postcss'),
autoprefixer = require('autoprefixer');
gulp.task('css', function() {
return gulp.src('src/style.css')
.pipe(postcss(
autoprefixer()
))
.pipe(gulp.dest('dest/style.css'));
});
What we see here is a two step process:
- First, we include the main PostCSS module.
- Next, we add PostCSS plugin(s) we want to use (which in these short example is the only one – Autoprefixer).
Of course, like with any new gulp plugin which you include into your gulpfile.js, PostCSS module and any additional PostCSS plugin need to be installed first. This can be done in a terminal, with a simple command, familiar to all Gulp users:
npm install gulp-postcss autoprefixer --save-dev
Choosing plugins
So, which plugins do we need? Well, this comes to your individual choice. For an easy start or just for supplementing your preprocessing workflow with some additional power, you will certainly gain an instant benefit with these two:
- Autoprefixer – probably the most popular PostCSS plugin, used for adding required vendor prefixes. As already mentioned at the beginning, there is high chance that you’re already using this one.
.box {
display: flex;
}
// Result after processing
.box {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
}
If you want to get in more deeper and recreate your basic Sass environment, most likely you’ll also need to require the following plugins:
- Postcss-simple-vars – for including Sass-like variables.
$blue: #056ef0;
$column: 200px;
.menu_link {
background: $blue;
width: $column;
}
// Result after processing
.menu_link {
background: #056ef0;
width: 200px;
}
- Postcss-nested – gives us a functionality of unwrapping nested rules like how Sass does it.
.phone {
&_title {
width: 500px;
@media (max-width: 500px) {
width: auto;
}
}
}
// Result after processing
.phone_title {
width: 500px;
}
@media (max-width: 500px) {
.phone_title {
width: auto;
}
}
- Postcss-mixins – 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);
}
// Result after processing
.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;
}
One of the most interesting plugins that we’re mentioning last, is CSSNext. This is actually a collection of plugins that, together, give us a possibility to use the latest CSS syntax today. It transforms new CSS specs into more compatible CSS without a need to waiting for browser support. CSSNext has a lot of features and some of them are:
- custom properties set & @apply
- custom properties & var()
- custom selectors
- color() function
- :any-link pseudo-class, etc.
In your CSS file you can do something like this:
// Example for custom properties set & @apply
:root {
--danger-theme: {
color: white;
background-color: red;
};
}
.danger {
@apply --danger-theme;
}
Why should you use PostCSS?
So, if you already have an effective workflow and you’re satisfied with using your favorite preprocessor for some time now, you might be still asking yourself why do I need to learn another tool (or make the switch from Sass)? What are the benefits?
To answer these questions, let’s summarize some of the advantages:
- Speed – even though in the meantime Sass got a significantly faster (e.g., LibSass), PostCSS is still the winner here
- Modularity – reduces bloat; you only include the functionality that you need
- Lightweight – with previous benefit, you get also this one
- Immediate implementation – if you want a new functionality, you don’t have to wait for Sass to be updated; you can make it on your own
Of course, everything’s not ideal and there are also certain drawbacks:
- Increased complexity – more planning is required (e.g., plugins must be called in a specific order)
- A different syntax (compared to Sass)
- PostCSS processing requires valid CSS
What’s next
It’s perfectly clear that PostCSS is all about the plugins. At the time of writing, there are more than 200 plugins available (and this number is only getting bigger). So, to go beyond the basics, you’ll need to search for other plugins that will extend this barebones setup.
Of course, if you find out that some handy functionality is missing, go ahead and solve the problem by making your own PostCSS plugin.
The post Using PostCSS with Sass appeared first on Inchoo.