Less.js Variable Interpolation
Last Updated :
26 Apr, 2025
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" >GeeksforGeeks</ 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" >GeeksforGeeks</ h1 >
< h3 >< b >Less.js Variable Interpolation</ b ></ h3 >
< div class = "container" >
< p class = "text" >GeeksforGeeks</ 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
Similar Reads
What is Variable Interpolation in SASS ?
SASS is a strong CSS preprocessor that adds a number of features to improve the usability and maintainability of stylesheets. Variable interpolation is one such feature that permits the dynamic insertion of variable values into selectors and properties. In this article, we will learn the variable in
3 min read
Less.js Lazy Variables Evaluation
Less.js is a simple CSS pre-processor that facilitates the creation of manageable, customizable, and reusable style sheets for websites. It is a dynamic style sheet language that boosts the functionality of CSS. Cross-browser interoperability is offered via LESS. CSS is improved and compiled for usa
3 min read
Less.js Variables Combinatorial Explosion
LESS works as an extension to the regular CSS and gives it some additional features to work with. , which is used to generate all possible combinations of the selector's list which are separated by a comma. In this article, we are going to learn about the combinatorial explosion which is available i
3 min read
SASS Interpolation
Interpolation is an insertion. Interpolation allows us to interpolate SASS expressions into a simple SASS or CSS code. This means you can define ( some part or the whole ) selector name, property name, CSS at-rules, quoted or unquoted strings, etc, as a variable. Interpolation is a new principle and
1 min read
Less.js Variables
LESS (Leaner Style Sheets) is a simple CSS pre-processor that facilitates the creation of manageable, customizable, and reusable style sheets for websites. It is a dynamic style sheet language that enhances the working power of CSS. LESS supports cross-browser compatibility. CSS pre-processor is a s
4 min read
Less.js Installation
LESS (Leaner Style Sheets) is a simple CSS pre-processor that facilitates the creation of manageable, customizable, and reusable style sheets for websites. It is a dynamic style sheet language that enhances the working power of CSS. LESS supports cross-browser compatibility. CSS pre-processor is a s
4 min read
Linear Interpolation in MATLAB
Interpolation is a numerical method of finding new data points by finding a pattern in a given set of discrete data points. There are various types and methods of interpolation in the field of Numerical Analysis such as linear interpolation, cubic interpolation, spline interpolation, etc. The key po
3 min read
Interpolation in Machine Learning
In machine learning, interpolation refers to the process of estimating unknown values that fall between known data points. This can be useful in various scenarios, such as filling in missing values in a dataset or generating new data points to smooth out a curve. In this article, we are going to exp
7 min read
String Interpolation in JavaScript
These are the following ways to interpolate the given strings: 1. Template Literals (ES6)Template literals are the most modern and preferred method for string interpolation in JavaScript. They use backticks (`` ` ``) and ${} syntax to embed expressions. [GFGTABS] JavaScript const n = "Alice
2 min read
Less.js Variables Default Variables
LESS (Leaner Style Sheets) is a simple CSS pre-processor that facilitates the creation of manageable, customizable, and reusable style sheets for websites. It is a dynamic style sheet language that enhances the working power of CSS. LESS supports cross-browser compatibility. CSS pre-processor is a s
3 min read