# 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:

That will then compile to this:

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

from

to

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:

and

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:

That will then compile to this:

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:

That would compile into this:

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

That would compile to this:

### 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:

That will compile to the following:

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:

That would compile to this:

### Putting It All Together

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

Which would all compile into this:

## 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.