In this article we learn about,
1.What is CSS Preprocessor?
2.Installations
3.How to useVariables?
4.Preprocessor Based Popular CSS Frameworks
5.What is Crunch?
6.How to use math function in LESS?
7.How to use arithmetic in LESS?
8.Mixins
9.Benefits of CSS preprocessor
10.What is next?
1.What is CSS preprocessor?
CSS preprocessor is nothing but which is a scripting language that extends CSS and gets compiled into regular CSS syntax.
Popular CSS preprocessors
1. Sass (http://sass-lang.com/)
2. LESS (http://lesscss.org/)
3.Stylus (http://learnboost.github.io/stylus/)
In these preprocessor particularly, Sass and LESS progress with rapid speed and cool features.
2.Installations
Sass (Syntactically Awesome StyleSheets)
If you’re using Windows, you may need to install Ruby first. In command line just use,
gem install sass
To run Sass from the command line, just use
sass input.scss output.css
You can also tell Sass to watch the file and update the CSS every time the Sass files changes:
sass –watch input.scss:output.css
LESS
1.Go get yourself a copy of less.js;
2.Create a file to put your styles in, like style.less, or else //cdnjs.cloudflare.com/ajax/libs/less.js/1.6.0/less.min.js
Add the following code to your HTML’s
:
3.How to useVariables?
Variables can be declared and used throughout the stylesheet.
Sass
Sass variables are prepended with the $ symbol and the value and name are separated with a semicolon, just like a CSS property.
Ex:
LESS
LESS variables are similarly as Sass variables, In the variable names instead of $ symbol prepended with the @ symbol.
4.Preprocessor Based Popular CSS Frameworks
Foundation: Sass based CSS framework (http://foundation.zurb.com/)
Bootstrap: LESS based CSS framework (http://getbootstrap.com/)
5.What is Crunch?
Crunch is GUI based less to css compiler. In this article Let we see how to write LESS based css with Crunch .
Just go Crunch website , download and install crunch editor.
In Crunch editor we can create our less file, then click Crunch file button to convert less file to css file.
Once generated css file in that we can see less variables replaced by the correspoding values. Following figure depicts this process. In that css fille look like as ordinary css file.
Perhaps If we need minified file of css file just click setting icon in Crunch editor and then checked Minify Crunched Css check box. Now again click the Crunch button Vow!!! we can get minified css.
6.How to use math function in LESS?
@calwidth : floor(@containerwidth/2);
Here we applied floor math function to @calwidth variable and dived by 2.
Ex:
LESS has the ablity to execute math function in the above example ‘test.less’ file execute ‘floor’ math function and generated calculated ‘max-width’ in test.css file .
7.How to use arithmetic in LESS?
Next , we see Arithmetic (+, -, *, /) operation,
Ex:
.margin-top{margin-top:25px;margin-left:4px;margin-right:4px;margin-right:40px !important;margin-bottom:80px;}
9.Benefits of CSS preprocessor
1. Cleaner code with reusable pieces
DRY -> Don’t Repeat Yourself, is a principle to reduce or try to avoid repetition in software engineering. Using CSS preprocessor we can write modular and reuseable code.
2. More flexibility to do things on the fly
Easy to compress and remove comments in source code of CSS file, fun with conditional statements, loops and mathematical operations.
3.Sharable snippets and libraries
Once our team builds our own preprocessor we would share that with other people in web community. Additionally we can get more scalable and optimize third party libraries.
10.What is next?
Except Crunch, some of other GUI Tools also available to compile less to css, such as Mixture, SimpLESS, CodeKit. You may go through those tools.