Sign InRegisterForgotten password?

RebelmouseTwitterFacebook

Tech Note: An Introduction to the Less CSS Compiler

Making CSS into a real language
LGF • Views: 29,181
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:
Tech Note: Make Web Pages Load Faster by Compressing CSS

Advertisement
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.

^ back to top ^

Turn off ads by subscribing!
For about 33 cents a day, our subscription option turns off all advertisements at LGF!
Read more...

► LGF Headlines

  • Loading...

► Tweeted Articles

  • Loading...

► Tweeted Pages

  • Loading...

► Top 10 Comments

  • Loading...

► Bottom Comments

  • Loading...

► Recent Comments

  • Loading...

► Tools/Info

► Tag Cloud

► Contact

You must have Javascript enabled to use the contact form.
Your email:

Subject:

Message:


Messages may be published in our weblog, unless you request otherwise.
Tech Note:
Using the Contact Form
LGF Pages Create a Page

This 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 and text already filled in.

Last updated: 2013-05-21 7:32 pm PDT

Recent Pages
Vicious Babushka
If Disney Cartoons Were Historically Accurate
, if embed doesn't play. If Disney Cartoons Were Historically Accurate -- powered by Cracked.com

17 hours, 50 minutes ago
Views: 436 • Comments: 0
Tweets: 2 • Rating: 1
Political Atheist
There Is No Such Thing Really as Professional Photographers
there's no such thing as Flickr Pro, because today, with cameras as pervasive as they are, there is no such thing really as professional photographers, when there's everything is professional photographers. Certainly there is varying levels of skills, but we didn't want to have a Flickr Pro anymore, we wanted everyone to have professional quality photos, space, and sharing." - Marissa Mayer, Yahoo ...

20 hours, 51 minutes ago
Views: 250 • Comments: 3
Tweets: 1 • Rating: 1
chadu
Sinkhole at 14th and F Closes Downtown D.C. Streets
Oh joy! Just a couple blocks from my office... A sinkhole at the intersection of 14th and F streets in Northwest Washington has closed streets around that intersection, and police warn that the closures could remain in place for two days. Crews are on the scene to repair the sinkhole. As a result, 14th Street is blocked between New York and Pennsylvania Avenues NW ...

22 hours, 5 minutes ago
Views: 241 • Comments: 0
Tweets: 5 • Rating: 0
thecommodore
WAPO’s Glenn Kessler Gives Three “Pinocchios” To Claim of Doctored Emails
It has long been part of the Washington game for officials to discredit a news story by playing up errors in a relatively small part of it. Pfeiffer gives the impression that GOP operatives deliberately tried to "smear the president" with false, doctored e-mails. But the reporters involved have indicated they were told by their sources that these were summaries, taken from notes ...

22 hours, 24 minutes ago
Views: 271 • Comments: 3
Tweets: 0 • Rating: 2
FemNaziBitch
Men Are Being Sexually Assaulted in the Military, but Nobody Is Talking About It — the Good Men Project
Our Soldiers are being raped...by our Soldiers. In the U.S. Military, annually, more men are being raped by men than women are being raped by men. I state this fact, articulating it in this manner, not to diminish what these women are enduring but to raise the level of awareness and discourse to fully include the men; they have been dropping through the ...

22 hours, 40 minutes ago
Views: 193 • Comments: 2
Tweets: 0 • Rating: 2
Haywood Jabloeme
CSOPE Classroom Curriculum System Dies Amid Criticism
AUSTIN, Texas (AP) - A classroom curriculum system used by hundreds of Texas school districts will stop offering lesson plans after some conservative groups claimed it was promoting anti-American values. State Sen. Dan Patrick said Monday that officials who created the system known as CSOPE will vote to scrap thousands of lesson plans this week, effectively killing the entire system. CSCOPE had helped ...

1 day, 3 hours ago
Views: 114 • Comments: 0
Tweets: 0 • Rating: 1
kristina37
We know how to reduce the incidence of common medical conditions but …
Obesity is on the rise-- as is the incidence of Heart Disease, Diabetes, Stroke and various forms of Cancer. And, shockingly, diseases that had in the past begun in old age are now appearing at much earlier ages. A groundbreaking scientific study showed how easily (and inexpensively) the rate of occurrence of these and other serious illness could be greatly reduced-- but this information ...

1 day, 22 hours ago
Views: 201 • Comments: 2
Tweets: 0 • Rating: 2
theye1
The Greatest (Movie) Speech Ever Made
73 years later is still relevant and powerful as ever. It's also because of this movie that Charlie Chaplin was labelled a "premature anti-fascist."

4 days, 13 hours ago
Views: 466 • Comments: 9
Tweets: 0 • Rating: 7
Romantic Heretic
The Singing Ringing Tree
Some people built this strange looking statue in England. And this is what it sounds like. Youtube Video

4 days, 14 hours ago
Views: 166 • Comments: 2
Tweets: 0 • Rating: 4
Skip Intro
Your Weekly Peggy Noonan Three Martini Column
We are in the midst of the worst Washington scandal since Watergate. The reputation of the Obama White House has, among conservatives, gone from sketchy to sinister, and, among liberals, from unsatisfying to dangerous. No one likes what they're seeing. The Justice Department assault on the Associated Press and the ugly politicization of the Internal Revenue Service have left the administration's credibility deeply, ...

4 days, 18 hours ago
Views: 371 • Comments: 6
Tweets: 0 • Rating: 2
 Frank says:

It is always advisable to be a loser if you cannot become a winner.