In my previous post I ranted about the way the design community seems to violate the DRY principle. Let’s revisit the code (and Repeat the code, I know, I know):
1 2 3 |
h1 { font-size: 1.5em; /* 24px / 16px */ } |
What’s going on here? Well, to get to our beloved Em measurements, we apparently need a calculation based on our body’s font-size (24px) and the h1 target font-size (16px). This “would make future adjustments much, much easier”.
It’s not that I dislike this really, but more that I despise (having to do) this. We’re saying “1.5em” here, only we’re doing it twice.
Last week I’ve tried one of the solutions to this: SASS. And let me say, this feels like it could be love at first sight! With SASS, the above snippet will quickly transform into the following:
1 2 3 |
h1 { font-size: (24 / 16) * 1em; } |
Much better, no? We’ve now only stated once what the font-size should be: a certain fraction × 1em. I’m a little bit disappointed about needing the “* 1em
” there, but hey: it’s a great reason to ask another Stack Overflow question.
Anyways, SASS doesn’t stop here. It will add more improvements, one of particular importance to the above snippet. Consider this:
1 2 3 4 5 6 |
$target-font-size-body: 16px; $target-font-size-h1: 24px; h1 { font-size: $target-font-size-h1 / $target-font-size-body * 1em; } |
What’s up with the additional lines of code? Isn’t that extra code bloat? Well no, those lines help us achieve two very important goals:
- Our calculation is now much more meaningful, and will now truly “make future adjustments much, much easier”.
- We can reuse those variables in our style sheets. In the somewhat contrived example above it doesn’t really shine, but you can surely imagine this is a great benefit to the entire style sheet.
For my current pet project I’ve tried SASS in a feature branch, but I’ve already closed that branch: it was merged into the main branch after only a few hours. With this I’m indeed implying there’s a very friendly learning curve for SASS.
And yes: I’m also implying that you should try it for yourself! There’s many more nice features I haven’t even mentioned yet. And if I didn’t convince you, perhaps the two-page tutorial will!