The Wayback Machine - https://web.archive.org/web/20240906004008/https://www.geeksforgeeks.org/css-preprocessor-less/
Open In App

CSS Preprocessor LESS

Last Updated : 04 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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. 

CSS-Preprocessor-LESS-copy

Pre-Requisites:

  • HTML
  • CSS

System Requirements:

  • Operating System: Cross-platform
  • Browser Support: IE (Internet Explorer 8+), Firefox, Google Chrome, Safari.

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:

  • Write the LESS code in a file.
  • Compile the LESS code into CSS code using the command lessc style.less style.css.
  • Include the compiled CSS file in the html file.

Features:

  • Variables: Variables can be used to store CSS values that may be reused. They are initialized with @. 

style.less

CSS
@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

CSS
h1 {
  color: #ddd;
  background: #512DA8;
}
  • Mixins: Mixins are a way of including a bunch of properties from one rule-set into another rule set. 

style.less

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;
}

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

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;
}
  • Nesting: LESS gives you the ability to use nesting. 

style.less

CSS
@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

CSS
.carousel {
  background: #512DA8;
}
.carousel .carousel-item {
  height: 300px;
}
.carousel .carousel-item img {
  position: absolute;
  top: 0;
  left: 0;
  min-height: 300px;
}
  • Mathematical Operations: Arithmetical operations +, -, *, / can operate on any number, color, or variable. 

style.less

CSS
@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

CSS
.carousel-item {
  height: 300px;
}
.carousel-item .item-large {
  height: 600px;
}
  • Functions: LESS provides a variety of functions like math, list, string, color operations, color blending, etc. 

style.less

CSS
@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

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

Example: File name gfg.html

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

CSS
@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

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

Output:

Image

Advantages:

  • LESS is cross-browser compatible.
  • LESS provides a list of operators making it easy for users to code.
  • Maintenance is easy due to the use of variables.

Disadvantages:

  • LESS provides fewer frameworks as compared to SASS.
  • It can be tricky to those who are new to CSS.


Similar Reads

CSS Preprocessor SASS
The SASS is the abbreviation of Syntactically Awesome Style Sheets. It is a CSS pre-processor with syntax advancements. The style sheets in the advanced syntax are processed by the program and compiled into regular CSS style sheets, which can be used in the website. It is a CSS extension that allows to use feature like variables, nesting, imports,
3 min read
Difference between CSS Custom Properties and Preprocessor Variables
In this article, we will learn the difference between CSS Custom properties and Preprocessor Variables. CSS Custom Properties: CSS custom properties are also referred to as CSS variables or cascading variables. These entities contain specific values that will be reused in the entire document &amp; can be accessed using the var() function (e.g., col
3 min read
Difference between CSS Variables and Preprocessor
In this article, You will learn the differences between the CSS variables (cascading variables) and CSS preprocessors. They both implement the extra capabilities into CSS in their own different ways. CSS variables and preprocessors are two different things. Both have advantages and disadvantages. The CSS preprocessor is a language that can be used
6 min read
What is HTML Preprocessor ?
In this article, we will learn about the HTML pre-processor &amp; will explore the pre-processor used for HTML. As its name suggests, the HTML pre-processor is the first stage of the whole compiling process which includes removing the comments, expanding the macros, the inclusion of the headers, etc. When we write HTML and CSS, sometimes we need to
4 min read
Less.js @import At-Rules less
In this article, we will see that Less.js @import At-Rules is basically used to import the file which is in the source code. And we can put the @import statement anywhere in the source code. And @import At-Rules allow us to spread the less code over to different files. Using the @import keyword we can separate and maintain our code structure easily
2 min read
Less.js @import At-Rules css
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 makes CSS more powerful. LESS provides cross-browser compatibility. A programming language called CSS pre-processor is used to enhance CSS and compile it for use by web br
3 min read
Less.js CSS Guards
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 compi
2 min read
Create a Read More Read Less Button using HTML CSS and JavaScript
The "read more - read less" button is a commonly used feature that enhances user experience by providing a convenient way to reveal or hide content. The article covers two distinct approaches, including Display Inline and Display Block. Table of Content Using Display InlineUsing Display BlockUsing Display InlineCreate a separate HTML, CSS, and Java
4 min read
How to pre-compile LESS into CSS ?
In this article, we will discuss how to pre-compile the LESS file into a CSS file &amp; will understand its implementation through the example. Introduction: LESS is a simple CSS pre-processor that makes it possible to create manageable, customizable, and reusable style sheets for websites. LESS is a backward-compatible language extension for CSS a
3 min read
How to create a LESS file and how to compile it ?
LESS (which stands for Leaner Style Sheets) is a backward-compatible language extension for CSS. CSS itself is great for defining styles and applying them to various HTML elements but it has a few limitations. Limitations of CSS: Writing CSS code becomes exhausting, especially in big projects.Maintaining CSS code is difficult due to the lack of pro
2 min read
How many numbers in the given array are less/equal to the given value using the percentile formula ?
The following approach covers how to calculate how many numbers in the given array are less or equal to the given value using the percentile formula in JavaScript. Problem Statement: You are given an array containing different integer values and also an integer value. You need to see how many numbers in the given array are less or equal to the give
3 min read
Less.js Misc image-height() Function
Less.js is a simple CSS pre-processor that facilitates the creation of manageable, customizable, and reusable style sheets for websites. Since CSS is a dynamic style sheet language, it is preferred. LESS is adaptable, so it works with a wide range of browsers. Only CSS that has been created and processed using the CSS pre-processor, a computer lang
3 min read
Less.js Type Functions
In this article, we will see the Type Functions provided by Less.js. Less.js (Leaner Style Sheets) is an extension to normal CSS which basically enhances the abilities of normal CSS and gives it superpowers. Basically, Type Functions are used to check whether a particular argument (provided to the function) is of a particular type (like string, num
6 min read
Less.js Extend Duplication Detection
LESS is a simple CSS pre-processor that makes it possible to create manageable, customizable, and reusable style sheets for websites. LESS is a dynamic style sheet language that increases the working power of CSS. LESS is cross-browser compatible. CSS pre-processor is a scripting language that improves CSS and gets compiled into regular CSS syntax
3 min read
Less.js Color Channel hsvhue() Function
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 usage by web browsers using a programming language ca
3 min read
Less.js Mixins Guarded Namespaces
Less.js is a simple CSS pre-processor that facilitates the creation of manageable, customizable, and reusable style sheets for websites. Given that CSS is a dynamic style sheet language, it is preferable. Since LESS is adaptable, it can be used by many different browsers. Only CSS that has been created and processed using a computer language known
3 min read
Less.js Parent Selectors
The Parent Selectors in LESS.js are used to select elements with the specific parents when modifying an existing class or pseudo-class to an existing selector. It is denoted by the ‘&amp;’ (ampersand) operator. It is important in the pre-processor to have a clean code with the hierarchy of like DOM tree in CSS. Different types of Parent Selectors:
3 min read
How to check if date is less than 1 hour ago using JavaScript ?
Given a date and the task is to check whether the given date is less than 1 hour ago or not with the help of JavaScript. Approach 1: Count the milliseconds of the difference between the current and prev_date.If those are greater than milliseconds in 1 hour, then it returns false otherwise returns true. Example: This example implements the above app
2 min read
Less.js Extend Use Cases for Extend
LESS is a simple CSS pre-processor that makes it possible to create manageable, customizable, and reusable style sheets for websites. LESS is a dynamic style sheet language that increases the working power of CSS. LESS is cross-browser compatible. CSS pre-processor is a scripting language that improves CSS and gets compiled into regular CSS syntax
3 min read
Less.js Misc default() Function
Less.js is a simple CSS pre-processor that facilitates the creation of manageable, customizable, and reusable style sheets for websites. Since CSS is a dynamic style sheet language, it is preferred. LESS is adaptable, so it works with a wide range of browsers. Only CSS that has been created and processed using the CSS pre-processor, a computer lang
3 min read
How to import LESS through npm ?
LESS stands for Leaner Style Sheets. It is a backward-compatible language extension for CSS. Web pages can be styled by writing appropriate code in a file with .less extension and then converting it in a CSS file. Steps to install LESS: Step 1: To install LESS, first make sure that node and npm are already installed in the system. If not, then inst
2 min read
Less.js Mixins !important keyword
Less.js is a CSS extension that provides some additional features to traditional CSS. The !important keyword is basically used to mark some important properties, mixins, variables, etc throughout the page as it can be difficult to track which property could be important or not so this keyword can be used to do the work. In this article, we will see
3 min read
Less.js jsList length() Function
Less.js is a simple CSS pre-processor that facilitates the creation of manageable, customizable, and reusable style sheets for websites. It is better since CSS makes use of a dynamic style sheet language. LESS is flexible enough to work in a wide range of browsers. To create and improve CSS so that web browsers can use it, a computer language known
3 min read
Introduction to LESS
LESS is a simple CSS pre-processor that makes it possible to create manageable, customizable, and reusable style sheets for websites. LESS is a dynamic style sheet language that increases the working power of CSS. LESS is cross-browser compatible. CSS pre-processor is a scripting language that improves CSS and gets compiled into regular CSS syntax
4 min read
JavaScript RangeError - Repeat count must be less than infinity
This JavaScript exception repeat count must be less than infinity occurs if the passed argument of String.prototype.repeat() method is infinity. Output message: RangeError: argument out of range (Edge) RangeError: repeat count must be less than infinity and not overflow maximum string size (Firefox) RangeError: Invalid count value (Chrome) Error Ty
2 min read
Less.js Color Blending average() Function
Less.js is a simple CSS pre-processor that facilitates the creation of manageable, customizable, and reusable style sheets for websites. CSS is more beneficial because it makes use of a dynamic style sheet language. LESS is flexible enough to work in a wide range of browsers. The CSS pre-processor is a computer language that is used to augment and
4 min read
Less.js @import At-Rules multiple
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 usage by web browsers using a programming language ca
3 min read
How to define scope in the context of variables in LESS ?
Variable scope is used to specify the availability of variables in certain parts of the code. Firstly, the variables are searched from the local scope (i.e the variables which are declared within the code block of a particular selector); if they are not found, then they are searched from the parent scope by the compiler. Let's create an index.html
1 min read
Less known Angular Features You Need to Know
Following are some interesting features of angular that can help you in SEO, application performance improvements, and improving code readability. 1. Title tags: These are displayed on SERPs(Search Engine Results Pages) as the clickable &amp; touchable heading for your search text. They’re important for usability, Search engine optimization and soc
4 min read
Less.js Color Channel green() Function
Less.js is a simple CSS pre-processor that facilitates the creation of manageable, customizable, and reusable style sheets for websites. Because of this dynamic style sheet language, CSS is more useful. Interoperability between browsers is a characteristic of LESS. The CSS pre-processor is a computer language that is used to augment and compile CSS
3 min read
Article Tags :