Sass Sundays: Easy Theme Tiles Using Sass Maps & Lists
As I was browsing Kuler for colour inspiration the other day, I started wondering: what’s an easy way to automate colour theme thumbnail generation with Sass? It took me about half an hour to come up with a widget I was satisfied with and I promptly posted it on Sassmeister.
Take a look at the finished result (and then come back for the detailed writeup).
The HTML is pretty straightforward: a simple unordered list, because that seemed the semantic thing to do. I wanted the whole thing to be as simple as possible, so I just added two classes, theme
(which provides the basic styling) and theme--themename
, which specifies the theme shown.
And here is the Sass code used to generate the pretty thumbnails:
I first specify all the common styles in the theme
class. This is all pretty simple stuff, except maybe for that font-size: 0
hack which is a handy way to remove the space between inline-block elements. Normally, I would reset this in the li {}
rule, but in this case I wanted to hide the list items anyway, so I skipped that.
Then, I use the much anticipated SassScript maps (coming in Sass 3.3) to create a theme collection. Each entry consists of a key (the theme name) and a Sass list used as value (the theme colours). To make it all work, I use two nested loops: for each theme, I go through the colours and assign each of them as the background colour of the appropriate list item. Tada!
So how do I add a new theme? I just add another unordered list with a new theme--themename
class and I add the new entry to the $themes
map in Sass. Simple as that!
Possible optimizations
This won’t work in Internet Explorer < 8, because nth-child is not supported there. Still searching for the best way to make this backwards compatible, but I don’t expect I’ll lose much sleep over it.
If you need to read more on Sass lists and maps, take a look at Una’s excellent post about generating colour guides as well as these excellent posts by Hugo Giraudel on understanding Sass lists and advanced Sass list functions.