Open In App

CSS Preprocessor LESS

LESS stands for Leaner Style Sheets. It is a backward-compatible language extension for CSS. It allows us to use features like variables, nesting, mixins, etc, all in a CSS-compatible syntax. LESS is influenced by SASS and has influenced the newer “SCSS” syntax of SASS. LESS was used in Bootstrap 3 but was replaced by SASS in Bootstrap 4. 

Pre-Requisites:



System Requirements

File Type: All LESS files must have the .less file extension.



Working: A web browser does not understand the LESS code itself. That is why you will require a LESS pre-processor to change LESS codes into simple standard CSS code.

Working Steps:

Features:

style.less




@lt-gray: #ddd;
@background-dark: #512DA8;
@carousel-item-height: 300px;
h1 {
    color:@lt-gray;
    background:@background-dark;
}

Syntax: To compile the above less code to CSS code write the following command-

lessc style.less style.css

The compiled CSS file comes to be: 

style.css




h1 {
  color: #ddd;
  background: #512DA8;
}

style.less




zero-margin {
    margin:0px auto;
    background: white;
}
  
.row-header {
    margin:zero-margin;
    padding:0px auto;
}
  
.row-content {
    margin:zero-margin;
    border-bottom: 1px ridge;
    min-height:400px;
    padding: 50px 0px 50px 0px;
}

Syntax: To compile the above less code to CSS code write the following command-

lessc style.less style.css

The compiled CSS file comes to be: 

style.css




zero-margin {
  margin: 0px auto;
  background: white;
}
.row-header {
  margin: zero-margin;
  padding: 0px auto;
}
.row-content {
  margin: zero-margin;
  border-bottom: 1px ridge;
  min-height: 400px;
  padding: 50px 0px 50px 0px;
}

style.less




@lt-gray: #ddd;
@background-dark: #512DA8;
@carousel-item-height: 300px;
.carousel {
    background:@background-dark;
  
    .carousel-item {
        height: @carousel-item-height;
        img {
            position: absolute;
            top: 0;
            left: 0;
            min-height: 300px;
        }
    }
}

Syntax: To compile the above less code to CSS code write the following command-

lessc style.less style.css

The compiled CSS file comes to be: 

style.css




.carousel {
  background: #512DA8;
}
.carousel .carousel-item {
  height: 300px;
}
.carousel .carousel-item img {
  position: absolute;
  top: 0;
  left: 0;
  min-height: 300px;
}

style.less




@lt-gray: #ddd;
@background-dark: #512DA8;
@carousel-item-height: 300px;
  
.carousel-item {
    height: @carousel-item-height;
}
  
.carousel-item .item-large {
    height: @carousel-item-height *2;
}

Syntax: To compile the above less code to CSS code write the following command-

lessc style.less style.css

The compiled CSS file comes to be: 

style.css




.carousel-item {
  height: 300px;
}
.carousel-item .item-large {
  height: 600px;
}

style.less




@base:0.5;
@width: 0.8;
  
.class {
    width: percentage(@width); // Returns `80%`
    color: saturate(@base, 5%);
    background-color: light(@base, 25%), 8;
}

Syntax: To compile the above less code to CSS code write the following command-

lessc style.less style.css

The compiled CSS file comes to be: 

style.css




.class {
  width: 80%;
  color: saturate(0.5, 5%);
  background-color: light(0.5, 25%), 8;
}

Example: File name gfg.html




<!DOCTYPE html>
<html>
  
<head>
    <meta charset="utf-8" />
    <title>LESS</title>
    <meta name="viewport" 
        content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="./css/style.css">
</head>
  
<body>
    <div class="head">Welcome to GeeksforGeeks.
        <ul class="list">
            <li class="a">Algo</li>
            <li>DS</li>
            <li class="a">Languages</li>
            <li>Interviews</li>
            <li>CS subjects</li>
        </ul>
    </div>
</body>
  
</html>

File name style.less




@color-primary: #009900
@font-pri: Sans-Serif
@font-sec: Helvetica
  
body{ 
    color: @color-primary; 
    font-size: 40px
  
.list{ 
    font-family: @font-pri; 
    font-size: 20px
    .a{ 
        font-family: @font-sec; 
        font-size: 10px
    

File name style.css which we get after transpiling style.less




body { 
color: #009900
font-size: 40px
.list { 
font-family: Sans-Serif
font-size: 20px
.list .a { 
font-family: Helvetica
font-size: 10px

Output:

 

Advantages:

Disadvantages:


Article Tags :