# PostCSS Deep Dive: Shortcuts and Shorthand

This post is part of a series called PostCSS Deep Dive.
Using PostCSS with BEM and SUIT Methodologies
PostCSS Deep Dive: Miscellaneous Goodies

So far we’ve used PostCSS to do things like optimizing stylesheets, adding functionality for preprocessing, and generating certain class naming conventions, but how can it help just writing plain old CSS?

There are so many little bits of code the average web designer types out thousands of times through the course of their career, and if you can gain just a little bit of time back on each one it adds up to a lot in the end.

In this tutorial we’re going to use PostCSS to cut down on every day code typing time via a series of shortcuts and shorthand additions. Let’s begin!

You’ll be familiar with this process by now, but the first thing you’ll need to do is setup your project to use either Gulp or Grunt. 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

For this tutorial we’ll be installing several plugins, each one handling a different type of shortcut or shorthand.

Whether you’re using Gulp or Grunt, run the following command inside your project folder to install the plugins we’ll be using:

If you’re using Gulp, add these variables under the variables already in the file:

Now add each of those new variable names into your processors array:

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.

If you’re using Grunt, update the processors object, which is nested under the options object, to the following:

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.

Alright, now your plugins are all installed let’s learn how to use them for CSS shortcuts and shorthand.

## Use Shorthand for Properties

There are a lot of properties we have to type over and over and over again in CSS. Sure, the time it takes to type a handful of characters once is very small, but over years of development it all adds up. We’re going to look here at two plugins that allow you to trim those properties down to shorthand versions to you can get a quick and smooth flow going as you hammer out your CSS.

The postcss-alias plugin by Sean King allows you to define your own shorthand, or “aliases”, for any properties you want to abbreviate. This gives you a way to make sure the shorthand you use fits with the way your mind works and is hence easy for you to remember.

To define some aliases add an at-rule at the top of your stylesheet with the syntax @alias {...}. Then inside the at-rule setup your aliases by adding alias-name: property-name;.

Add the following example to your “src/style.css” file, which will setup aliases for the border-size, border-style and border-color properties:

Then add this code to test out using the new aliases:

Compile your file, and in your “dest/style.css” file you should now see:

### Plug ‘n’ Play Shorthand

If you want to use a lot of property shorthands, but you don’t want to go through the steps of defining each one yourself, you can check out the plugin postcss-crip by Johnie Hjelm which has hundreds of property abbreviations ready for plugin and play use.

For example, add the following code to your “src/style.css” file, which contains shorthand for the margin-top, margin-right, margin-bottom and margin-left properties:

Compile your code and you should see the expanded properties appear in your “dest/style.css” file:

And see the full list of property abbreviations at: https://github.com/johnie/crip-css-properties

## Output @font-face in One Line

The postcss-font-magician plugin by Jonathan Neal very much lives up to its name. It allows you to use custom fonts via a simple font-family rule as though you were using a standard font, and the font will just magically work.

That’s it. That’s all it takes to make use of postcss-font-magician. No special syntax, just use the font name as you would any other.

In this case, the Indie Flower font is one that I’ve selected from the Google Fonts library. I haven’t added any custom font files to my project, so the plugin will see that and instead go check to see if my specified font is available from Google Fonts. When it finds that it is available, it will create the appropriate @font-face code completely automatically.

Compile your file then look at your “dest/style.css” file, where you’ll see this code has been added in:

You can check that the font is loading correctly by creating a new file in your “dest” folder named “index.html” and adding this code to it:

For the fonts to load you’ll need to view this file via an https:// address, rather than a file:// address, so either upload the file to a web host or use an app like Prepros to create a live preview.

You should see the Indie Flower font applied to all your text, like so:

## Create CSS Shapes

If you’ve ever used pure CSS to create shapes, you’ll know it can be a great way to include things like circles and triangles into your designs, but that it can also be quite tricky. Particularly in the case of triangles, figuring out what code you need to get a shape oriented in the right direction at the right size can feel somewhat counter intuitive.

That’s where the postcss-circle and postcss-triangle plugins by Jed Mao come to the rescue. Both plugins create a simplified syntax, and an intuitive way of creating circles and triangles with pure CSS.

To create a circle, use the syntax circle: size color;. Add the following code to your “src/style.css” file:

Compile it and you’ll see the following code added to your “dest/style.css” file:

Now add this HTML to the “dest/index.html” file you created in the last section:

Take a fresh look at your file in a browser and you should now see a red circle:

There are three types of triangle you can add using this plugin; isosceles, right-isosceles and equilateral. Each have slightly different sets of syntax, which you can read about in full at the plugin’s Github repo.

We’ll go through adding an isosceles triangle, the syntax for which is:

Let’s add this example isosceles triangle to our “src/style.css” file:

Compile the file and you should now see complete triangle CSS in your “dest/style.css” file:

On refreshing this file in your browser, you should now see a red isosceles triangle pointing to the right:

## Use Shortcuts for Common Tasks

Setting colors for links is a job that has to be done in every project, and requires setting styles for default links as well as four different link states. The postcss-all-link-colors plugin by Jed Mao lets you shortcut that process, outputting the colors for all your links at once.

Then compile your file and you’ll see all the required link states have been set:

You also have the option to generate a different color for a specific state. Just add some curly braces at the end of the rule, and inside that use the syntax state: color;.

Update the code you just added to your “src/style.css” file to the following:

Now when you compile you’ll see the hover state has a different color to the rest of the styles:

### Center Vertically or Horizontally

Centering, either vertically or horizontally, is one of those tasks that’s a perpetual thorn in the side of CSS developers. Jed Mao’s postcss-center plugin makes the task a lot simpler with the introduction of top: center; for vertical centering, and left: center; for horizontal centering.

Then compile it and you’ll see the following code:

Note: the centering uses absolute positioning, so you will need to wrap your centered elements with a parent that uses relative, absolute or fixed positioning. Given absolutely positioned elements don’t affect the height or width of their parents, you’ll also want to set the height and width of the parent element to suit.

For example, add left: center; to the .circle class you created earlier so it will be horizontally centered:

Then add this second class to act as a wrapper around the circle, giving it relative positioning and a height matching the circle:

Now add an element bearing this class as a wrapper around your existing circle html:

When you go back and refresh your page, you should see your circle has been horizontally centered:

### Output Clearfix in One Line

In any design that incorporates floats, creating a clearfix class is so handy that it’s just about essential. Sean King’s postcss-clearfix plugin turns creating that clearfix styling into a one line job, by adding fix as a possible value that can be used with the clear property.

For a clearfix that will work on IE8+ add the following to your “src/style.css” file:

On compiling, you’ll see it has produced the following clearfix code, ready for use:

If you need a clearfix that will work on IE6+ use the value fix-legacy instead of plain fix, like so:

When this code is compiled, you’ll see it includes a little extra content that makes it friendly to legacy browsers:

### Set Positioning in One Line

When you want to use non default positioning, i.e. absolute, fixed or relative, you have to type out the values for the element’s top, right, bottom and left positioning manually. There is no shorthand as you might use when setting margins or padding in one line. That is, until you install Sean King’s postcss-position plugin.

With this plugin, when using the position property, after setting a value of absolute / fixed / relative, you can declare top, right, bottom and left values in the same line.

After compilation, you’ll see the shorthand has compiled into the separate lines you would normally have to type out manually.

The way these values are declared is with the same pattern as in margin or padding shorthand, i.e. you can also set top and bottom in the first value, then right and left in the second, or you can set one value to apply to all four.

For example, try out the following code:

You should see it compile into:

### Set Width and Height at Once

The postcss-size plugin by Andrey Sitnik allows you to crunch the often used width and height properties down into a single size property. You can use it in two ways: passing two values with the first evaluating to width and the second to height, or passing one value that will be used for both width and height.

Test this out by adding the following CSS to your “src/style.css” file:

On compilation, you should see this code now in your “dest/style.css” file:

### Set Vertical and Horizontal Spacing

As a big lover of efficient coding, when writing out margins and padding, which are often equal on two sides, I’ve frequently found myself wishing there was a shortcut to declare vertical and horizontal spacing at once. I even wrote a few preprocessor mixins to do just that. But I won’t need those mixins anymore thanks to the postcss-verthorz plugin by David Hemphill.

With this plugin you can use padding-vert or margin-vert to set vertical spacing, and padding-horz or margin-horz to set horizontal spacing. Add the following example code to your “src/style.css” file to see this working:

After compilation you’ll see these rules have been expanded into full padding and margin declarations.

You can also shortcut these properties even further by abbreviating them to two letters. The example code we used above could be abbreviated to the following and the output would be exactly the same:

### Output Color Codes

My favorite default text color is #232323 and I don’t know if it’s just me, but I get tired of hammering out those same two digits over and over again. I often wished there was a shortcut, similar to the way you can trim #ffffff down to #fff. With the postcss-color-short plugin by Andrey Polischuk, there is.

When using this plugin, if you set a two digit color code those digits will be repeated until a six digit code is created, e.g. #23 will become #232323. If you set a one digit color code, it to will be repeated until there are three digits, e.g. #f will become #fff. You can even use it to set rgba() colors, where the first digit you pass will be repeated three times, and the second used as the alpha value, e.g. rgba(0, 0.5) will become rgba(0, 0, 0, 0.5).

Add the following to your “src/style.css” file to try all the above out:

After compilation you’ll see all the colors have been output in their full form:

## Summing Up

Let’s quickly recap everything we’ve been over in this tutorial:

• Small every day coding tasks don’t seem like much individually, but they add up to a huge amount of time overall, so shortcutting them can be worthwhile
• The postcss-alias plugin lets you create your own shorthand for properties
• The postcss-crip plugin has hundreds of predefined property shorthands
• The postcss-font-magician lets you use custom fonts as though they were default fonts, auto generating @font-face code for you
• The postcss-circle and postcss-triangle plugins make creating CSS circles and triangles straight forward and intuitive
• The postcss-center plugin provides vertical and horizontal centering with top: center; and left: center;
• The postcss-clearfix plugin lets you output clearfix code with clear: fix;
• The postcss-position plugin allows you to add your top, right, bottom and left settings as part of your use of the position property
• The postcss-size plugin lets you set width and height at once
• The postcss-verthorz plugin allows horizontal spacing and vertical spacing to be output with single rules.
• The postcss-color-short plugin gives you the ability to use one and two digit hexcodes, and other color shortcuts.

## In the Next Tutorial

In the next tutorial we’ll move onto plugins that are great, but just don’t fall into any particular category. I’ll see you soon in “Miscellaneous Goodies”.