Less.js is a simple CSS pre-processor that facilitates the creation of manageable, customizable, and reusable style sheets for websites. This dynamic style sheet language gives CSS more functionality. LESS provides interoperability across browsers. A programming language called CSS pre-processor is used to enhance and compile CSS so that web browsers may use it. It is also a CSS language extension that provides features like variables, functions, mixins, and operations that let us create dynamic CSS while yet keeping backward compatibility.
The LESS Variables, as they are called, control the common values used in a specific area and can be used anywhere inside the definition of code. LESS can be used with these variables to change a certain value throughout the entire code. Using variables makes it easier when we need to change a certain value in our CSS code, which can be difficult otherwise.
Variables in Less are generally used in storing values and are used repetitively in the less code. But not only values but attributes of different nature are interpolated with variables and used likewise. There are four places where variables can be used:
Selector’s Name: We can store the names of selectors in variables and use them before CSS rules.
Syntax:
@var: sel; .@{var} { ... }
URLs: We can also store the whole or a part of a path/directory inside the variables.
Syntax:
@var: "URL_name" sel{ property: url("@{var}") }
Import Statements: We can also store the whole or a part of a path/directory inside the variables for importing files from that location.
Syntax:
@var: "URL_name" @import "@{var}/...";
Properties: Like selectors, we can also store names of properties inside the variables and we can use them repetitively.
Syntax:
@var: property_name; selector { @{var}: value; }
To know how to Compile LESS code into CSS code open this.
Example 1: The example below shows the interpolation of variables in the Selector’s Name and URLs.
HTML
<!DOCTYPE html> < html > < head > < link rel = "stylesheet" type = "text/css" href = "styles.css" /> </ head > < body > < h1 style = "color:green" >neveropen</ h1 > < h3 >< b >Less.js Variable Interpolation</ b ></ h3 > < div class = "container" > </ div > </ body > </ html > |
styles.less:
CSS
@body-bg- color : #eeeeee ; @text- color : rgb ( 0 , 200 , 100 ); @container-bg: rgb ( 220 , 43 , 55 ); @image: "1.png" ; @var: container; body { background : @body-bg-color; } .@{var}{ height : 200px ; width : 450px ; background : url ( "@{image}" ); } |
1.png:
Now, to compile the above LESS code to CSS code, run the following command:
lessc styles.less styles.css
The compiled CSS file comes to be:
styles.css:
CSS
body { background : #eeeeee ; } .container { height : 200px ; width : 450px ; background : url ( "1.png" ); } |
Output:
Example 2: The example below shows the interpolation of variables in Import Statements and Properties.
HTML
<!DOCTYPE html> < html > < head > < link rel = "stylesheet" type = "text/css" href = "style.css" /> </ head > < body > < h1 style = "color:green" >neveropen</ h1 > < h3 >< b >Less.js Variable Interpolation</ b ></ h3 > < div class = "container" > < p class = "text" >neveropen</ p > </ div > </ body > </ html > |
style.less:
CSS
@var: "test.less" ; @import "@{var}" ; |
test.less: This is the less file that is being imported.
CSS
@body-bg- color : #eeeeee ; @text- color : rgb ( 0 , 200 , 100 ); @container-bg: rgb ( 220 , 43 , 55 ); @var: color; body { background : @body-bg-color; } .container{ height : 200px ; width : 400px ; background-@{var}: ( #00821e ); } .text { padding : 70px 0px 0px 95px ; @{var} :yellow; font-size : 2 rem; } |
Now, to compile the above LESS code to CSS code, run the following command:
lessc style.less style.css
The compiled CSS file comes to be:
style.css:
CSS
body { background : #eeeeee ; } .container { height : 200px ; width : 400px ; background-color : #00821e ; } .text { padding : 70px 0px 0px 95px ; color : yellow; font-size : 2 rem; } |
Output:
Reference: https://lesscss.org/features/#variables-feature-variable-interpolation