330 Locust Dr.
Verona, WI 53593
Powderkeg Web Design
Message Us

When is LESS more?

January 02, 2015

Every website on the internet gets its “style” from files called “Style Sheets”. These “Style Sheets” describe how the elements on the HTML page should be displayed. Without these style sheets websites would be boring lists of black and white text.

The most common file type for a style sheet is Cascading Style Sheet (CSS). These CSS files use element types, IDs, and classes to identify elements and to apply “styles” to them. For example, if we have some text inside of a container element on our site we could edit that text like so:

Now all of our text in that container will be red. Simple enough. Now lets think about how many different elements there are on modern websites, and how deeply nested some of those elements can be inside of other elements. Also, often you need to target multiple elements nested deeply in the HTML code. Let me give a very typical example of how a section of a CSS file might look on a modern website.

This is obviously not from a real site but it demonstrates a very common scenario. For a long time it was difficult to keep CSS files readable and many developers experimented with indentation and other formatting tricks to help themselves and other developers read the CSS code.

In the last few years however a new trend has taken hold in the Web Development world known as LESS. Although LESS itself isn’t a style sheet it is compiled into a style sheet using Javascript. Now the same CSS above can be written in LESS like so:

To some this may seem just as ugly and complicated as the original but once you understand the hierarchy of LESS it really does make reading and working with the code much easier and really does keep the code cleaner. This becomes much more apparent when working with 1000+ line style sheets.

But wait, there is more! Because LESS is not a stylesheet in itself but is actually being compiled by another program that allows us to take advantage of variables and functions.

Most sites have a standard color palette to match their logo or brand. These colors are often repeated over and over again in a site’s style sheet. In CSS this means that every instance of this color needs to be hard coded.

Now in LESS we can just declare this color as a variable and reuse it throughout the code like so:

This alone makes LESS worthwhile but we also have the advantage of declaring functions inside of our code which can also be reused. Like so:

This type of functionality allows for a huge amount of reusability and standardization which makes the life of a developer so much easier.

We are really only scratching the surface here. There is so much to learn and do in LESS that I couldn’t possibly go into all of the options here.

For a more advanced list of LESS advantages check out this Smashing Magazine’s article.

Web Application Team Lead Developer

James is a versatile developer with a wide range of web related development experience. Although he has his degree in Computer Networking, his true passions are programming and database design. James has helped to develop sites of all shapes and sizes for both the government and private sector. He also enjoys mobile application development in his free time.