Hey everyone! 👋 You know, most of my time as a beginner I just used to write plain CSS and never really think of what happens next? Like, how can we finetune our designs so that its compatible for any type or versions of browsers out there as well as load up faster than always? Wouldn't that be cool? 🤷♂️
Hell yeah, that would be cool. 😎 So this is gonna be about fine-tuning your css designs in a manner that would make your pages load faster 🚀 as well as make your designs all browser compatible. 💻 And by all means, make your CSS production ready for getting the best out of your CSS. 🎉
We are going to install a couple packages from npm, for which you need to have node installed. If that's not done, do it from here now. Also if you're dizzy, grab a cup of coffee and you're good to go. ☕
Let's start a new project using npm init --y
('y' is for default values).
The dependencies that we are going to install (as devDependencies) are,
So, npm install node-sass concat autoprefixer postcss-cli npm-run-all --save-dev
. Let's now write some scripts that'll execute our process.
I'm going to show the scripts object down below (in package.json) and later on explain each one of it. You can refer to them, if any of it doesn't make sense.
"scripts": {
"compile:sass": "node-sass sass/main.scss css/style.comp.css",
"concat:css": "concat -o css/style.concat.css css/style.comp.css css/any-other-file.css",
"prefix:css": "postcss --use autoprefixer -b \"last 10 versions\" css/style.comp.css -o css/style.prefix.css",
"compress:css": "node-sass css/style.prefix.css css/style.css --output-style compressed",
"build:css": "npm-run-all compile:sass concat:css prefix:css compress:css"
}
Before we explain, I want you to know we are have our sass in sass/main.scss which will be processed and stored in css/ folder. (if you dont write sass, make two folders one as input where you've written css by yourself and output where the program is going to store the css files after execution) P.S.: folder names are arbitary
You can run these scripts by simply typing, npm run **your-command-name**
to your terminal.
It's only if you write in Sass, you need this step otherwise skip it. This will just compile your sass code to browser readable css.
compile:sass
: name of packagenode-sass
: name of package, we installed abovesass/main.scss
: address of our manually written sass filecss/style.css
: address where the resulting output of css file will be storedIf you have your css in more than one file, then you'd probably want to combine them into one css file which is better for your browser which only would have to make one HTTP request instead of two.
concat
: package name-o
/ --output
: denotes the output filecss/style.concat.css
: address of output file where all combined css is going to be storedcss/style.comp.css
: file returned from step 1css/any-other-file.css
: anyother css file which needs to be combined in the finalP.S.: If you have just one file this step doesn't make any sense. So skip, but make sure your keep track of the trail i.e. which file is returned from which step and which one is being put.
This is the command with which your css is going to have vendor prefixes like -webkit-
, -moz
, -ms-
and -o-
, which otherwise you'd have to add manually.
Vendor prefixing is a way for browser makers to add support for new CSS features before those features are fully supported in all browsers.
How and why you would want to use CSS vendor prefixes in your website
postcss
: package name (autoprefixer is a part of postcss)--use
: list of postcss plugins to use (i.e autoprefixer, here)autoprefixer
: package name which prefixes our css-b
: b is for browsers so we can specify which browsers to make our css compatible for"last 10 versions"
: this fetches last 10 versions of all major broswers from caniuse.comcss/style.concat.css
: address of file we're going to put-o
: option for outputcss/style.prefix.css
: address output fileP.S.: While writing last 10 versions
I faced an issue, which was because of the double quotes as it is included inside another double quotes, so we need to escape this using \
before every double quote inside the main double quotes.
This will reduce your css file size to a great extent resulting in faster page loads.
node-sass
: package namecss/style.prefix.css
: file containing the input for this command (uncompressed css)css/style.css
: address of the output file (compressed css)--output-style compressed
: command to make the compressionP.S.: You can choose to output the compressed css into another css file, like style.min.css
which is alright, but you'll then need to add a link tag to your html to include this.
At last you want to run all of this by running just one command.
npm-run-all
: package namecompile:sass concat:css prefix:css compress:css
: list of commands in order of their sequence for executionFunFact: You can add --parallel
after the package name in this to run all the processes simultaneosly which is helpful in some places, but not here. Why? Because here, each script is like a function which takes a file as input and its output file is required in the next function/script, which isn't possible if they run simultaneously.
Finally, we've setup our build process which means your project is now one command away from being ready for deployment, which is **npm-run-all build:css**
. So in a nutshell this process,
I love writing as much as learning new technologies. However the truth is, the journey is never easy. And that is the only reason I write what I write, coz these were once the questions that I faced as a beginner and I want to help others who have the same ones that I did.
So I am curious about what difficulties or questions you once faced or are facing as a beginner? 🤷♂️ Reply in this thread, as we can all learn from each other. 👇