Naming Sass Color Variables
Variables in SASS are supposed to make our lives easier. Why write a value over and over again when you can name it and re-use it, right? Plus, variables give us the ability to change the value once and have it applied everywhere it’s referenced. That’s just awesome.
But variables can get pretty messy, especially for sites that use a lot of colors. I’ve seen different approaches for naming color variables and want to share the process I use to ensure they stay organized and maintainable.
Keep Colors in a Partial
I like to keep all my colors in their own separate partial file. Give it a name like
_colors.scss and keep it in a directory with other partials that make up the base of your app.
Then use this file to contain all of your colors:
I find that keeping my colors in one place apart from other variables makes it easier to find them later.
Use Memorable Names
This sounds obvious, but use names you will remember. The names should describe the color in a way that makes sense if you were talking about it normal conversation.
For example, this:
…makes so much more sense than this:
What happens if you have different shades of the same color? I say stick to giving them memorable names:
Isn’t that so much nicer than this?
You can use themes to make the colors even more memorable. I tend to name colors after foods because coding makes me hungry and that’s what’s on my mind. But you could use flowers, cartoon characters, gemstones, or anything else that makes sense to you.
Separate Types of Color
If the last example wasn’t a dead giveaway, I find it super helpful to group similar colors together. I like my colors like the food on my plate: not touching each other.
Assign Variables to Functions
The worst scenario with color variables is this. Let’s say
$blue is the primary color of your website, so you’ve used it all over your code.
Then, one day, you (or some wishy-washy designer) decide to change the primary color from blue to orange.
You can’t rename your
$blue variable, because then you have to change it everywhere else as well. You could just change the value, but then the variable name
$blue doesn’t make sense.
I’ve been able to avoid this situation by assigning colors to the function they serve, which looks something like this:
Now you can change your site’s color theme on a dime without sweating it.
Do these tips work every time? Of course not. I try not to be dogmatic in my approach to most things and that involves recognizing that not all sites are the same and that different approaches may need to be used for different situations.
However, I’ve found these tips for naming SASS color variables work for many of the sites I’ve not only built, but maintain as well.
Let me know if you’ve seen other approaches are have your own way of rolling with naming color variables.