Tech Note: An Introduction to the Less CSS Compiler

Making CSS into a real language
LGF • Views: 36,562
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 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
US Military M17 & M18 Pistols Causing Unintentional Discharges Documents detail U.S. soldiers shot by their own Sig Sauer guns; military says no reason for concern Around lunchtime on Feb. 8, 2023, inside an administrative office at Fort Eustis in Virginia, a sergeant with the Army’s 221st Military ...
William Lewis
1 hour ago
Views: 30 • Comments: 0 • Rating: 0
The Good Liars at Miami Trump Rally [VIDEO] Jason and Davram talk with Trump supporters about art, Mike Lindell, who is really president and more! SUPPORT US: herohero.co SEE THE GOOD LIARS LIVE!LOS ANGELES, CA squadup.com SUBSCRIBE TO OUR AUDIO PODCAST:Apple Podcasts: podcasts.apple.comSpotify: open.spotify.comJoin this channel to ...
teleskiguy
2 weeks ago
Views: 710 • Comments: 0 • Rating: 0