LESS, The Flavor of CSS You Should be Writing for Drupal

It's no secret that we're obsessed with design. When we create a custom Drupal theme from our detailed designs, sometimes we end up with some pretty gnarly stylesheets. Since Drupal offers a generalized solution (typically a good thing), it creates unique, and often long, IDs and Classes for most HTML elements. When creating the theme, we are faced with a couple options:

  1. Create boatloads of templates to maintain consistent class and ID naming conventions. This makes maintaining templates more complicated.
  2. Create boatloads of CSS (often 2000 - 4000 lines) to bend the default markup to your will, our typical approach. This makes maintaining CSS more complicated.

Themes like Mothership are great for clean markup, but we wanted to improve the CSS side of things as well. We knew there had to be a way to write more concise and maintainable CSS while keeping templates maintainable in Drupal, which led us to the LESS CSS Preprocessor Module. LESS was originally created by the Ruby community and later ported to PHP by Leaf Corcoran. The LESS CSS Preprocessor Module for Drupal was added to the mix in March of 2010.

What is it?

LESS CSS Preprocessor Module is a compiler that generates css from a small superset language that adds many additional features seen in other languages. Once LESS is compiled, it's cached by the default CSS caching Drupal provides, so the performance hit is negligible.

What can it do?

LESS CSS lets you do some pretty interesting things, and I'll highlight my favorites.

1. Variables

The first thing that makes me stand up and shout is the ability to define variables in your CSS. Having variables is extremely helpful working in a team environment. Instead of having a bunch of weird hex codes that don't mean anything, you can define your colors as meaningful variables.

EXAMPLE:

At the top of your CSS document, you can define variables like this:

@branding-guide-green: #999900;

Now, we can reference @branding-guide-green wherever we like in our CSS.

h1 {
  color: @branding-guide-green;
}

2. Function like capability

Commonly we use the CSS property border-radius to round corners of html elemnts. Currently border-radius takes three lines of code to make it as cross browser compatible as possible (surprise, IE doesn't support border-radius)? With LESS CSS, we can do this with one line of code.

EXAMPLE:

Define your function at the top of the document. In this example our default radius is 5px.

@border-radius(@radius:5px) {
  -webkit-border-radius: @radius;
  -moz-border-radius: @radius;
  border-radius: @radius;
}

Standard CSS

#some-id {
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  border-radius: 5px;
}

LESS CSS

#some-id {
  @border-radius;
/* This will pull in all three lines of CSS with the default 5px radius */
}

LESS CSS (variation)

#some-id {
  @border-radius(10px);
/* If you want to use a different border-radius, 
pass it whatever pixel value you wish  */
}

3. Nesting

Typically in CSS you have to repeat yourself a lot. LESS can help you prevent this by allowing you to nest elements. You can definitely get carried away with nesting and make your CSS more difficult to read, so you'll need to find a happy medium.

EXAMPLE:

Standard CSS

#some-element {
  background-color: #000;
}
#some-element li {
  padding: 5px;
}
#some-element li a{
  color: #000;
}
#some-elemnt li a:hover {
  color: #CCC;
  text-decoration: none;
}

Same Example Using LESS (notice...much fewer selectors)

#some-elment {
  background-color: #000;
  li {
    padding: 5px;
    a{
      color: #000;
     :hover {
          color: #CCC;
          text-decoration: none;
      }
    }
  }
}

LESS may not solve all your CSS woes, but if used well, it can be a game changer. Check out the LESSPHP website and LESS CSS Preprocessor Module for more examples that will blow your mind. Seriously, try it out and you'll find out how much time you can save.

Filed under: 

5 Comments

Nice! I've been wanting to try out a CSS framework, but wasn't sure which one to try. Have you tried xCSS or any of the other CSS frameworks? What made you decide on LESS?

@q0rban LESS seemed like a clear choice because there was an existing Drupal module for 6.x. xCSS has a module for Drupal 7, but the syntax feels less (no pun intended) like css, and more like PHP. I think either one accomplishes the same end goal.

Someone should create a comparison of all the different CSS Preprocessors and link to it from the comparisons of contributed modules page.

Done. Well, started anyway.

I've updated your comparisons chart for SASS since the syntax does provide functionality for math and functions (although you're just passing in arguments to the function/mixins).

The good part of LESS is that is pro-evolutionary stylesheets instead of revolutionary ones. What do I mean? I mean that you don't need to modify your actual stylesheet, you can use it as you have it now, and you can start refactoring with LESS without learning a new syntax. it's really easy to remember the syntax of LESS features because it respect plain CSS syntax. In my opinion, LESS is how CSS should have been in first place.

Add a comment

About the Author

Joel Steidl

Senior Designer

Joel excels at making complicated design problems simple through good interface design. Whether designing or coding, Joel has a passion for doing things right.

Read More Joel's Blog Posts