Amazon.com Widgets Less is More for your CSS

Less is More for your CSS

By Nick at October 24, 2012 19:34
Filed Under: Delphi, Software Development, Tech Stuff

As you may have noticed, I made a few changes to the website here at good ol’ nickhodges.com.  I added a column of boxes on the left, with the idea being to make stuff more accessible without so much scrolling.  And, well, yeah, okay, I’ve used that space to add a few more advertisements.

And while I was at it, I cleaned up a few things with the layout and the CSS.  It’s what I did with the CSS that I’d like to talk about today.

The main thing that I did was to start using the CSS pre-processor called Less.  Less allows you to do a lot of things “right” when writing CSS.  The first thing you notice when you start writing CSS is that it is a horrible violator of the DRY principle – Don’t Repeat Yourself – and Less solves a bunch of those problems.  It’s hard to write “clean CSS code”.  Less can help.  Less allows you to:

  • Create a *.less file that is then processed into your CSS file.  You can do this manually with a command line preprocessor, or you can do it on the client-side with Javascript.  Below, I’ll discuss how I pre-processed my *.less file automatically
  • Declare variables for colors and other things.  Say you have a color that you use in a number of different places.  You can declare

         @WidgetBorderColor: green;

    and then everywhere you had to type green, you could put @WidgetBorderColor instead.  Then, if you want to change from green to red, you only have to do it in one place.  That is really nice. 
  • You can also declare mixins – or chunks of CSS code that are commonly used.  For instance, if you have a bunch of places that use  

         {padding: 0px; margin: 0px}

    then you can declare

         .p0m0 {padding: 0px; margin: 0px}

    and use that as an “alias” in your *.less files

       body {
        .m0p0;
        font-family: Verdana;
        font-size: 13px;
        color: @Black;
        background: @White ;
       }

  • You can also use parameters to pass in to your mixins if you want, an other more complex constructs, though none of it is terribly difficult.   The Less page has all the documentation in one place.  The two features above were what I used in an effort not to repeat myself in my CSS.

Once you have your less file set up, you can process it via the command line, or using a GUI compiler tool.  Because Less is written in Javascript, you can use node.js to run it on your local machine.  To do that, you can download the Node Package Manager and then install  Less by simply executing

npm install less

npm is kind of interesting – it’s an online registry, so when you issue the above command, it goes out to a public registry and finds the Less package and simply installs it – there’s nothing for you to install. I guess it is much like Nuget in that regard. 

After that, you can then execute the lessc compiler. 

So the workflow at this point is:

    1. Write CSS and Less code in a *.less file
    2. Compile the *.less file into a *.css file.  (For instance, I use a batch file with:  c:\Code\node_modules\.bin\lessc C:\NickHodges\themes\nick\style.less c:\NickHodges\themes\nick\style.css
    3. Deploy the new CSS file with my site
    4. Repeat as necessary

Step 2 is kind of a bother, though.  You have to shell out to the command line to do the compile.  Who wants to do that if you don’t have to?

Well, I sure don’t.  So I found a cool tool called WinLess which does a nice thing – it automatically compiles your Less code into CSS every time you save your *.less file.

image

It works great – you just point it to your *.less files, and whenever you save changes, it automatically compiles the Less code down to CSS.  That way, your CSS is always up to date, and you just have to worry about your Less code.  Maintaining the CSS file is totally painless.  So now the workflow is:

  1. Update your *.less file as desired
  2. Save
  3. Deploy your *.css file.

That’s a lot easier, eh?

Now what I really want is a tool to find patterns in your CSS and automatically turn them into Less entries.  That would rock.

CSS is powerful and cool, and you can’t create a well-designed website without it.  But as a language, it’s not really very “best coding practices friendly”.  Less makes it much more so.  If you set things up correctly, you can do your styling in Less and forget about CSS altogether.

What do you guys do with your CSS?

Comments (8) -

10/24/2012 8:35:48 PM #

Wouter van Nifterick

What do you guys do with your CSS?

I usually use a PHP script to generate my CSS. That allows me to define variables, make loops, conditional branches, load stuff from a database, include external files, and leave out irrelevant parts if needed.

While developing, I link to style.php, which returns text/css. At the same time it updates a public style.css.

In the final version I link to the "compiled" style.css.
Advantages:
- Saves server resources (not having to generate the css)
- CSS can be cached in the browser, which saves bandwidth for the user and on the server

After I make a change to style.php, I can "compile" the css by requesting style.php once. Viewing the page that uses it is enough for that.

I've been doing it like that for years, and it's pretty solid.

Wouter van Nifterick Netherlands |

10/24/2012 9:07:06 PM #

Nick Hodges

Hey, that is pretty sweet.

Nick Hodges United States |

10/24/2012 9:40:06 PM #

Natalie Vincent

Hmmm... That got me thinking... Why not compile the CSS from your Less source with a PHP script? Then you don't have to do that "compile before you upload" step. The PHP script could cache a compiled version, and only re-compile when the Less file changes.

Here is a PHP Less compiler: http://leafo.net/lessphp/

Smile

N@

Natalie Vincent Australia |

10/24/2012 9:42:11 PM #

Colin Johnsun

Slightly off-topic, but I wonder what the technical and social (Embarcadero, 3rd party vendors and the rest of the Delphi dev community acceptance) hurdles to get a Delphi package manager in place similar to npm or Nuget. It would be great to be able to install Delphi components or libraries and be able to handle all of the necessary dependencies like these other two package managers do for their respective eco-systems.

I can see a lot of value in having such a system in place. All you need to do is look at Nuget's impact on the .net community and its uptake by their developers as well as from Microsoft itself. npm also had the same effect and impact on the nodejs community.

Colin Johnsun Australia |

10/24/2012 10:01:57 PM #

Nick Hodges

Colin --

You are right -- it would be a huge, huge feature.  Nuget has taken the VS crowd by *storm* -- seriously, I can't remember a development tool feature take off like that.

RAD Studio needs such a thing.

Nick

Nick Hodges United States |

10/25/2012 12:44:04 AM #

Colin Johnsun

I think the difficulty for its conception, is that it is something that developers would love to have but for the time to put the effort to building such a tool needs a lot of collaboration, standardisation and buy-in from various stakeholders. In most cases, it is hard to justify the time and effort to building such a tool that would be of little benefit for a single developer without other developers or organisations ensuring its adoption. Having organisations sponsoring its efforts is also important because it gives the package manager some credibility as well. eg. Microsoft for Nuget and Joyent for npm. For NuGet, it started as an internal project by Phil Haack when he worked at Microsoft but for the most part, Microsoft has their own team that is actively managing and contributing to the success and direction of NuGet. I hope Embarcadero is reading this as I think there is a lot to gain from such an initiative Smile

Colin Johnsun Australia |

10/26/2012 2:45:06 PM #

Ingo

I know some editots do support LESS syntax hilite, e.g. HippoEdit (AFAIR) and SynWrite (this is Delphi one).

Ingo Russia |

10/26/2012 4:49:26 PM #

Ryan Lerch

You should check out Sass seems like you might like it, its simalar to Less but seems more robust.

http://sass-lang.com/

Ryan Lerch United States |

Comments are closed

A Pithy Quote for You

"Good judgement is the result of experience ... Experience is the result of bad judgement."    –  Fred Brooks

Amazon Gift Cards

General Disclaimer

The views I express here are entirely my own and not necessarily those of any other rational person or organization.  However, I strongly recommend that you agree with pretty much everything I say because, well, I'm right.  Most of the time. Except when I'm not, in which case, you shouldn't agree with me.

Month List