How to vendor prefix and minify CSS?

Writing CSS from scratch along with adding vendor prefixes can be a daunting task if done manually. Vendor prefixes can be easily added using the autoprefixer plugin of PostCSS.

Also, the size of CSS file matters because CSS files can be render blocking so you need to keep a check on the size of the CSS file. This is where cssnano comes handy.

In this tutorial, you will be able to configure and set up PostCSS, autoprefixer and cssnano to vendor-prefix and minify your CSS.

Prerequisites

PostCSS

It is a tool to modify and transform your CSS. There are so many plugins for PostCSS to perform all kinds of tasks ranging from compressing CSS to using new CSS features right off the bat.

We will be using several postcss plugins to add vendor prefixes and minify CSS. But first, we need to install postcss-cli.

The postcss package can be installed from npm.

Autoprefixing CSS

Autoprefixer plugin uses Can I use to search for browser support and accordingly add vendor prefixes to CSS properties. You can install autoprefixer from npm.

Now, you can autoprefix your CSS file using this command:

The above command will parse each and every .css file inside the styles directory and use -u autoprefixer to autoprefix files and output them in the prefixed directory. The --no-map argument is optional. If you want a source map to be generated, then remove the --no-map argument.

CSS file before prefixing:

After prefixing:

Minifying CSS

The cssnano plugin can minify/compress CSS and make it suitable for production use.

Install cssnano plugin using this command:

Minify your already vendor-prefixed CSS file using this command:

It will minify all the files present in the prefixed directory and output them to the minified directory.

The final result you get is:

Bonus

You can autoprefix as well as minify CSS using one single command:

Conclusion

Vendor-prefixing and minifying are some of the important boxes you need to check in order to make your CSS production-ready. Using PostCSS, you can do so much with your CSS. There are a bunch of awesome plugins available to use in postcss.

Signing off.

This post was originally published in Syntackle.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Murtuza

Murtuza

Full Stack Developer | Content Creator | Technical Writer