Try Tuts+ Premium, Get Cash Back!
Using Less CSS, BluePrint and WP for a Faster Workflow

Using Less CSS, BluePrint and WP for a Faster Workflow

Tutorial Details
  • Program: WordPress, Less.CSS, and BluePrint (Mac users only)
  • Estimated Time: 30 minutes
  • Difficulty: Intermediate

Less.CSS is becoming a popular tool for making stylesheets “dynamic”. I’ve been working with integrating Less.CSS with WordPress for a while now, so today we’re going to be looking at a short tutorial on how to get started with using it and the BluePrint CSS framework for a faster workflow in WordPress.


What is Less CSS?

I know what you’re thinking but, sorry folks, it doesn’t mean that you just write LESS code. How many times have you been working on a project and wished that you could simplify things by including a function in your CSS file, using some variables to change up colors or other properties, or other programming like techniques? With Less CSS, your wish can be turned into reality.

Less is a CSS tool that enables you to speed up and simplify your development by letting you use variables, inheritance ( via mixins ), functions ( operations ) and more. In a nutshell, Less CSS gives you the ability to mix programming techniques with your CSS. Enough talk though. Lets get started!

Less comes in two main flavors, a compiled version and a JavaScript powered version. For the purpose of our discussion we are going to be focusing on the compiled version. I’ve chosen to focus on this version primarily because I don’t like including files in my site that I don’t have to. After all, each file that your site needs to function and look attractive causes the site to load a little slower.

Less CSS compiler

So the way it works is as following: you visit the following link and download the app for mac. Sorry Windows users, it’s Mac only, so this version of the tutorial will only work for Mac users. Once you download the app you have to set up your site structure ( which I will cover below ).

We will be covering a great deal of content in this tutorial. I believe in working as efficiently as possible with the best tools available. For this reason, we will be employing WordPress and Blueprint to work with this project. Before moving to the next step go ahead and download the Blueprint Framework.

The only file we are going to be using is screen.css since it combines all the major files that make up the framework into one file. Go ahead and find that and pull it out to your desktop. For the purposes of brevity this tutorial assumes you are familiar with setting up WordPress as well as the basics of Blueprint. If you need more information on this, leave me some comments and I will reply or will write a post on it. There is also an informative tut on Nettuts about Blueprint

In order to set up your file structure refer to the screenshot below. This is what your directory structure should look like, minus the css files in the stylesheets folder. These will be auto-generated for you when you save a file.

directory_structure

The important things to take from this screenshot are that we have a styles.css folder in the theme directory in WordPress which does some trickery. This file just pulls in some other files from a subdirectory via the @import directive. The reason for this is that WordPress uses the comments in styles.css to identify a theme. Not putting that in tends to mess things up pretty badly. By including files in the styles.css file via a composite nature we are essentially taking advantage of all the dynamic features of Less CSS while making sure that our theme behaves properly in WordPress. We are minifying our CSS too, to speed up the load time of the site. In addition, it will strip out all comments and white space.

There are a few settings which I like to check. You probably want to do that as well. Below is a screenshot of these.

Less Css Preferences

Next, place the screen.css file in a subdirectory named stylesheets along with a main.css file. Then rename both of these files to have a .less extension instead of .css. Once you have done that, go ahead and fire up the Less app on your Mac and drag your theme folder into the App. This essentially sets it up so the Less app knows about your project. Now, each time you save your less files they get auto compiled into CSS. Pretty nifty huh? If for some reason this doesn’t happen, then you can click the compiler tab and click compile all.

Ok, so now that all of this is set up, what does Less really do for me? I’m glad you asked young Padawan. Let me show you!

So, say you have a few primary colors on your website. Instead of having to put the hex or RGBA values each time you want to use a color, you can simply use a variable to store your color and then use the variable name to identify the color you want to use. For instance, to store light gray you can declare a variable like so:

@lightGray = #ddd;

/* -- and then use it in your css like this: --*/

div#box1 { background: @lightGray; }

Now, whenever you need to change your color all you have to do is change the value of @lightGray and it will propagate it all out to the rest of your css file.

The next nifty trick I want to discuss is inheritance. For the CSS newbies out there, inheritance just means that one class can inherit or pick up traits from a previous class ( Parent class ). This is done by a Less construct known as Mixins. These virtually embed all of the properties of a class inside another class. One of the cool things about Mixins is that whatever property you specify in your child class will override the same behavior that is being inherited from the parent class. In plain English, this means that I have a class that makes up my buttons on my page using some handy css3 gradients. Although I like the padding, font size, and other attributes on the button, I may want my colors to change. I would do the following:

/*-- Button that I am using as the base / parent button --*/
.myBlueButton {
display: block;
padding: 4px 12px;

background: rgb(30,87,153); /* Old browsers */
background: -moz-linear-gradient(top, rgba(30,87,153,1) 0%, rgba(41,137,216,1) 50%, rgba(32,124,202,1) 51%, rgba(125,185,232,1) 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(30,87,153,1)), color-stop(50%,rgba(41,137,216,1)), color-stop(51%,rgba(32,124,202,1)), color-stop(100%,rgba(125,185,232,1))); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, rgba(30,87,153,1) 0%,rgba(41,137,216,1) 50%,rgba(32,124,202,1) 51%,rgba(125,185,232,1) 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, rgba(30,87,153,1) 0%,rgba(41,137,216,1) 50%,rgba(32,124,202,1) 51%,rgba(125,185,232,1) 100%); /* Opera11.10+ */
background: -ms-linear-gradient(top, rgba(30,87,153,1) 0%,rgba(41,137,216,1) 50%,rgba(32,124,202,1) 51%,rgba(125,185,232,1) 100%); /* IE10+ */
background: linear-gradient(top, rgba(30,87,153,1) 0%,rgba(41,137,216,1) 50%,rgba(32,124,202,1) 51%,rgba(125,185,232,1) 100%); /* W3C */

}

/* -- Button that Im using as the child / inheriting button --*/
.myGreenButton {
.myBlueButton;
background: rgb(157,213,58); /* Old browsers */
background: -moz-linear-gradient(top, rgba(157,213,58,1) 0%, rgba(161,213,79,1) 50%, rgba(128,194,23,1) 51%, rgba(124,188,10,1) 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(157,213,58,1)), color-stop(50%,rgba(161,213,79,1)), color-stop(51%,rgba(128,194,23,1)), color-stop(100%,rgba(124,188,10,1))); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, rgba(157,213,58,1) 0%,rgba(161,213,79,1) 50%,rgba(128,194,23,1) 51%,rgba(124,188,10,1) 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, rgba(157,213,58,1) 0%,rgba(161,213,79,1) 50%,rgba(128,194,23,1) 51%,rgba(124,188,10,1) 100%); /* Opera11.10+ */
background: -ms-linear-gradient(top, rgba(157,213,58,1) 0%,rgba(161,213,79,1) 50%,rgba(128,194,23,1) 51%,rgba(124,188,10,1) 100%); /* IE10+ */
background: linear-gradient(top, rgba(157,213,58,1) 0%,rgba(161,213,79,1) 50%,rgba(128,194,23,1) 51%,rgba(124,188,10,1) 100%); /* W3C */
}

There is one caveat to keep in mind. If you are using the css3 gradient generator, Less doesn’t seem to like the line that begins with filter. As you can see, the child button grabs all the behavior from the parent button by including the attributes of the blue button on the line marked.

myBlueButton;

.

It then overrides the color by specifying its own background colors. The next and last item we will be covering is using a parametric mixing ( a mixing that takes parameters ) with Less. This is useful in instances where you are using browser specific prefixes and such.

.borderRadius (@border_radius) {
-moz-border-radius: @border_radius;
-webkit-radius: @border_radius;
-border-radius: @border_radius;
}

Of course, there are many more behaviors and effects you can implement. You can follow the link at the top of the tut to the Less site and study to your hearts content. There is also a quick tip video on Nettuts that talks about the Less.js library.


Conclusion

That’s the basics of how to set up a project to use in WordPress with Less and BluePrint. I hope that what you learned here today will prove to be useful to you in future projects. If you have any more questions, please feel free to respond in the comments.

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • Pingback: Elegant D » Using Less CSS, BluePrint and WP for a Faster Workflow | Wptuts+

  • http://varemenos.com/ Varemenos

    They could at least make a linux version?

    • Robert

      You can always use the JavaScript Version. I sometimes do this during development, because the compiler doesn’t always @import right.

    • Rob

      There is SimpLESS ( http://www.wearekiss.com/simpless ) which is supposedly available on Linux as well as Win/Mac. Unfortunately the installer has been broken for some months.

      ..Looks like I’m going have to stop being so lazy and work out how to use a command-line based LESS compiler for Linux. Any hints anyone?

  • http://www.imageworksllc.com Joshua Briley

    LESS is great if you have complete buy-in from the entire team, or if you’re the only one managing CSS. The CSS produced from the less compiler is not so nice… difficult to manage. However, if you can keep the css in it’s “OOP’ format (by using JavaScript), then it’s a wonderful thing.

    • http://www.imperativedesign.net Paul
      Author

      Yeah I ran into the team issue with less a time or two. It’s benefits I think far outweigh the cons. Besides, if someone on your team doesn’t use it the learning curve for picking it up is extremely easy.

  • mr

    wondering if there is a way to compile somehow the less file on the ftp when updateing something? without compiling offline and uploading the css file to ftp

    • Paul
      Author

      You could use something like SVN. My past workflow was just to develop locally on XAMPP, then upload the site once the entire thing is complete. I’ll write a post when I get some free time on using SVN to work locally, push to a remote hosting solution, and use one database and one url for both installations soon. :)
      For now, coda and local dev would be my route to go.

      • http://www.imageworksllc.com Joshua Briley

        Paul,

        That would be fantastic*50. ;-)

  • http://www.designtodevelop.com Steven

    Yes I agree Less is an invaluable tool really helps speed up development. Again only down side is the compilation. I work on a PC and Mac and whenever I need to compile I have to jump on my Mac to do so. I would stay away from the JavaScript, in case users don’t have JavaScript and would reduce the loading time having to use JS to compile the CSS.

    Hopefully they can develop a tool which will compile Less on runtime before deployment. Rather than opting to use Blueprint I suggest using 960 grid. It has a much larger community and has a CSS generator so you can have multiple grids and sizes. Where blueprint is a little restricted I found, 960 grid is more flexible. If you need a start up framework I would suggest the HTML5 Boilerplate http://html5boilerplate.com/

    • KeMo

      Hey Steven,

      On Windows, just download the DotLess version with is a .NET implementation of Less. Then you just run it from the command line with the –watch flag which will automatically compile the Less to CSS whenever it detects that the .less file has changed. Give it a try:

      http://www.dotlesscss.org/

      use it like this:

      dotless.Compiler.exe style.less –watch

      • http://www.designtodevelop.com Steven

        Yeah I’ve seen this before, I think we used it on our .net application in the pass but that was for work. But personally I work with PHP so I need a complier that works for PHP ideally.

        But thanks

  • http://www.adesignlink.com Chad

    For those who are on windows… Is there a GUI compiler for us anywhere?

  • http://bontavlad.com Vlad

    What are the advantages of Less over Sass ? I’m using css for some time now and I’m hooked, I can’t go back to normal css.The thing is {Less} is more or less :)) like sass but sass is compiled locally with no app or fancy steps ( just “sass –watch input.sass:output.css” and it compiles to 3 outputs: indented, one line, or compressed ) its ultra fast on a good computer(coded in ruby) and has the awesome Compass framework as BFF(if you don’t know what compass is check it out, you’ll love yourself for doing that) (plus a lot of flexibility when thinking at the built in functions that i find the quite useful for responsive web page).
    Let me be clear I’m not flaming or trolling or say that Less is crap, I’m just testing the field and be happy to switch to switch to Less if it helps me more.

  • http://www.imperativedesign.net Paul
    Author

    Hello folks,
    So to be honest I’ve heard awesome things
    About SASS. I haven’t used it but should probably
    Look into it.

    In terms of CSS frameworks, I’ve been using the
    Less to generate a grid for me now so even less markup.
    I may write more on this in the future. Just been very busy
    As of late.

  • Pingback: A Free wordpress newsletter » WordPress News, Tutorials & Resources Roundup No.7

  • Pingback: WordPress News, Tutorials & Resources Roundup No.7 | Customize WordPress Blog

  • Divian

    Hi guys,

    nice post covered here.

    However some disadvantages for me, I want to point out…
    - The only downside I find is that Coda and most likely other editors don’t seem to support the .less extension as the .css extension (styling the code by colors I mean).
    - more, I like to code online on a test server instead of local and you can’t compile it online (making a .less into a .css while using the less app or something similar). The reason I point this out is because I like to use my css (or less i this case) together with cssEdit and .less files don’t load for that matter (so in this case the less.js is a no go here)…

    Don’t get me wrong, besides that, less can be a big time saver!

    • 14ner

      use less.js

  • Filipe

    I still did not start working with it, and I would like to know, what is the difference between less and sass?

  • http://eduplessis.com Edouard Duplessis

    You can use lessphp or more easy for WordPress… wp-less plugin.

    you do the less and it output a beautiful css

  • http://eduplessis.com Edouard Duplessis

    and if you want to use a grid system with Less …. use http://semantic.gs/ its magic

  • http://www.11-internet.nl Jan-Willem Bobbink

    The most disappointing is the fact that you can’t compile online. I would definetely integrate in some of my websites if they would make that possible.

  • http://www.phantomnimbus.com Beau Severson

    Just as a final note in case no one mentioned it, there is now an Air powered app called Crunch that brings the less.app niceness to Windows, Linux, and Mac. I love that it is cross platform and easy to use.

    Check it out here. http://crunchapp.net

    • Rob

      Adobe no longer support AIR on Linux, so sadly Crunch is no longer an option. :(

  • BALLU

    Is less compile time faster than css?

  • Connor Gaughan

    So correct me if I’m wrong here but this seems like a great way to do local development while using MAMP and then deploy the final site with the compiled CSS. To run this on a live server you would need to use it with less.js to compile everything via Javascript correct?

  • sana