Tech Note: An Introduction to the Less CSS Compiler

Making CSS into a real language
LGF • Views: 36,367
Source code via Shutterstock

Here’s where I geek out and lavish praise on a great software tool I wish I’d started using a long time ago: LESS « The Dynamic Stylesheet language.

In web development, one of the drawbacks of CSS is that it’s not really a language, but a static list of styles without variables or functions or math operations or any of the other dynamic features that you find in a computer language. I’ve often wished for these features when writing CSS code; for example, setting a variable to a certain color and then using the variable throughout the stylesheet would let you change the color everywhere it appears, simply by changing one variable declaration instead of tedious searching and replacing.

Well, that’s what Less does. It’s a language that extends CSS with a rich set of features, including variables, operations, functions, and “mixins” that allow you to generate entire blocks of code based on simple declarations. You write the code with the extended features of Less, then compile it into the CSS code that will be used on a site.

I can’t recommend it highly enough. I began converting our CSS to Less earlier this week, and it’s already become one of the standard LGF coding modules.

Here’s an example of a “mixin” I’m using to set the gradient fill backgrounds for several different LGF page elements, like the buttons and the top banner. To work in as many browsers as possible, it’s necessary to declare the linear gradient in four different styles, and also include a default background color for browsers that don’t handle gradient fills in CSS. (Variables begin with the ‘@’ symbol.)

.gradientbg (@bg, @start, @finish, @origin: top) {
 	background: @bg;
	background: -webkit-linear-gradient(@origin, @start, @finish);
	background: -moz-linear-gradient(@origin, @start, @finish);
 	background: -ms-linear-gradient(@origin, @start, @finish);
	background: -o-linear-gradient(@origin, @start, @finish);
}

Notice that the mixin declaration can include parameters, and parameters can have default values. This lets us now style an element with a background linear gradient fill with one simple line of code, instead of remembering (or looking up) that mess of different styles. Here’s the style for our top banner to illustrate:

#branding {
	position: relative;
 	border-bottom: 1px solid #B8B8B8;
	margin: 0 0 12px 0;
	width: 100%;
	height: 109px;
	.gradientbg(#E0E0E0, #F0F0F0, #D0D0D0);
}

The mixin is on the last line; it sets the default background solid color to #E0E0E0, and the start and finish colors for the gradient fill to #F0F0F0 and #D0D0D0. Notice that the mixin declaration actually has four parameters; @origin has a default value of “top,” so by leaving it out when we invoke the mixin the default value is used when the CSS code is generated.

When compiled, the above code looks like this:

#branding {
	position: relative;
	border-bottom: 1px solid #B8B8B8;
	margin: 0 0 12px 0;
	width: 100%;
 	height: 109px;
 	background: #E0E0E0;
	background: -webkit-linear-gradient(top, #F0F0F0, #D0D0D0);
	background: -moz-linear-gradient(top, #F0F0F0, #D0D0D0);
	background: -ms-linear-gradient(top, #F0F0F0, #D0D0D0);
	background: -o-linear-gradient(top, #F0F0F0, #D0D0D0);
}

This is just a quick introduction; there’s much more to Less. There’s also a version for PHP, which is actually the one I’m using in our production code: Lessphp - LESS Compiler in PHP.

To make things even nicer, when you compile your CSS code you can tell Less to “minimize” the generated CSS code by removing all unnecessary white space and comments. At LGF, this results in a file that’s about 25% smaller.

I use the Less compiler as part of a Linux shell script that compiles and combines several CSS files into one “master” file, then renames it to our production file to safely update the code … but that’s a topic for another post.

And that, dear readers, ends our geek session for the day.

Related

Jump to bottom

31 comments
1 Mostly sane, most of the time.  Sat, Jun 30, 2012 2:29:19pm

This is a computer programming post.

A geek post would point out that if you took all the 3's and made them 9, it would program your computer to endlessly broadcast "Vote for Saxon."

Or, perhaps, "Bad Wolf."

2 blueraven  Sat, Jun 30, 2012 2:32:12pm

Are you saying Less is more?

Totally do not understand anything in this post.

3 Dark_Falcon  Sat, Jun 30, 2012 2:32:59pm

re: #1 Mostly sane, most of the time.

This is a computer programming post.

A geek post would point out that if you took all the 3's and made them 9, it would program your computer to endlessly broadcast "Vote for Saxon."

Or, perhaps, "Bad Wolf."

Its still highly technical but it is also well written and informative.

4 b_sharp  Sat, Jun 30, 2012 2:38:29pm

re: #2 blueraven

Are you saying Less is more?

Totally do not understand anything in this post.

No, Less is not more, less is more with extensions.

5 b_sharp  Sat, Jun 30, 2012 2:40:23pm

Thanks Charles, the timing of this post couldn't have been better. I'm building a database for a client using MySQL and PHP on a Linux server, and it's been a few years since I've actually coded anything more than "Hello World" to show the grandkid.

6 b_sharp  Sat, Jun 30, 2012 2:41:48pm

re: #2 blueraven

Are you saying Less is more?

Totally do not understand anything in this post.

BTW, 'more' is a paginator in Linux, and 'less' (all lowercase) is also a paginator with a few more features than 'more'.

7 wrenchwench  Sat, Jun 30, 2012 2:42:29pm

re: #6 b_sharp

BTW, 'more' is a paginator in Linux, and 'less' (all lowercase) is also a paginator with a few more features than 'more'.

I'm sure you now have blueraven all straightened out.

8 b_sharp  Sat, Jun 30, 2012 2:43:44pm

re: #7 wrenchwench

I'm sure you now have blueraven all straightened out.

Hey Rusty, how did the dancing in the rain go?

9 wrenchwench  Sat, Jun 30, 2012 2:46:36pm

re: #8 b_sharp

Hey Rusty, how did the dancing in the rain go?

Not enough of it yesterday, but we had a good one today. It poured as though from a fire hose for about ten minutes. On the other hand, that's just what they're afraid of up where the big fire was. The terrain is so steep, and now denuded, that they're expecting major flooding. They've removed a couple of bridges rather than see them washed away.

10 Charles Johnson  Sat, Jun 30, 2012 2:48:37pm

Haven't used this code formatter in a while and had a little conflict between the formatter and the new code I installed to auto-link Twitter hashtags and usernames. Now straightened out - if you reload, the code now looks like it's supposed to.

11 erik_t  Sat, Jun 30, 2012 2:49:15pm

The temperature is too damned high!

60-odd mile bike ride was not an intelligent decision.

12 prairiefire  Sat, Jun 30, 2012 2:49:57pm

Also, men's Olympic gymnastic trails on tv at the mo mo.

13 dragonfire1981  Sat, Jun 30, 2012 2:54:52pm

OT, but a poster featuring this image just popped up on a neighbor's window.

14 b_sharp  Sat, Jun 30, 2012 2:57:41pm

re: #9 wrenchwench

Not enough of it yesterday, but we had a good one today. It poured as though from a fire hose for about ten minutes. On the other hand, that's just what they're afraid of up where the big fire was. The terrain is so steep, and now denuded, that they're expecting major flooding. They've removed a couple of bridges rather than see them washed away.

I feel sorry for the southern US, it seems you just can't get a break lately.

15 Dark_Falcon  Sat, Jun 30, 2012 3:01:44pm

re: #11 erik_t

The temperature is too damned high!

60-odd mile bike ride was not an intelligent decision.

You're lucky you didn't melt!

16 b_sharp  Sat, Jun 30, 2012 3:01:46pm

re: #13 dragonfire1981

OT, but a poster featuring this image just popped up on a neighbor's window.

Ho Hum.
If Wishes Were Feathers.

17 wrenchwench  Sat, Jun 30, 2012 3:07:40pm

re: #14 b_sharp

I feel sorry for the southern US, it seems you just can't get a break lately.

When it's not on fire, it's pretty nice at 6,000 feet. South of here, about 2,000 feet lower, it's hot. On the radio they've been saying the temps will be in 'the low to mid 100s'. I'm sure they mean around 100 to 105, but it sounds like they're expecting it to be about 150. [That's degrees Fahrenheit, of course.]

18 b_sharp  Sat, Jun 30, 2012 3:16:01pm

re: #17 wrenchwench

When it's not on fire, it's pretty nice at 6,000 feet. South of here, about 2,000 feet lower, it's hot. On the radio they've been saying the temps will be in 'the low to mid 100s'. I'm sure they mean around 100 to 105, but it sounds like they're expecting it to be about 150. [That's degrees Fahrenheit, of course.]

6000 ft? How do you breathe?
Sorry, where I live we're at 500m and the highest point in the prov is 1500m and it looks like a solitary mountain from a distance.

Our temps are pretty average for this time of year, around 25-30C (~77-86). I hear you guys are having records broken all over the place.

19 erik_t  Sat, Jun 30, 2012 3:17:21pm

re: #18 b_sharp

6000 ft? How do you breathe?

Air becomes Officially Thin at 10kft/3000m.

20 Cap'n Magic  Sat, Jun 30, 2012 3:19:14pm

I remember the days when you were part of CodeHead Software-glad to see that there's always something new to learn and improve one's toolbelt.

21 b_sharp  Sat, Jun 30, 2012 3:19:15pm

re: #19 erik_t

Air becomes Officially Thin at 10kft/3000m.

I was officially thin at 17. Now I'm officially fat.

22 allegro  Sat, Jun 30, 2012 3:21:00pm

re: #18 b_sharp

6000 ft? How do you breathe?
Sorry, where I live we're at 500m and the highest point in the prov is 1500m and it looks like a solitary mountain from a distance.

Our temps are pretty average for this time of year, around 25-30C (~77-86). I hear you guys are having records broken all over the place.

The last time I was in NM my brother and I were in the Jemez Mtns hiking to a hot springs. Well not hiking as much as dragging and gasping a lot going up that incline. A guy went trotting past, looked at us and said, "Sea level dwellers, huh," and laughed as he boogied on. It was really kinda humiliating.

23 Bear  Sat, Jun 30, 2012 3:21:06pm

re: #17 wrenchwench

What about 559.58 to 564.58 degrees Rankine? //

24 wrenchwench  Sat, Jun 30, 2012 3:21:54pm

re: #18 b_sharp

6000 ft? How do you breathe?

Rapidly.

Mr. w wants to go back to sea level. He smoked for 50 years, and though he quit over ten years ago, he still find this altitude difficult. Some day we'll be back by the ocean.

25 wrenchwench  Sat, Jun 30, 2012 3:23:30pm

re: #23 Bear

What about 559.58 to 564.58 degrees Rankine? //

You made me use that google thing, damn you!!

26 wrenchwench  Sat, Jun 30, 2012 3:24:10pm

The appropriate Page.

27 Bear  Sat, Jun 30, 2012 3:25:27pm

re: #26 wrenchwench


then try Kelvin. //

28 Bear  Sat, Jun 30, 2012 3:27:02pm

Had to look up those also. But in an old handbook.

29 Romantic Heretic  Sat, Jun 30, 2012 3:57:25pm

Ach. Code! Nooooo!

Sorry, I have unpleasant memories of my programming days. I was a heretic, an outspoken heretic. I preferred Niklaus Wirth's stuff and loathed C and Unix. Many programmers are priests in the religion of Ritchie.

So I was pretty much frozen out of the field. I'm still bitter about it.

30 nines09  Sat, Jun 30, 2012 6:17:07pm

Ok. I looked earlier. My oldest son does code. He talks, I glass over. I looked again. I glassed over. Thank you for the info, but I feel like I've just been handed a globe and it is 1204.

31 Tiny Alien Kitties are Watching You  Sat, Jun 30, 2012 6:22:46pm

So...uhh...I take it that this is a good thing for CSS and...uhh...declarations and stuff?


This article has been archived.
Comments are closed.

Jump to top

Create a PageThis is the LGF Pages posting bookmarklet. To use it, drag this button to your browser's bookmark bar, and title it 'LGF Pages' (or whatever you like). Then browse to a site you want to post, select some text on the page to use for a quote, click the bookmarklet, and the Pages posting window will appear with the title, text, and any embedded video or audio files already filled in, ready to go.
Or... you can just click this button to open the Pages posting window right away.
Last updated: 2023-04-04 11:11 am PDT
LGF User's Guide RSS Feeds

Help support Little Green Footballs!

Subscribe now for ad-free access!Register and sign in to a free LGF account before subscribing, and your ad-free access will be automatically enabled.

Donate with
PayPal
Cash.app
Recent PagesClick to refresh
Once Praised, the Settlement to Help Sickened BP Oil Spill Workers Leaves Most With Nearly Nothing When a deadly explosion destroyed BP’s Deepwater Horizon drilling rig in the Gulf of Mexico, 134 million gallons of crude erupted into the sea over the next three months — and tens of thousands of ordinary people were hired ...
Cheechako
Yesterday
Views: 72 • Comments: 0 • Rating: 0
Texas County at Center of Border Fight Is Overwhelmed by Migrant Deaths EAGLE PASS, Tex. - The undertaker lighted a cigarette and held it between his latex-gloved fingers as he stood over the bloated body bag lying in the bed of his battered pickup truck. The woman had been fished out ...
Cheechako
4 days ago
Views: 169 • Comments: 0 • Rating: 1