Simple CSS color management with SASS
When I first got my CSS on (did I really just say that?) back in 2006, managing color values in my style sheet was pretty simple: I had my colors for text, and then my colors for elements with solid backgrounds, and that was about it. If I had my act together, maybe I’d write a comment at the top of my style sheet detailing the five or six hex values I was going to need, so I could refer to them as I coded.
And then CSS 3 happened. Gradients! Box shadows! rgba! Mother-effing hsl()! It was a wonderful time: Subtle color effects and variations that were previously the exclusive province of bitmapped background images were possible via straight up CSS. And as my designs in 2008 quickly filled up with drop shadows and gradients on everything humanly possible, I couldn’t help but notice that my style sheets were just as quickly filling up with all the color values I needed to support them. I’m sure this looks familiar:
background: rgb(157,213,58); 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%); 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))); 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%); 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%); 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%); 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%); |
The new vendor prefixes weren’t helping much either. As someone who develops in Firefox, I couldn’t tell you the number of times I’d change, say, a color in the -moz-linear-gradient selector, and forget to make the same update to the webkit gradient. D’oh. Hope you Chrome and Safari users liked hot pink and purple together.
Since I started using SASS, however, I’m happy to report that I’ve been able to return to the halcyon days of only needing to use a handful of hex values in my style sheet. In fact, when I start a new project, I usually only need four. Here’s my workflow:
First: Create SASS variables for your basic colors
There’s no one-size-fits-all solution here, and obviously every project is different. However, I find that most apps and sites utilize four main colors: A brand color, an action color, a function color, and an alert color. (For the purposes of this post, we’ll use my best guesses at Facebook’s colors as an example.)
$brand-color: #3b5998; $action-color: #67a54b; $function-color: #f2f2f2; $alert-color: #b22727; |
Second: Lighten your load. Or darken it.
Once you’ve defined your basic colors, let SASS’s amazing lighten() and darken() functions create the tints for each.
$brand-color-light: lighten( $brand-color, 10% ); $brand-color-lighter: lighten( $brand-color, 20% ); $brand-color-dark: darken( $brand-color, 10% ); $brand-color-darker: darken( $brand-color, 20% ); etc., etc. for all your colors... |

You’re going to want to tweak the percentages on a per-color basis here to get a nice gradation. (It should also be noted that SASS also has functions for dynamically adjusting a base color’s saturation, hue, and a whole bunch of other amazing stuff I feel like I haven’t scratched the surface of yet!)
Third: Set up your gradients, box shadows, etc.
Once you’ve got a whole bunch of nice tints for each of your base colors, setting up some mixins to use is pretty straightforward:
@mixin brand-gradient { background: url( imagefallback.png ) repeat-x $brand-color-darker; background: -moz-radial-gradient(center, ellipse cover, $brand-color 22%, $brand-color-darker 100%); background: -webkit-gradient(radial, center center, 0px, center center, 100%, color-stop(22%,$brand-color), color-stop(100%,$brand-color-darker)); background: -webkit-radial-gradient(center, ellipse cover, $brand-color 22%,$brand-color-darker 100%); background: -o-radial-gradient(center, ellipse cover, $brand-color 22%,$brand-color-darker 100%); background: -ms-radial-gradient(center, ellipse cover, $brand-color 22%,$brand-color-darker 100%); background: radial-gradient(center, ellipse cover, $brand-color 22%,$brand-color-darker 100%); } |
… then simply rinse and repeat for all the gradients you’re going to need across your site or app:

But what about rgba and hsl?
Personally, I’m old school and tend to stick with hex values because they’re short and I’m used to ‘em. And when I do need rgba transparency, for example, SASS makes it easy for me to stay right within my comfort zone, as follows:
background: rgba( $brand-color, 0.5 ); |
Pretty cool.
The beauty of this approach is that I can change every single tint of a color sitewide by simply tweaking a single hex value. And by quarantining all this CSS in a separate colors.scss file, I no longer have all those gross, vendor-prefixed declarations cluttering up my main style sheet. So thanks, SASS, for simplifying my job and letting me color like it’s 2006 all over again. P.S. For the next release, if you could build something in to get me back the hair I’ve lost since 2006, that would be awesome. kthxbai.

May 02, 2012 @ 18:47:17
Great read. What are your thoughts on Compass?
May 02, 2012 @ 19:08:21
Very similar to the approach I take.
I didn’t know about `background: rgba( $brand-color, 0.5 );` though! Awesome!
May 02, 2012 @ 19:29:38
Impressive! You may have just converted me……….
May 02, 2012 @ 20:23:52
This blog made me feel funny in my pants. I like it.
May 02, 2012 @ 21:03:34
To see it in action on a real world, open source project: https://github.com/fredwu/angel_nest/blob/master/app/assets/stylesheets/style.css.scss
Simple CSS color management with SASS | 12 Spokes » Web Design
May 02, 2012 @ 21:04:11
[...] Simple CSS color management with SASS | 12 Spokes [...]
May 02, 2012 @ 23:08:59
You should check out Compass next, which works with Sass and will let you write one or two lines for the gradients instead of six, and mean only one place to update the gradient colors. No more forgetting to change the colors in Opera!
May 02, 2012 @ 23:10:47
Named colors have been the #1 CSS requested feature since …. 1998. Still coming – later.
May 02, 2012 @ 23:52:35
Great. I’ve tried to tackle this a while ago with LESS, but somehow it gets difficult if one needs to ‘simulate’ colors coming from a design via photoshop, i.e. if the lighten/darken colours are specified by layering with transparent whites.
http://welearned.net/2011/11/less-is-more-more-or-less/
http://webrocker.de/dev/less/color/index.html
The idea was to have a set/rule which enables to simply “swap” out the base colour, but somehow the resulting tones are not quite as expected. I guess thats because a colour “seen” through a 60% transparent white is not the same as “lighten” the same colour (in the HSL model which is used internally by LESS) by 60%.
If all the transformations (spin,hue,lighten) are applied to recreate the lighter colour, then this set of rules will not result in the expected lighter colour if a different base is used. But if you don’t have to deal with a given set of colours, the colour functions of LESS/SASS are really great.
May 03, 2012 @ 02:07:17
I love this technique. Before SASS, this is how I would achieve a similar efficiency:
1) Create a layout with a solid color background
2) Set up each design element, such as boxes, buttons, and form fields, with a translucent black or white background, at some multiple of 10%
3) In CSS3, this can be achieved with rgba values. As a fallback for older browsers, I have twently transparent single pixels that become the repeated background image of these elements.
Now, when the solid background color changes, the whole design changes since the black and white shades darken or lighten the solid color. What’s more, this works as well in Photoshop as it does in the browser. It’s a great way to give your web app user the ability to choose his ‘theme’ by selecting a single color.
May 03, 2012 @ 03:14:34
I do exactly the same thing, although I use LESS instead of SASS.
I’d change one thing in your example. Instead of repeating all the rules in your gradients, I’d create a parameterised mixin called ‘gradient’. It would accept two parameters: ‘from’ and ‘to’. That mixin would declare gradients for all specific browsers using the from and to parameters. Then, I’d reuse it in each gradient.
May 03, 2012 @ 07:26:19
You could do even better by making your gradient @mixin take color arguments, and then say:
@mixin brand-gradient {
@include @gradient($brand-color, $brand-color-darker);
}
May 03, 2012 @ 07:31:57
Thanks! I’ve been using SASS for a while now but never got around to set this up properly.
May 03, 2012 @ 07:37:24
@Tom – That’s a really good point, and definitely something I’ve noticed myself. I guess that’s one of the nice things about coding up your own designs: you don’t need to answer to a designer with a Photoshop comp. If I were in that situation, though, I’d tweak the percentages on my tints to get them as close as possible to matching the tints on the comp.
May 03, 2012 @ 07:39:35
@Pawel -That would mean all your gradients would look exactly the same – just with different colors — although, that’s probably what you want 90% of the time anyway, so excellent suggestion!
May 03, 2012 @ 07:44:22
@Sam – I haven’t tried Compass yet. I probably should; it looks awesome. However, there’s a stubborn part of me that doesn’t want to lean on that heavy of a framework.
May 03, 2012 @ 07:56:52
What’s function color? Nice read…I’m just starting to use sass and this definitely convinced me that it’s worth it.
May 03, 2012 @ 08:04:24
@berto – The function color is (probably a pretty poor) way of describing a color I often see in UIs that prompts for a “secondary” action – examples would be a Close or Cancel button, or administrative functions like “Edit your account”, etc. The action color, on the other hand, is something that the app or site’s creator wants to the user to do – “Sign up now!”, etc.
May 03, 2012 @ 09:24:05
@Chris – thanks for that explanation. Clears it up. I’m trying to architect the front end of a small site, but I want to do it right. This post has been very helpful. I’m using twitterbootstrap though which uses LESS, but I think this is pretty much the same concept. Thanks!
May 04, 2012 @ 04:35:12
What is SASS? I assume it’s some sort of a JS plugin? You’ve failed to cover this minor detail?!
May 05, 2012 @ 06:52:46
@Darren, SASS is a language that compiles into CSS, have you heard of CoffeeScript? SASS is the CoffeeScript of CSS … same idea, better syntax, same end result.
http://www.sass-lang.com
Great post btw, Chris!
May 05, 2012 @ 08:33:42
Thanks, Wael!
May 20, 2012 @ 01:16:18
Chris,
Thanks for the post. It introduced me to SASS and I am switching to it.
Your mention of function and brand color raises a question in my mind. Is there a formal/semi-formal basis for designing a color scheme in web apps? Perhaps, a best practices guide or design patterns? Any pointers to this would be very helpful to me.
Thanks
May 25, 2012 @ 09:27:06
Great attempt at the gradient mixin. Too bad it won’t work in IE9, 8, or 7. You’ll still need the “filter” declaration for that.
And even then, IE9 doesn’t support rgba for gradients, you have to use an 8-hex setup. Does SASS have any fancy functions to handle that conversion?
May 25, 2012 @ 09:51:03
@Josh I wouldn’t say it doesn’t work. IE 9 and below gets either a fallback image or a solid color. I don’t mess around with the IE filter stuff. It doesn’t seem to work half the time, and it’s really ugly. My opinion: move on – IE 10 is only an auto-update around the corner!
@Phil – That’s an excellent question, but I don’t know of anything off-hand. My color variable names are only based on my own experiences and what I’ve seen on other sites/apps. At the end of the day, it’s really whatever you, as a designer, feel the app needs to differentiate the various things you need the user to do on your app or site.