PostCSS Deep Dive: Miscellaneous Goodies
Throughout this series we’ve been through many different categories of PostCSS plugins, offering all kinds of different advantages for your CSS development. But some of the very best PostCSS plugins don’t fit into the categories we’ve covered in our previous series entries.
In this tutorial you’re going to learn about five of these “miscellaneous goodies”. We’ll cover:
- Generating grids with Lost
- Seeing how your designs look to colorblind people with postcss-colorblind
- Converting px units to rem with postcss-pxtorem
- Auto-generating an RTL version of your stylesheet with rtlcss
- Auto-generating a style guide with postcss-style-guide
Let's begin!
Setup Your Project
As ever, the first thing you’ll need to do is setup your project to use either Gulp or Grunt, depending on your preference. If you don’t already have a preference for one or the other I recommend using Gulp as you’ll need less code to achieve the same ends, so you should find it a bit simpler to work with.
You can read about how to setup Gulp or Grunt projects for PostCSS in the previous tutorials
respectively.
If you don’t want to manually setup your project from scratch though, you can download the source files attached to this tutorial, and extract either the provided Gulp or Grunt starter project into an empty project folder. Then with a terminal or command prompt pointed at the folder run the command npm install
.
Install Plugins
Now we’ll need to install five plugins into your project. Whether you’re using Gulp or Grunt, run the following command inside your project folder to install the plugins we’ll be using:
1 |
npm install lost postcss-colorblind postcss-pxtorem postcss-style-guide --save-dev |
As well as these five we’ll also be using rtlcss, but as it works a little differently to other PostCSS plugins we’ll be using it via its corresponding Gulp or Grunt plugins.
If using Gulp, install gulp-rtlcss with the command:
1 |
npm install gulp-rtlcss gulp-rename --save-dev |
And if using Grunt install grunt-rtlcss with the command:
1 |
npm install grunt-rtlcss --save-dev |
Now we’re ready to load the plugins into your project.
Load Plugins via Gulp
If you’re using Gulp, add these variables under the variables already in the file:
1 |
var lost = require('lost'); |
2 |
var colorblind = require('postcss-colorblind'); |
3 |
var pxtorem = require('postcss-pxtorem'); |
4 |
var styleGuide = require('postcss-style-guide'); |
5 |
|
6 |
var rtlcss = require('gulp-rtlcss'); |
7 |
var rename = require('gulp-rename'); |
Now add each of those new variable names into your processors
array:
1 |
var processors = [ |
2 |
lost, |
3 |
// colorblind,
|
4 |
pxtorem, |
5 |
styleGuide
|
6 |
];
|
Note: colorblind
is commented out, which will be explained later.
Do a quick test that everything is working by running the command gulp css
then checking that a new “style.css” file has appeared in your project’s “dest” folder.
Load Plugins via Grunt
If you’re using Grunt, update the processors
object, which is nested under the options
object, to the following:
1 |
processors: [ |
2 |
require('lost')(), |
3 |
// require('postcss-colorblind')(),
|
4 |
require('postcss-pxtorem')(), |
5 |
require('postcss-style-guide')() |
6 |
]
|
Note: require('postcss-colorblind')(),
is commented out, which will be explained later.
Do a quick test that everything is working by running the command grunt postcss
then checking that a new “style.css” file has appeared in your project’s “dest” folder.
Generate Grids with Lost
Lost is a completely PostCSS based grid generation system by Cory Simmons, the creator of Jeet, another very successful grid system written in Stylus. It has a long list of impressive features, yet its deployment is very straight forward.
Start by creating a file named “index.html” in your “dest” folder. We’ll be setting up a basic grid layout in this file. Add the following code to it:
1 |
<!DOCTYPE html>
|
2 |
<html lang="en"> |
3 |
<head>
|
4 |
<link rel="stylesheet" href="style.css"> |
5 |
</head>
|
6 |
<body>
|
7 |
|
8 |
<div class="row"> |
9 |
<div class="main"> |
10 |
<h1>Main Area</h1> |
11 |
</div>
|
12 |
<div class="sidebar"> |
13 |
<h1>Sidebar</h1> |
14 |
</div>
|
15 |
</div>
|
16 |
|
17 |
</body>
|
18 |
</html>
|
Then add this code to your “src/style.css” to setup a basic two column grid layout, with a main area column and sidebar column:
1 |
@lost gutter 1.618rem; |
2 |
|
3 |
.row { |
4 |
lost-center: 75rem; |
5 |
}
|
6 |
|
7 |
.main { |
8 |
lost-column: 3/5; |
9 |
}
|
10 |
|
11 |
.sidebar { |
12 |
lost-column: 2/5; |
13 |
}
|
Let’s break down what we’ve done here.
First, we’ve use a @lost
at-rule to set our own value for one of Lost’s options: gutter size. By default gutters (gaps between columns) are 30px
. I always like to use 1.618rem
for spacing in designs, so I’ve set that to be the new gutter size with the line @lost gutter 1.618rem;
.
Next, we’ve setup a .row
class that we’ll wrap around our columns. This only requires using the property lost-center
and specifying a width for the row. The plugin will take care of setting a max-width
on the class, centering it, and clearing it.
After that, in the .main
and .sidebar
classes, we’ve created our columns.
Lost doesn’t restrict you to working with a predetermined number of columns like twelve or sixteen; you can have any number of columns you want. Column widths are determined by using the property lost-column
and setting a fraction as the value. In our code the .main
class uses a setting of 3/5
so it will take up 3 of 5 columns, and the .sidebar
uses 2/5
so it will take 2 of 5 columns.
Compile your file and in your “dest/style.css” file you should now see this code:
1 |
.row { |
2 |
*zoom: 1; |
3 |
max-width: 75rem; |
4 |
margin-left: auto; |
5 |
margin-right: auto; |
6 |
}
|
7 |
|
8 |
.row:before { |
9 |
content: ''; |
10 |
display: table; |
11 |
}
|
12 |
|
13 |
.row:after { |
14 |
content: ''; |
15 |
display: table; |
16 |
clear: both; |
17 |
}
|
18 |
|
19 |
.main { |
20 |
width: calc(99.99% * 3/5 - (1.618rem - 1.618rem * 3/5)); |
21 |
}
|
22 |
|
23 |
.main:nth-child(n) { |
24 |
float: left; |
25 |
margin-right: 1.618rem; |
26 |
clear: none; |
27 |
}
|
28 |
|
29 |
.main:last-child { |
30 |
margin-right: 0; |
31 |
}
|
32 |
|
33 |
.main:nth-child(5n) { |
34 |
margin-right: 0; |
35 |
}
|
36 |
|
37 |
.main:nth-child(5n + 1) { |
38 |
clear: left; |
39 |
}
|
40 |
|
41 |
.sidebar { |
42 |
width: calc(99.99% * 2/5 - (1.618rem - 1.618rem * 2/5)); |
43 |
}
|
44 |
|
45 |
.sidebar:nth-child(n) { |
46 |
float: left; |
47 |
margin-right: 1.618rem; |
48 |
clear: none; |
49 |
}
|
50 |
|
51 |
.sidebar:last-child { |
52 |
margin-right: 0; |
53 |
}
|
54 |
|
55 |
.sidebar:nth-child(5n) { |
56 |
margin-right: 0; |
57 |
}
|
58 |
|
59 |
.sidebar:nth-child(5n + 1) { |
60 |
clear: left; |
61 |
}
|
And when viewed in a browser, your “dest/index.html” file should now present a two column layout like this:



It’s a little hard to see exactly what’s going on with our grid here, which is the reason Lost also provides lost-utility: edit;
to highlight your grid for easy visualization during development.
Add this to each of the classes we’ve created so far:
1 |
.row { |
2 |
lost-center: 75rem; |
3 |
lost-utility: edit; |
4 |
}
|
5 |
|
6 |
.main { |
7 |
lost-column: 3/5; |
8 |
lost-utility: edit; |
9 |
}
|
10 |
|
11 |
.sidebar { |
12 |
lost-column: 2/5; |
13 |
lost-utility: edit; |
14 |
}
|
Now when you recompile and refresh your page you should see your grid highlighted like this:



Let’s make that all a little clearer to see again with some extra styling (which will also help us with later sections in this tutorial). Update your “src/style.css” file to the following, adding a couple of extras like padding inside the columns and a few background and text colors:
1 |
@lost gutter 1.618rem; |
2 |
|
3 |
* { |
4 |
box-sizing: border-box; |
5 |
}
|
6 |
|
7 |
html, body { |
8 |
height: 100%; |
9 |
margin: 0; |
10 |
font-family: "Open Sans"; |
11 |
}
|
12 |
|
13 |
html { |
14 |
padding: 0; |
15 |
}
|
16 |
|
17 |
body { |
18 |
padding: 1.618rem; |
19 |
background: #16a085; |
20 |
}
|
21 |
|
22 |
.row { |
23 |
lost-center: 75rem; |
24 |
}
|
25 |
|
26 |
.main, .sidebar { |
27 |
padding: 1.618rem; |
28 |
min-height: 500px; |
29 |
}
|
30 |
|
31 |
.main { |
32 |
lost-column: 3/5; |
33 |
background: white; |
34 |
color: #232323; |
35 |
}
|
36 |
|
37 |
.sidebar { |
38 |
lost-column: 2/5; |
39 |
background: #2c3e50; |
40 |
color: white; |
41 |
}
|
Compile your CSS again and reload your page and you should now have a classic two column layout like this:



What we’ve covered here just skims the surface of what can be done with Lost, so be sure to read about all the rest of the features at: https://github.com/corysimmons/lost
See Through the Eyes of the Colorblind
Color blindness effects a larger portion of your site’s visitors than you might realize. For example, the most common type of color blindness is deuteranomaly, effecting 6% of all males and 0.4% of females. To put that in perspective, it’s estimated IE9 and IE10 combined are used by around 4% of all web traffic. One might suggest that if we can put a significant amount of time into supporting specific browsers, we can put an equal measure of time into supporting people.
The postcss-colorblind plugin by Brian Holt helps immeasurably in assessing how accessible a design is for people with various forms of color blindness, as it allows you to see for yourself how your color schemes would look if you had the same visual perception. It allows you to generate versions of your stylesheet that simulate eight different kinds of color blindness. Let’s see how you can use it.
Add Some Extra Colors
First, we’ll add some extra colors to our design so far to help us more clearly see the effect of the different stylesheets we’re about to generate. We’ll add five “metro style” tiles, by adding the following html below the row we already have in our “dest/index.htm” file:
1 |
<div class="row"> |
2 |
<div class="tile">This is a Tile</div> |
3 |
<div class="tile">This is a Tile</div> |
4 |
<div class="tile">This is a Tile</div> |
5 |
<div class="tile">This is a Tile</div> |
6 |
<div class="tile">This is a Tile</div> |
7 |
</div>
|
Now add the following code to your “src/style.css” file to style these tiles with five different colors:
1 |
.row { |
2 |
margin-bottom: 1.618rem; |
3 |
}
|
4 |
|
5 |
.tile { |
6 |
lost-column: 1/5; |
7 |
padding: 3.75rem 1.618rem; |
8 |
text-align: center; |
9 |
font-size: 1.25rem; |
10 |
color: white; |
11 |
}
|
12 |
|
13 |
.tile:nth-of-type(1) { |
14 |
background: #f39c12; |
15 |
}
|
16 |
|
17 |
.tile:nth-of-type(2) { |
18 |
background: #c0392b; |
19 |
}
|
20 |
|
21 |
.tile:nth-of-type(3) { |
22 |
background: #8e44ad; |
23 |
}
|
24 |
|
25 |
.tile:nth-of-type(4) { |
26 |
background: #2980b9; |
27 |
}
|
28 |
|
29 |
.tile:nth-of-type(5) { |
30 |
background: #d35400; |
31 |
}
|
After compilation, you should see your file now looks like this in a browser:



Generate Colorblind Simulations
You may have noticed that when we setup our processors
array earlier the entry for colorblind
was commented out. That’s because as soon as the plugin is active it will apply colorblind simulation to your stylesheet, so you don’t want to turn it on until you’re ready to use it. Uncomment it in the processors
array now.
To simulate any of the eight types of colorblindness, pass the option method
for this plugin in your Gulpfile or Gruntfile, along with the name of the type of colorblindness you want to check on.
For example, to simulate deuteranomaly set this option:
1 |
/* Gulpfile */ |
2 |
colorblind({method: 'deuteranomaly'}), |
3 |
|
4 |
/* Gruntfile */ |
5 |
require('postcss-colorblind')({method: 'deuteranomaly'}) |
Now recompile your stylesheet and refresh your page and you will see your design as a person with deuteranomaly does:



You’ll notice the colors look notably different. A person with deuteranomaly sees red and green differently, so while you’ll notice the blue is almost the same, the reds and greens are quite different.
To visualize protanopia set this option:
1 |
/* Gulpfile */ |
2 |
colorblind({method: 'protanopia'}), |
3 |
|
4 |
/* Gruntfile */ |
5 |
require('postcss-colorblind')({method: 'protanopia'}) |
Recompile your stylesheet and now you’ll see this:



A person with protanopia essentially does not see red at all, and sees green in a different way. You’ll notice again that the strong blues are not too heavily effected, but purple has become pure blue, and the remaining colors have become variations of the same yellowish brownish colors. It has become very difficult to distinguish the red and two orange colored tiles from one another.
This ability to generate all different types of colorblind simulations is incredibly insightful, and helps us to ensure the color schemes we have chosen don’t rely on perception of a particular hue and are thereby accessible to all.
Read more about postcss-colorblind at: https://github.com/btholt/postcss-colorblind
Convert px Units to rem
In almost all designs, there are very good reasons for the rem
unit to play a prominent role. However it is difficult to think in rem
units, and much easier to think in px
units when creating layouts. The postcss-pxtorem plugin helps with this speedbump, by automatically converting px
units to rem
units.
The plugin uses a white list of properties to which it applies, so by default px
units will be converted to rem
when used on:
- font
- font-size
- line-height
- letter-spacing
You can add extra properties to this white list by setting a prop_white_list
option. Update your Gulpfile or Gruntfile to add the width
property like so:
1 |
/* Gulpfile */
|
2 |
pxtorem({ |
3 |
prop_white_list: ['width', 'font', 'font-size', 'line-height', 'letter-spacing'] |
4 |
}),
|
5 |
|
6 |
/* Gruntfile */
|
7 |
require('postcss-pxtorem')({ |
8 |
prop_white_list: ['width', 'font', 'font-size', 'line-height', 'letter-spacing'] |
9 |
}),
|
Now add the following code to your “src/style.css” file so we can test the conversion process:
1 |
.convert_this { |
2 |
width: 500px; |
3 |
font-size: 18px; |
4 |
}
|
Compile your file and in your “dest/style.css” file you should now see the resulting rem
values:
1 |
.convert_this { |
2 |
width: 31.25rem; |
3 |
font-size: 1.125rem; |
4 |
}
|
Read more about postcss-pxtorem at: https://github.com/cuth/postcss-pxtorem
Generate a RTL Version of Your Stylesheet
If you are catering to a global audience, you may need to provide support for scripts that are read from right to left rather than left to right, such as Arabic and Hebrew for example. When the orientation of script is flipped, so too should the layout of your site so the entire design makes sense for those who look at the right side of the screen first.
The rtlcss plugin by Mohammad Younes makes the process of creating a right-to-left layout much easier, as it can automatically scan your stylesheet and convert its orientation, swapping the word “left” for “right” and vice versa.
This plugin actually works a little differently to other PostCSS plugins, in that we can’t add it to our processors
array. Instead, during our project setup we installed the Gulp and Grunt plugins for rtlcss, and we’re going to setup separate tasks to execute it.
If using Gulp, add this code to your Gulpfile:
1 |
gulp.task('rtl', function () { |
2 |
return gulp.src('./dest/style.css') |
3 |
.pipe(rtlcss()) |
4 |
.pipe(rename({ suffix: '-rtl' })) |
5 |
.pipe(gulp.dest('./dest')); |
6 |
});
|
If using Grunt, add this line after your existing grunt.loadNpmTasks
line:
1 |
grunt.loadNpmTasks('grunt-rtlcss'); |
Then add a comma ,
after your postcss
task, and paste in this new rtlcss
task:
1 |
rtlcss: { |
2 |
'default':{ |
3 |
dest : 'dest/style-rtl.css', |
4 |
src : 'dest/style.css' |
5 |
}
|
6 |
}
|
Now, if using Gulp run the command gulp rtl
, and if using Grunt run the command grunt rtlcss
to generate a right-to-left stylesheet named “style-rtl.css” in your “dest” folder.
Edit your “dest/index.html” file to load “style-rtl.css” instead of “style.css”, refresh your site, and you should see your layout has been flipped around:



You’ll notice the text is still left aligned, but this can be easily remedied by just adding text-align: left;
in your default stylesheet, which rtlcss will convert to text-align: right;
.
Read more about rtlcss and its Gulp and Grunt counterparts at:
- https://github.com/MohammadYounes/rtlcss
- https://github.com/jjlharrison/gulp-rtlcss
-
https://github.com/MohammadYounes/grunt-rtlcss
Generate a Style Guide
The postcss-style-guide plugin is a fantastic tool created by Masaaki Morishita. It allows you to generate a styleguide automatically based on your stylesheet. All you have to do is add some comments to your CSS, and those comments will be parsed as Markdown and used to populate your styleguide.
The first thing we’re going to do is configure a couple of options for our styleguide. We’re going to set the name of our project so it can be displayed in the header of the styleguide, and we’re also going to use a custom theme named 1column.
To install the custom theme we’ll be using into your project, run the command:
1 |
npm install psg-theme-1column --save-dev |
Now update your Gulpfile or Gruntfile to pass a name
and theme
options as follows:
1 |
/* Gulpfile */
|
2 |
styleGuide({ |
3 |
name: 'Auto Style Guide', |
4 |
theme: '1column' |
5 |
})
|
6 |
|
7 |
/* Gruntfile */
|
8 |
require('postcss-style-guide')({ |
9 |
name: 'Auto Style Guide', |
10 |
theme: '1column' |
11 |
})
|
The way postcss-styleguide works is that it will look for your comments in your stylesheet and turn each one it finds into a styleguide entry. It will assume any comment relates to the CSS that follows it, all the way through to another comment or the end of the document.
For this reason, any CSS that you want to showcase in your styleguide should be moved to the bottom of your stylesheet. We’re going to have our colored tiles appear in the styleguide, as well as an h1
element, so we’ll need both of those to be at the end of our document.
Start by moving all the classes that effect our tiles down to the bottom of your stylesheet; that’s the .tile
class and the five .tile:nth-of-type()
styles. Then also add a little code to control h1
elements so the bottom of your stylesheet looks like this (the .tile:nth-of-type() styles aren’t shown to save space):
1 |
h1 { font-size: 42px; } |
2 |
|
3 |
.tile { |
4 |
lost-column: 1/5; |
5 |
padding: 3.75rem 1.618rem; |
6 |
text-align: center; |
7 |
font-size: 1.25rem; |
8 |
color: white; |
9 |
}
|
Now we can add in some comments to describe these styles. Any html added in these comments will be rendered as html in the styleguide, and the CSS that follows a comment will appear in a display box.
Add some comments to your stylesheet describing the h1
style and the .tile
class, and including some example html, so you end up with the following:
1 |
/*
|
2 |
This is the h1 style
|
3 |
|
4 |
<h1>Heading 1</h1>
|
5 |
|
6 |
*/
|
7 |
|
8 |
h1 { font-size: 42px; } |
9 |
|
10 |
/*
|
11 |
These use the .tile class
|
12 |
|
13 |
<div class="row">
|
14 |
<div class="tile">This is a Tile</div>
|
15 |
<div class="tile">This is a Tile</div>
|
16 |
<div class="tile">This is a Tile</div>
|
17 |
<div class="tile">This is a Tile</div>
|
18 |
<div class="tile">This is a Tile</div>
|
19 |
</div>
|
20 |
|
21 |
*/
|
22 |
|
23 |
.tile { |
24 |
lost-column: 1/5; |
25 |
padding: 3.75rem 1.618rem; |
26 |
text-align: center; |
27 |
font-size: 1.25rem; |
28 |
color: white; |
29 |
}
|
Now compile your stylesheet, look in your project’s root folder and open the “styleguide.html” file you find there in a browser:



Hey presto, instant styleguide!
At the moment it looks a little funky because it has picked up the green body background color from our stylesheet. What we want is for our content to be on a white background, and we can get the styleguide to pick up some other code from our stylesheet to make this happen.
The central area of this styleguide template uses a section
element, so above the comment describing your h1
element, add this code:
1 |
section { |
2 |
padding: 1rem; |
3 |
background: white; |
4 |
}
|
Recompile your code, refresh your styleguide, and you’ll see it’s now making use of the styling we just added to section
elements and looks like this:



There, much better.
Read more about postcss-style-guide at: https://github.com/morishitter/postcss-style-guide
Let’s Recap
Summing up everything we’ve covered above:
- The Lost plugin allows you to create flexible grids with just a few properties, with lots of extra functionality available too should you need it.
- The postcss-colorblind plugin lets you see for yourself how your designs look to people with any of eight different types of colorblindness.
- The postcss-pxtorem plugin enables you to write code using
px
units but have them automatically converted torem
units during processing.
- The rtlcss plugin automatically generates right-to-left stylesheets by scanning your code for the word “right” and replacing it with “left”, and vice versa.
- The postcss-style-guide plugin automatically generates a styleguide, based on comments added to your stylesheet.
Coming Next: Make Your Own Plugin
In the next and final installation of this PostCSS Deep Dive you’ll learn how to unlock one of the biggest advantages that PostCSS offers; that being the ability to use it to create any type of functionality you want.
You’ll learn how to make your own basic plugin, and from that basis hopefully you’ll go on to make many more plugins for whatever need might arise in the future.
I’ll see you in the final tutorial!