How to vendor prefix and minify CSS?
Using PostCSS, Autoprefixer and cssnano

Software Engineer, building syntackle.com
Search for a command to run...
Using PostCSS, Autoprefixer and cssnano

Software Engineer, building syntackle.com
No comments yet. Be the first to comment.
Authentication is a fundamental aspect of modern web and mobile applications. It ensures that users can securely access an app while protecting their data. Firebase, a platform developed by Google, offers a simple and efficient way to add authenticat...

Hello there! My name is Shivam, and today, I will show you how to create this beautiful horizontal scrolling parallax effect with HTML, CSS, and JavaScript. Wouldn't this look stunning on your blog or portfolio site? To achieve this effect, we will ...

Dealing with fonts can get quite overwhelming. For quite some time, I was searching for a way to self-host google fonts because the google fonts API's network request increased the render-blocking time more than I expected. I stumbled upon fontsource...

Our favorite month is here! And we have some amazing activities planned for our community 🤩 Yes, you are right. We are talking about Hacktoberfest! In this blog, we want to cover various ways you can participate in Hacktoberfest through the ReactPla...

The second edition of ReactPlay Hackathon is here!

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.
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.
npm i -g postcss-cli
The postcss package can be installed from npm.
npm i postcss postcss-cli --save-dev
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.
npm i autoprefixer --save-dev
Now, you can autoprefix your CSS file using this command:
postcss src/styles/*.css -u autoprefixer --dir src/prefixed --no-map
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:
* {
margin: 0;
box-sizing: border-box;
text-size-adjust: auto;
}
After prefixing:
* {
margin: 0;
box-sizing: border-box;
-webkit-text-size-adjust: auto;
-moz-text-size-adjust: auto;
text-size-adjust: auto;
}
The cssnano plugin can minify/compress CSS and make it suitable for production use. Install cssnano plugin using this command:
npm i cssnano --save-dev
Minify your already vendor-prefixed CSS file using this command:
postcss src/prefixed/*.css -u cssnano --dir src/minified --no-map
It will minify all the files present in the prefixed directory and output them to the minified directory.
The final result you get is:
*{-webkit-text-size-adjust:auto;-moz-text-size-adjust:auto;text-size-adjust:auto;box-sizing:border-box;margin:0}
You can autoprefix as well as minify CSS using one single command:
postcss src/styles/*.css -u autoprefixer cssnano --dir src/styles/production --no-map
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 variety of awesome plugins available to use in postcss.