LESS (Leaner Style Sheets) is a CSS preprocessor that extends CSS with dynamic behavior, including variables, nesting, mixins, and mathematical operations, all while maintaining compatibility with standard CSS.

Pre-Requisites:
- Basic knowledge of HTML and CSS
Key Features of LESS
- Variables: Store reusable values like colors, fonts, and measurements using the @ symbol.
- Mixins: Include reusable groups of CSS properties in other rulesets.
- Nesting: Write CSS in a hierarchical structure reflecting the HTML.
- Mathematical Operations: Perform arithmetic on CSS values.
- Functions: Use built-in functions for math, color manipulation, and more.
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.
How LESS Works
A browser cannot directly process LESS code. A LESS preprocessor compiles the LESS file into standard CSS that browsers can interpret.
Working Steps:
- Write the LESS code in a file with a .less extension.
- Compile the LESS file into CSS using the command:
- bash:
- lessc style.less style.css
- Include the compiled CSS file in your HTML file.
Here is a list of all the features of LESS, each explained one by one:
Features:
1. Variables: Variables can be used to store CSS values that may be reused. They are initialized with @.
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;
}
2. Mixins: Mixins are a way of including a bunch of properties from one rule-set into another rule set.
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;
}
3. Nesting: LESS gives you the ability to use nesting.
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;
}
4. Mathematical Operations: Arithmetical operations +, -, *, / can operate on any number, color, or variable.
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;
}
5. Functions: LESS provides a variety of functions like math, list, string, color operations, color blending, etc.
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
HTML
<!--Driver Code Starts{-->
<html>
<head>
<link rel="stylesheet" href="./css/style.css">
</head>
<!--Driver Code Ends }-->
<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>
<!--Driver Code Starts{-->
</body>
</html>
<!--Driver Code Ends }-->
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;
}
}
style.css
body {
color: #009900;
font-size: 40px;
}
.list {
font-family: Sans-Serif;
font-size: 20px;
}
.list .a {
font-family: Helvetica;
font-size: 10px;
}
Output:

Advantages of LESS
- 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 of LESS
- LESS provides fewer frameworks as compared to SASS.
- It can be tricky to those who are new to CSS.
Best Practices for Using LESS
- Modularize Your Code: Divide your styles into logical, reusable modules or partials to enhance maintainability.
- Limit Nesting Levels: Avoid deep nesting to prevent overly specific selectors and maintain readability.
- Utilize Variables: Define variables for commonly used values like colors and fonts to ensure consistency and simplify updates.
Similar Reads
CSS Preprocessor SASS
Sass (Syntactically Awesome Style Sheets) is a CSS preprocessor that enhances standard CSS by introducing features like variables, nesting, imports, mixins, and inheritance, all while maintaining compatibility with all CSS versions. Key Features of Sass:Variables: Store reusable values such as color
4 min read
What are CSS preprocessors?
CSS preprocessors are used to write styles in a special syntax that is then compiled into standard CSS. They extend the functionality of standard CSS by introducing features like variables, nesting, mixins, and functions. By using preprocessors, you can take advantage of advanced features that are n
3 min read
Haml | HTML Pre-processor
As its name suggests the pre-processor is the first stage of the whole compiling process it includes removing the comments, expanding the macros, the inclusion of the headers, etc. In HTML and CSS when it comes to writing it, It is a bit crucial as we have to do the same job again and again like clo
3 min read
What is HTML Preprocessor ?
In this article, we will learn about the HTML pre-processor & 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
4 min read
Less.js Merge
LESS is a Leaner Style Sheets, which is a simple CSS pre-processor that facilitates the creation of manageable, customizable, and reusable style sheets for websites.Less.js Merge is used to extend or aggregate multiple properties into a comma or space separated list under a single property. So merge
2 min read
Less.js String replace() Function
Less.js is a simple CSS pre-processor that facilitates the creation of manageable, customizable, and reusable style sheets for websites. It is preferable since CSS is a dynamic style sheet language. LESS can be utilized by many different browsers because it is versatile. Web browsers can only use CS
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 â&â (ampersand) operator. It is important in the pre-processor to have a clean code with the hierarchy of like DOM
3 min read
CSS | Shorthand Properties
Shorthand properties allow us to write multiple properties in a single line and in a compact way. They are useful as they provide clean code and also decrease the LOC (Line of Code). The Shorthand properties we will be covering: BackgroundFontBorderOutlineMarginPaddingList Background: The CSS Backgr
4 min read
Primer CSS Inline Styles
Primer CSS is a free open-source CSS framework that is built upon systems that create the foundation of the basic style elements such as spacing, typography, and color. This systematic method makes sure our patterns are steady and interoperable with every other. Its approach to CSS is influenced by
2 min read
CSS | @supports Rule
The CSS @supports rule specifies a support condition for feature support on a browser, i.e. a specific CSS style property can be specified as a condition to check for its support on the browser and if the condition is true then the block of code is executed else not. It is a CSS conditional rule and
3 min read