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 Tutorial CSS stands for Cascading Style Sheets. It is a stylesheet language used to style and enhance website presentation. CSS is one of the three main components of a webpage, along with HTML and JavaScript.HTML adds Structure to a web page.JavaScript adds logic to it and CSS makes it visually appealing or
7 min read
CSS Introduction CSS (Cascading Style Sheets) is a language designed to simplify the process of making web pages presentable.It allows you to apply styles to HTML documents by prescribing colors, fonts, spacing, and positioning.The main advantages are the separation of content (in HTML) and styling (in CSS) and the
4 min read
CSS Syntax CSS is written as a rule set, which consists of a selector and a declaration block. The basic syntax of CSS is as follows:The selector is a targeted HTML element or elements to which we have to apply styling.The Declaration Block or " { } " is a block in which we write our CSS.HTML<html> <h
2 min read
CSS Selectors CSS Selectors are used to target HTML elements on your pages, allowing you to apply styles based on their ID, class, type attributes, and more. There are mainly 5 types of selectors.Basic CSS Selectors: These are used to target elements by tag, .class, or # ID for fundamental styling needs.Combinato
7 min read
CSS Comments CSS comments are used to add notes or explanations to your code, helping you and others understand it better. They start with /* and end with */ and can be used for both single-line and multi-line comments. Note: Comments are ignored by browsers, so they wonât affect how your webpage looks or works.
2 min read
CSS Colors CSS colors are used to set the color of different parts of a webpage, like text, background, and borders. This helps make the page look more attractive and easier to read. You can define colors using names, hex codes, RGB values, and more.You can try different formats of colors here- #content-iframe
5 min read
CSS Borders Borders in CSS are used to create a visible outline around an element. They can be customized in terms ofWidth: The thickness of the border.Style: The appearance of the border (solid, dashed, dotted, etc.).Color: The color of the border.You can try different types of borders here- #custom-iframe{ he
5 min read
CSS Margins CSS margins are used to create space around an element, separating it from neighboring elements and the edges of the webpage. They control the layout by adjusting the distance between elements, providing better organization and readability.Syntax:body { margin: value;}HTML<html> <head>
4 min read
CSS Height and Width Height and Width in CSS are used to set the height and width of boxes. Their values can be set using length, percentage, or auto.Width and HeightThe width and height properties in CSS are used to define the dimensions of an element. The values can be set in various units, such as pixels (px), centim
4 min read
CSS Outline CSS outline is a property used to draw a line around an element's border. It does not affect the layout, unlike borders. It's often used to highlight elements, providing a visual emphasis without altering the dimensions of the element.Syntaxselector{ outline: outline-width outline-type outline-color
4 min read