Open In App

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 scripting language that improves CSS and gets compiled into regular CSS syntax so that it can be used by the web browser. It is also a backward-compatible language extension for CSS that provides functionalities like variables, functions, mixins, and operations that enable us to build dynamic CSS.

Description for variables: The Variables in LESS.js govern the common values used in a single location, ie, they are known to keep values stored in them and can be used anywhere within the definition of code. LESS allows you to use these variables to change the specific value in the entire code. It might get troublesome when we need to change one particular value in our CSS code, but with the help of variables, it can easily be done.



Variables in less:

Syntax:



a,
.link {
    color:blue;
}

Syntax:

@my-selector: banner;

.@{my-selector}
{
   //property values
}

Syntax:

@primary-color: blue;
@bgcolor: red;

.section 
{
  @color: primary-color;

  .element 
  {
     color: @@color;
  }
}

Syntax:

.lazy-eval
{
    height: @va;
}

Syntax:

.widget
{
  color:blue;
  background-color: $color;
}

Syntax:

@color:red;
// first declaration of @color
@import "style.less"; 
// this will override @color defined previously
@color: green; 
p {
   color : @color;
}

Example 1: This example demonstrates the use of the default variable in Less.js.

Filename: index.html




<!DOCTYPE html>
<head>
    <link rel="stylesheet"
        href="Three.css" type="text/css" />
</head>
<body>
    <div><br><br>
        <h1>Welcome to GeeksforGeeks</h1>
        <h3><b>Less.js Variable</b></h3>
    </div>
</body>
</html>

Filename: One.less




@color: red;

Filename: Two.less




@import "One.less"
@color: green
@normal:black;
h1
{
   text-align: center;
   color : @color;
}
h3
{
   text-align: center;
   color: @normal;   
}

Now, to compile the above LESS code to CSS code, run the following command:

less Two.less Three.css

The compiled CSS file comes to be: 

Filename: Two.css




h1 {
    text-align: center;
    color: green;
}
h3 {
    text-align: center;
    color: black;
}

Output: 

 

Example 2: The following example, demonstrates the use of variables in Less.js.

Syntax:

@my-selector: banner;

.@{my-selector}

Filename: index.html




<!DOCTYPE html>
<head>
    <link rel="stylesheet"
        href="style.css" type="text/css" />
</head>
<body class="banner3">
    <div><br><br>
        <h1 class="banner">Welcome to GeeksforGeeks</h1>
        <h3 class="banner2"><b>Less.js Variable</b></h3>
    </div>
</body>
</html>

Filename: style.less




@primary:black;
@color:green;
@normal:white;
@my-selector: banner;
@hello:banner2;
@one:banner3;
  
.@{my-selector} {
    font-weight: bold;
    line-height: 20px;
    color: @color;
    margin: 0 auto;
    text-align: center;
}
.@{hello} {
    text-align: center;
    color: @normal;
}
.@{one}{
    background-color: @primary;
}

Now, to compile the above LESS code to CSS code, run the following command:

less styles.less style.css

The compiled CSS file comes to be: 

Filename: style.css




.banner {
    font-weight: bold;
    line-height: 20px;
    color: green;
    margin: 0 auto;
    text-align: center;
}
.banner2 {
    text-align: center;
    color: white;
}
.banner3 {
    background-color: black;
}

Output:

 

Reference: https://lesscss.org/features/#variables-feature


Article Tags :