LESS versus CSS | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (175).NET Core  (29).NET MAUI  (208)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (220)BoldSign  (14)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (66)Flutter  (133)JavaScript  (221)Microsoft  (119)PDF  (81)Python  (1)React  (101)Streamlit  (1)Succinctly series  (131)Syncfusion  (919)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (159)Xamarin  (161)XlsIO  (36)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (150)Chart  (132)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (633)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (40)Extensions  (22)File Manager  (7)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (508)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (11)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (387)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (597)What's new  (333)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
LESS versus CSS

LESS versus CSS

Introduction

Dealing with a large volume of plain CSS can be annoying when you face weird styling issues. When working with complex web applications, keep your CSS code as clean as possible. That’s why you need the LESS preprocessor to simplify your work and save time. However, you should also have CSS knowledge and an understanding of its basic architecture before you start to work with any of the preprocessors.

In this blog post, I identify the differences between LESS and CSS and the benefits of LESS over CSS.

What is a CSS Preprocessor?

A CSS preprocessor is a scripting language that generates a CSS syntax equivalent from another type of code, making it readable to web browsers. The styles applied through basic CSS will always be static throughout the application; whereas preprocessors allow you to write conditional code snippets to dynamically apply styles when required.

Also, the preprocessor allows you to maintain a standard code structure, presenting your stylesheets in a more readable way, so you can keep track of the styles of larger web applications.

CSS

If you are a new front-end developer, you must first know how to write basic CSS to better understand the workings of a preprocessor, as the preprocessor itself will compile and generate CSS in its final stage.

A beginner chooses CSS to customize the appearance of HTML elements because of its simplicity and basic style syntax. CSS allows you to easily control various parts of web pages—like headers, footers, content—in a way that’s easily understood.

You can either define static CSS styles inline by setting them as attributes of an element, or you can keep them in a separate CSS file and refer to them specifically when they need to be applied to HTML elements.

LESS

LESS, also known as Leaner Style Sheets, is a dynamic CSS preprocessor that compiles and generates CSS during runtime on the server or client side. LESS has advanced features—like variable substitutions, mixins, operations, combining functions—that help design a layout in a smarter way, supporting minimal yet flexible code usage.

You can even reuse the defined styles throughout the code. Moreover, it is broadly compatible with different browsers.

Why LESS over CSS?

There are many benefits of LESS that make it comparatively better than CSS. I’ll walk you through some of its important features: variables, mixins, operations, nesting, and functions.

Variables

Similar to how you define variables in other programming languages, you can set a variable in LESS and access it throughout your program. All variables should be prefixed with the @ symbol. They can store any type of value, such as selectors, property names, colors, dimensions, URLs, font names, etc.

Here, I’ve defined a variable as @bg-color and reused it on two HTML elements to set the background color. You can even define such variables in a common location and reuse them in your entire application wherever you want.

LESS Code

@bg-color:  #3FFD45;

div {
   background-color:  @bg-color;
}
p {
   background-color:  @bg-color;
}

Compiled Equivalent CSS Code

div {
  background-color: #3FFD45;
}
p {
  background-color: #3FFD45;
}

You can learn more about variable usage in LESS from the Variables section at LESSCSS.org.

Mixins

A mixin is like a variable, but the only difference is that it represents an entire class. You can group a set of properties into a specific class name and call them in necessary places to avoid repeated code definitions.

It also acts as a function and accepts arguments. This works by assigning those argument values to the group of properties defined within the rule set.

On the following code, I defined the mixins with a group of properties for ID and class selectors (parametric mixins) and made a call to those mixins from another rule set.

LESS Code

#divContent{
   color: #F3455A;
   background-color: #3FFD45;
}
.responsive-div(@height: 20px; @width: 40px) {
   height: @height;
   width: @width;
  #divContent;
}
.outer-div {
   .responsive-div(50px; 80px);
}
.inner-div {
   .responsive-div();
}

Compiled Equivalent CSS Code

#divContent {
  color: #F3455A;
  background-color: #3FFD45;
}
.outer-div {
  height: 50px;
  width: 80px;
  color: #F3455A;
  background-color: #3FFD45;
}
.inner-div {
  height: 20px;
  width: 40px;
  color: #F3455A;
  background-color: #3FFD45;
}

For more information about mixins in LESS, refer to the Mixins section from LESSCSS.org.

Operations

You can also perform basic arithmetic operations in LESS—such as addition, subtraction, division, and multiplication—on numeric values, colors, and variables.

In the following example, I initially define the static width and height variables, and then I assign the height and width values for the .inner-div element to half of their original value. I also set a different color by using an add operation.

LESS Code

@base-color: #3FFD45;
@width: 50px;
@height:50px; 

.inner-div {
   width: @width / 2;
   height: @height / 2;
   color: @base-color + #2EED54;
} 

Compiled Equivalent CSS Code

.inner-div {
  width: 25px;
  height: 25px;
  background-color: #6dff99;
}

Nesting

To make your code clearer when dealing with a larger volume of CSS, use the LESS nesting feature. You can define the stylesheet in a hierarchical structure by nesting a selector within another selector.

In the following example, I define two div elements with different height values but the same background color.

LESS Code

div{
  height: 500px;
  background-color: #3ffd45;
  .inner{
      height: 300px;
      color: #000000;
  }
} 

Compiled Equivalent CSS Code

div {
  height: 500px;
  background-color: #3ffd45;
}
div .inner {
  height: 300px;
  background-color: #3ffd45;
  color: #000000;
}

To know more about parent selectors in LESS, refer to the Parent Selectors section at LESSCSS.org.

Functions

There are also predefined functions available in LESS, allowing you to map JavaScript code to manipulate values, transform colors, and much more.

In the following example, I define a div element and customize it using math color operations and color definition function concepts.

LESS Code

@base-color: #8765EF;
@padding: 3.5px;

div{
color: darken(@base-color, 30%);
padding-left: round(@padding);
padding-right: round(@padding);
padding-top: ceil(@padding) * 2;
padding-bottom: ceil(@padding) * 2;
background-color: rgb(45, 100, 87);
}

Compiled Equivalent CSS Code

div {
  color: #3712a9;
  padding-left: 4px;
  padding-right: 4px;
  padding-top: 8px;
  padding-bottom: 8px;
  background-color: #2d6457;
}

By using these predefined functions, you can easily derive colors from a base color and maintain the same color family throughout the web page. Since it’s similar to JavaScript, this method makes defining logical functionality within the styling section easy.

To know more about functions in LESS, refer to the Functions section at LESSCSS.org.

Conclusion

There are many other preprocessors on the market, like SASS and Stylus; however, one should master the basics of CSS before using preprocessors. CSS and LESS are not completely different, but LESS does offer additional features that make your projects easier. More than that, the compiled version of LESS is actual CSS. Feel free to share your feedback as comments to this blog.

Syncfusion offers about 65 lightweight, modular, responsive components to make web application development as easy as possible. Check out our components for different platforms from the following list:

If you need assistance, feel free to contact us through our support forumfeedback portal, or Direct-Trac support system.

Tags:

Share this post:

Comments (3)

It really works?

Hi Manoj,

We are quiet unclear about your query. Please provide some detailed information like in which case you have faced an issue while using LESS instead of CSS. The provided information will be helpful for us to analyze about the requirement and update you the response as early as possible.

Please let us know if you would require any further assistance.

Regards,
Maithiliy K

It’s really work and easy to code

Comments are closed.

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed