CSS Preprocessors - Write LESS and put some SASS into your styling
As a website styler, it can get quite frustrating in this world of CSS, having to endlessly replicate code lines just to target another selector below the one you are styling, it can add up to literally thousands of lines of code, unnecessary code as well which you will eventually have to sift through just to find that line height that seemed like a good idea to set at the time…
Enter the CSS Preprocessor, this little tool will change your styling world forever. Just a precursor though, whilst I will be explaining LESS in this blog post, you can apply the same theory to almost any CSS Preprocessor.
I was forced to use LESS about a year or two ago when looking into using the Bootstrap framework, at first I hated it, it seemed so overly unnecessary, and it didn't seem to help me at all.
After getting frustrated and just dumping my normal CSS within the LESS files, I realised there must be a reason that such a juggernaut like Bootstrap uses such a tool like this. So I did some homework.
If you are looking for some good tutorials then the best place to start is usually the site where the code comes from. So I made my way to http://lesscss.org and started to read up.
It's really quite simple, each level you want to select can be nested and styled within itself.
Take this code snippet for example.
Quite cumbersome when you have to write out the same classes again and again just to target another selector… If this were done with LESS you can squeeze it into the following.
At first this might seem pointless, you aren't actually saving any lines of code, but it's the fact that you don't have to keep retyping (or copying and pasting) the same selectors again and again.
Also this way you can group your styling and have much cleaner and leaner code.
This is just the tip of the iceberg with LESS though, there is MUCH more that can be done with it. The point of LESS is to use LESS code.
Mixins as they are known in LESS language are snippets of code that you can re-cycle, so you don't have to keep writing out the same thing time and time again and they also mean you will never have to use that hacky method of search and replace EVER (seriously I mean it).
Take the example below:
This snippet is one that is used a lot when you are dealing with a business brand colour, for this example the brand colour is red. Now let's say you wanted to re-use this in an entire new site, but the new brand colour was blue… with CSS you would whip out the old search and replace and hope to god that:
- All the spacing was the same…
- The exact same hex code was used every single time…
- If there were any variations, again, the same hex code was used
- There was only 1 style sheet… otherwise you are in for trouble
These are just the things that spring to mind, and I can guarantee that every time you do this, there will be some stupid variation that a designer put in, like a drop shadow that is an alpha with some transparency or a tint colour or SOMETHING like that.
Again this is where LESS will save you literally HOURS of time. The mixin is set once and then is reused throughout the project and can be manipulated without ever having to change the original colour.
See the snippet above, used below with LESS
To make this even more advanced you can use:
So now when you change the mixin colour at the start of the code, it will flow through beautifully and change all the styling with one fell swoop (take that Search and replace!)
To touch on the above code as well, LESS also allows you to have the ability to apply mathematical functions to any style so you can have pixel point accuracy, most designers use percentage for opacity or colour changes (like a highlight or hover colour) and this way you can apply it in the same way, so when the client says “just a little lighter” rather than whipping out your hex colour palette, just change the percentage to match and it's all done for you.
There is A LOT more you can do with these processors, but I thought I would share some easy to explain features which have really changed the way I style.
A complex program needs memory
LESS is no exception, you will need your own development server, and you don't want to be coding in LESS on a live site. It uses resources which converting all the coding to CSS and it is not shy about gobbling them all up. But with that, it will make sure you use proper web development practices, develop on a dev server, save all your changes, and then put them live.
Learning LESS is hard at first
It can be difficult if like me, you can be styling for well over 10 years… I was set in my ways, but over the last 2 years, I have relearnt and been able to take advantage of LESS. It's not ALWAYS the right time to use LESS If you have a site that you don't really have access to obtain a dev copy of, or it is quite a simple design, sometimes a CSS prepocessor might not be the right option, so you need to pick and choose your battles. Have fun! Links to some CSS Preprocessors:
- May 2016 (1)
- June 2015 (7)
- May 2015 (3)
- April 2015 (2)
- March 2015 (6)
- February 2015 (3)
- January 2015 (2)
- December 2014 (3)
- November 2014 (1)
- October 2014 (8)
- September 2014 (8)
- August 2014 (8)
- July 2014 (14)
- June 2014 (12)
- May 2014 (9)
- September 2013 (2)
- April 2013 (2)
- March 2013 (5)
- November 2012 (2)
- June 2011 (1)
- February 2011 (1)
- August 2010 (1)
- July 2010 (1)
- June 2010 (7)
- May 2010 (1)
- March 2010 (5)
- February 2010 (1)
- November 2009 (4)
- July 2009 (1)
- July 2008 (11)
- October 2007 (2)