# Using CSS Preprocessors With WordPress - LESS + CodeKit

In the first part of this series I gave a quick overview of the popular CSS preprocessors LESS and SASS. I also shared a few of the frameworks in which they are used.

I plan on taking a deeper dive into LESS and talking about the parts of the language I use most often. I also plan on showing you how to get started using it with CodeKit.

Let's dig in!

## What I Use and Why

I personally use LESS for most WordPress projects. I spend a lot of time working with Standard and it uses Bootstrap as its framework of choice.

Since Bootstrap uses LESS for compiling its CSS, I have learned how to use its .less files to make modifications. I know of other web designers and developers who prefer to use Foundation. Since Foundation uses SASS, they have learned to use that instead.

I am personally starting to work on another web project that uses SASS, and it has been relatively easy to pick things up due to my experience with LESS.

## A Look at LESS

### Variables

Variables are pretty self-explanatory. You can create a variable somewhere in your .less file and then reference it in other places in that file, or other files. I tend to use it for colors, fonts, padding, and margins.

Here's an example:

 1 2 // Variables  3 @red: #ff0000;  4 @green: #00ff00;  5 @blue: #0000ff;  6 7 // Styling  8 9 // Links  10 a { color: @blue; }  11 a:visited { color: @blue; } 

That will then compile to this:

 1 2 a { color: #0000ff; }  3 a:visited { color: #0000ff; } 

If you wanted to change the color of both link stylings, then you just need to change the variable:

 1 @blue 

from

 1 #0000ff 

to

 1 #0000a4 

You change in one spot, recompile, and you have changed all instances of that variable throughout the file.

Another great way to use these variables as well would be to use more semantic naming like:

 1 @primaryColor 

and

 1 @secondaryColor 

You can then use those variables throughout your code. When your designer comes up with those colors, you can just change them once, recompile, then you're done!

### Nesting

Nesting is used just like you would nest logic in an if/else block in your PHP or JavaScript: You set your higher level selector like .post, then place the other selectors inside of it. It will eliminate the need to repeatedly type out .post in front of your other selectors like so:

 1 2 // Post  3 .post {  4  padding: 20px;  5  .post-header {  6  margin: 20px 0;  7  }  8 } 

That will then compile to this:

 1 2 .post { padding: 20px; }  3 .post .post-header { margin: 20px 0; } 

You can also use & to concatenate selectors as well. An example could be if you wanted to target specific widgets in your sidebar. Let's say you wanted to style the background color of Recent Posts and Recent Comments widgets differently. You can do the following in LESS:

 1 2 .widget {  3  padding: 20px;  4  &.widget_recent_entries {  5  background-color: white;  6  }  7  &.widget_recent_comments {  8  background-color: black;  9  }  10 } 

That would compile into this:

 1 2 .widget { padding: 20px; }  3 .widget.widget_recent_entries { background-color: white; }  4 .widget.widget_recent_comments { background-color: black; } 

You can also use this for pseudo classes like :hover, :active, :visited, :before, and :after.

 1 2 // Links  3 a {  4  color: blue;  5  &:hover {  6  color: red;  7  }  8  &:visited {  9  color: red;  10  }  11 } 

That would compile to this:

 1 2 a { color: blue; }  3 a:hover { color: red; }  4 a:visited { color: red; } 

### Mixins

Mixins are basically a number of style attributes that you want to group together. A great example of this is for attributes that are different between browsers like border radius.

Instead of having to remember each one, you can call your mixin and it will provide each attribute for you. Here's an example:

 1 2 // Mixin  3 .border-radius {  4  border-radius: 4px;  5  -moz-border-radius: 4px;  6  -webkit-border-radius: 4px;  7 }  8 // Widget  9 .widget {  10  .border-radius;  11 } 

That will compile to the following:

 1 2 .widget {  3  border-radius: 4px;  4  -moz-border-radius: 4px;  5  -webkit-border-radius: 4px;  6 } 

What if you wanted to use border radius multiple times in your .less file but wanted different amounts for each? That's when you would use a Parametric Mixin.

This simply means that you can reuse a mixin and pass it an argument to use. Here's an example:

 1 2 // Mixin  3 .border-radius (@radius: 4px) {  4  border-radius: @radius;  5  -moz-border-radius: @radius;  6  -webkit-border-radius: @radius;  7 }  8 9 // Widget  10 .widget {  11  .border-radius();  12 }  13 14 // Post  15 .post {  16  .border-radius(8px);  17 } 

That would compile to this:

 1 2 .widget {  3  border-radius: 4px;  4  -moz-border-radius: 4px;  5  -webkit-border-radius: 4px;  6 }  7 .post {  8  border-radius: 8px;  9  -moz-border-radius: 8px;  10  -webkit-border-radius: 8px;  11 } 

### Putting It All Together

Here's an example of using variables, nesting, and mixins all at the same time:

 1 2 // Variables  3 @widget-recent-posts-border-radius: 4px;  4 @widget-recent-posts-background-color: white;  5 @widget-recent-posts-link-color: @blue;  6 7 @widget-recent-comments-border-radius: 8px;  8 @widget-recent-comments-background-color: black;  9 @widget-recent-comments-link-color: @red;  10 11 // Colors  12 @blue: #0000ff;  13 @red: #ff0000;  14 15 // Mixins  16 .border-radius (@radius: 4px) {  17  border-radius: @radius;  18  -moz-border-radius: @radius;  19  -webkit-border-radius: @radius;  20 }  21 22 // Widgets  23 .widget {  24  &.widget_recent_entries {  25  background-color: @widget-recent-posts-background-color;  26  .border-radius(@widget-recent-posts-border-radius);  27  ul li a {  28  color: @widget-recent-posts-link-color;  29  }  30  &.widget_recent_comments {  31  background-color: @widget-recent-comments-background-color;  32  .border-radius(@widget-recent-comments-border-radius);  33  ul li a {  34  color: @widget-recent-comments-link-color;  35  }  36  }  37 } 

Which would all compile into this:

 1 2 .widget.wiget_recent_entries {  3  background-color: white;  4  border-radius: 4px;  5  -moz-border-radius: 4px;  6  -webkit-border-radius: 4px;  7 }  8 .widget.widget_recent_entries ul li a { color: #0000ff; }  9 .widget.wiget_recent_comments {  10  background-color: black;  11  border-radius: 8px;  12  -moz-border-radius: 8px;  13  -webkit-border-radius: 8px;  14 }  15 .widget.widget_recent_entries ul li a { color: #ff0000; } 

## Using CodeKit

### Importing a Project

It's extremely easy to setup your CodeKit Project. You can simply drag and drop your folder into CodeKit, or you can click the plus button in the bottom left and then select a folder from the file browser.

Once you do that, CodeKit will automatically scan all the files in that folder and then group them into the following categories:

• Styles
• Scripts
• Pages
• Images

### Configuring the Project

So you have now imported your project files. Next we are going to set the output path of your .less files. I would suggest to have a css folder and a less folder inside of it. You will want to make sure to have all of your files in that less folder to point to your style.less file.

You will want to switch over to the Styles view. We will want to have the style.less file be compiled into your theme's root folder as style.css. To do this, you will want to right-click on the style.less file, and select "Set output path…". This will bring up a File Browser.

Next, you will want to click in the Output filename and extension text input. Once you do that it may auto populate style.css for you. If it doesn't, then you will want to enter in style.css. Lastly you will click on Select to save.

We are almost there! Next we will need to select a compilation setting. You will see the compilation settings screen reveal itself when you click on a .less file. We have three available output styles:

• Regular
• Minified
• Compressed (YUI)

Select one of the above and click on "Compile". You should get a notification that your style.less file has compiled.

If all of your syntax is correct, your compile will be successful. If there are any errors, it will flip you over to the Log view and give you the line number and explanation of the error.

If everything compiles correctly, you should be able to open up your style.css file and see the output. As you edit your .less files and save them, CodeKit will automatically recompile your files.

Note: Make sure that you don't make any changes to the style.css file, because as soon as you recompile your .less files, your changes will be overridden.

## Conclusion

We have now gone through a deeper dive into LESS and broken down a few of the popular features. I have explained a few reasons why I personally choose to use LESS in my WordPress projects and how to use CodeKit to compile all of my files.

In the next post, I will go into greater detail around how to structure your '.less' files and connect them all together.