Chapter 15. Working with LessCSS
LessCSS (http://lesscss.org/) is a dynamic stylesheet language that allows you to code CSS more efficiently. Not only does it allow you to create variables with sensible names that you can re-use anywhere in your stylesheet, it also allows you to nest CSS styles, which is a huge timesaverâespecially working in Drupal, when you might find yourself styling several different selectors within one page or block of the site.
In LessCSS, youâll create your code in a file with the extension .less. Once youâve created your code, you compile it into a .css either using a Javascript call in the browser (thereâs even a Drupal module for itâdrupal.org/project/less), or use Less.app (available for Mac at incident57.com/less) to compile it and upload the .css file to your server. Generally, I go for the latter approach.
Creating Variables
Variables are little bits of code that you can call at will in your stylesheet. My favorite use for variables is in picking out colors. For example, letâs assume that your site uses a specific shade of brown (#572700) in a variety of places throughout the layout. In regular CSS, youâd have to input each instance manually, and youâll more than likely have the color written downâwith a bunch of other colors used in your layoutâon a pad somewhere near your desk.
Using LessCSS, youâd define the color once using @brown: #572700
; and then call the color
wherever it appears using color:
@brown;
or background-color: ...
Get Design and Prototyping for Drupal now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.