Last Updated : 09 Jan, 2025
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:A browser cannot directly process LESS code. A LESS preprocessor compiles the LESS file into standard CSS that browsers can interpret.
Working Steps: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.
@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 LESSRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4