Sign InRegisterForgotten password?

RebelmouseTwitterFacebook

Tech Note: An Introduction to the Less CSS Compiler

Making CSS into a real language
LGF • Views: 29,299
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

^ 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-26 5:26 pm PDT

Recent Pages
Randall Gross
What is 0.00001 Percent?
From Mother Jones: 5) Fewer than 300 phone numbers were targeted in 2012.NSA officials say that even though the agency has access to Americans' phone records, it investigated fewer than 300 phone numbers connected to US citizens in 2012. The officials did not provide any detail on the number of email addresses targeted. much more

7 minutes ago
Views: 26 • Comments: 0
Tweets: 0 • Rating: 0
DO WINGNUT WORDS SHOW THEY EVOLVED BRAINS?11!!
Baker’s Graduation Cake Mix-Up Ends in Purr-fect Mistake
When Laura Gambrel, 22, of Zionsville, Ind., graduated from Indiana University in May, she wanted to keep the celebration pretty low key. She didn’t walk at the ceremony, nor did she have a party because she planned to go right back to the university this coming fall for grad school. It seemed only fitting then that the one thing her mother attempted to do ...

10 minutes ago
Views: 6 • Comments: 0
Tweets: 0 • Rating: 0
nines09
Liam Clancy - The Band Played Waltzing Matilda
Been a while since I was reminded of this one. Friend of mine posted it today. Let me share it with you. Liam Clancy

3 hours, 49 minutes ago
Views: 63 • Comments: 3
Tweets: 0 • Rating: 4
Heywood Jabloeme
Senate Intel Committee Blocks Former Staffer From Talking to Press About Oversight Process
The Senate Select Committee on Intelligence has taken the unusual step of actively blocking a former committee aide from talking to TPM about congressional oversight of the intelligence community. At issue isn't classified sources and methods of intelligence gathering but general information about how the committee functions -- and how it should function. The committee's refusal to allow former general counsel Vicki Divoll to ...

5 hours, 58 minutes ago
Views: 78 • Comments: 1
Tweets: 0 • Rating: 1
FemNaziBitch
Study: Reading Fiction Makes People Comfortable With Ambiguity
Are you uncomfortable with ambiguity? It's a common condition, but a highly problematic one. The compulsion to quell that unease can inspire snap judgments, rigid thinking, and bad decision-making. Fortunately, new research suggests a simple antidote for this affliction: Read more literary fiction. A trio of University of Toronto scholars, led by psychologist Maja Djikic, report that people who have just read a short ...

7 hours, 21 minutes ago
Views: 76 • Comments: 1
Tweets: 0 • Rating: 3
Dancing along the light of day
Bill Clinton: No alternative to two-state solution
‘No matter how many settlers you put out there, the Palestinians are having more babies than the Israelis,’ former US president says Former US president Bill Clinton on Monday night urged Israel to work toward a two-state solution with the Palestinians, saying that he had never heard a credible alternative that would enable Israel to remain a Jewish and a democratic state. “No matter ...

17 hours, 19 minutes ago
Views: 124 • Comments: 1
Tweets: 0 • Rating: 1
Skip Intro
Miss Utah USA Gives Worst Pageant Answer Since Miss South Carolina
Dye her hair blonde and she's got a job at Fox News. Question: "A recent report shows that in 40 percent of American families with children, women are the primary earners, yet they continue to earn less than men. What does this say about society?" Marissa Powell: "I think we can relate this back to education, and how we are ... continuing to try ...

1 day, 3 hours ago
Views: 388 • Comments: 6
Tweets: 0 • Rating: 0
Cap'n Magic
U.S. embassy security? Not in my experience
Pat Ferguson Hanson: I was working as a young diplomat at the U.S. Embassy in Athens, Greece, in 1975 when CIA Station Chief Richard Welch was killed in front of his wife upon their return home from a staff Christmas party. I know firsthand something about security at our posts overseas, and I consider myself lucky to be alive. Before he was murdered, Welch ...

1 day, 21 hours ago
Views: 191 • Comments: 0
Tweets: 0 • Rating: 0
Political Atheist
Trailer For PreView-A Short Thriller
In February of last year a writer friend gave me a challenge. "Write a story for a two minute video". I came back with an idea. So what if a self sure man bought a haunted camera? I'm beginning to submit to the first few film festivals. For that reason the film will not be online for some time. There will be a limited ...

1 day, 22 hours ago
Views: 96 • Comments: 0
Tweets: 1 • Rating: 0
Kragar
Virginia Candidate Jackson Has History of Money Problems
Jackson's money problems began back in 1985 when the Harvard-trained lawyer was living in Middlesex County, Mass., and was hit with a federal tax lien for failing to pay $3,000 since 1983, state records show. He paid off the IRS debt two years later, but his money troubles continued. In 1993, Jackson filed for Chapter 7 bankruptcy, according to federal court records. Among the ...

4 days, 22 hours ago
Views: 310 • Comments: 5
Tweets: 1 • Rating: 3
 Frank says:

The gorilla is on an island,eats bananas and has a good time all day long. He plays out there in the bushes. Some Americans find out about the gorilla and they hear how BIG he is - you know.They're very impressed with the size of the beast. So they catch the gorilla & they stick him in a boat & bring him back to the US. They show him off to everybody & make a bunch of money. ...Then they kill him ! -- The song King Kong.1968 tour Wisconsin.