What is LESS and SASS??
LESS and SASS are 'Css preprocessors'. But after than one question arise in our mind that is what is preprocessor and why to use us. ‘CSS preprocessors’, which offers to add mind-blowing
features to the Cascading Style sheet language, and equip the modern-day
front end engineer with tons of nitro and increase productivity.
First of all Let we know why we use preprocessors.? In simple Css we faced many problems like as CSS stylesheets are getting larger, more complex, and harder to maintain in multypage website. To Overcome this problem, the concept of Css Preprocessors was born. It offering an advanced way of writing CSS, which extends the basic
functionalities. This advanced code is later compiled as normal CSS code
using respective compilers (which depends on what preprocessor you are
using), which the browser will understand.
 |
Css Preprocessor
|
Which Preprocessors to use?
1. LESS
2. SASS
3. Stylus
LESS (Leaner CSS) is designed by Alexis Sellier. LESS is a dynamic style sheet language that extends CSS and, just like SASS, it has dynamic behavior such as variables, mixins, operations and functions.
It allows you to write CSS in a simpler way by just using and combining mixins, functions, and so on. LESS also increases readability and organization of CSS using imports, nested rules and comments with .less extension.
Resources you need to complete this tutorial:
Step 1:
Installing SimpleLESS
For our LESS client-side compiler we’re going to use SimpleLESS because it is simple to use and it’s free so go ahead and download it.
If you don’t see the current version on the top bar, that means it’s not working properly so you need to download it again.
Step 2:
Prepare the Markup and LESS Javascript
Before we explain LESS functionalists, we need to prepare our markup and LESS files. So go ahead and create new folder then name it “LESS” or whatever you prefer. Next, inside the LESS folder, create an HTML file and name it index.html and copy and paste the code below. Notice that we have included the LESS library below the less style sheet.
Step 3:
Next, create a new file and name it style.less. For the purpose of showing you how to compile LESS, let’s put some LESS sample codes in this file. Go ahead, copy and paste the code below to style.less file.
So, by this time, it is expected you’ll have a file structure similar to this.
Step 4:
Compiling LESS using SimpLESS
To compile LESS file to CSS, run the SimpLESS application and drag and
drop the LESS folder that we created above in the application.
You can see that the folder was successfully added to the application if
the folder appears on the list of the application. Notice that you can
see a text that notifies you if the file has been compiled or not. In
this example, as you can see in the right corner, it says “never
compile” so this means that our LESS folder was never compiled.

The application also offers different features namely:
prefix, minify, love. You can see these on the right corner under the
status notification. Please take note that all these features can be
turned on or off individually. Consider the following use of these
features.
Prefix - allows you to attain cross-browser compatibility using vendor prefixes.
Minify- compress your CSS document that’s integrated CSS-minification.
Love- these will comment or state SimpLESS to your document.
So if you open again the LESS folder, you can see that a CSS file was
added to the folder. Make sure you link the compiled CSS file
(style.css) to your index.html to add the styles you added to your LESS
file.
Declaring Variables:
Just like
SASS,
LESS also uses variables. If you have coded before, you
know how variables make each process easier. For a pre-processor like
LESS, variables are being used as placeholders for each value of the
styles being used throughout the LESS file. LESS uses the
@ sign to
define a
variable. Let’s put this into practice to know how this works.
Open your
LESS file and put in the following code. For this code, I
created 3 variables for width, border and color. I added the
myWidth and
myBorder variables on the H2 tag while I added the
myColor variable to our list with the ID of
myWork.
So if you are going to open your style.css file, you can see that the
code was updated. Go ahead and run this code into your browser and you
can see similar to the image below.
LESS Mixins
One of the best features of any CSS preprocessor is the ability to
create mixins, which allow you to tie a bunch of properties and
customizable values all into one nice little package. Sometimes we want to create styles that can be repeated throughout the
style sheet. Good thing mixins are here to help. Just like SASS, it also
has the same features that you can add on each elements repeatedly but
without the use of @include directive. Instead, just a simple class
style directive would suffice. To show you how mixins work in LESS.Let's see example below
This takes all of the box-shadow code and wraps it up nicely into a
single mixin that can be implemented with custom values (or the defaults
that we used above). Now, instead of writing a huge chunk of code every
time you want a box-shadow, this is all you need:
Nested Rules:
The example above can be simplified even further by utilizing LESS’s
nested rules feature. Watch how we can throw all the box styles into the
same set of brackets and eliminate the need to type out the div ID
every time.
As you can see, all of the box item’s link styles, link hover styles and paragraph styles are contained within the same parent.
LESS Operators
One of the best features of LESS is the ability to perform mathematical
operations. Using a style fixed value, you can add, subtract, multiply
or even divide. LESS allows you to perform operations on variables. This can save you a lot of mental math in the long run.
As an oversimplified example, say you wanted to create a box that was
twice as wide as it was tall. You could declare the height in a
variable and then set another variable that is twice that number. Let’s see this in action.
Then later in your code you would use these variables to set the height
and width of your box. If you were to then go back and change boxHeight
to 400px, boxWidth would automatically be set to 800px.
LESS Functions
LESS provides a bunch of functions to manipulate elements and styles.
For this tutorial we will focus on color functions if you want to learn
more about LESS functions you can CLICK HERE to learn more.
Check out the series of color functions below.
darken (color, amount)
lighten (color, amount)
saturate(color, amount)
desaturate(color, amount)
alpha(color)
Go ahead and open your style.less file and copy the following codes.
As you can see that the background color was darkened by 60% and then
the H1 and H2 tags were lightened by 10% and also the color of
paragraph tags were lightened by 20%. So when you run this in your
browser, you can get similar result just like the image below.