Mixins for semi-transparent colors
One of the things I love most about Sass is its ability to calculate colors based on other colors. I often use functions like darken(), saturate(), and adjust-color() to calculate highlight colors or shadows for things like buttons.
Recently I've enjoyed using semi-transparent colors in my designs to better blend elements with their backgrounds. Sass makes it easy to create semi-transparent colors with the rgba() function:
.button {
background: rgba(black, 0.5);
}.button {
background: rgba(black, 0.5);
}In CSS, rgba() takes four parameters. The first three are for red, green, and blue. The last is the alpha channel. Sass allows you to pass just two parameters. The first can be any color and the last, like the CSS version, is the alpha channel. At compile time Sass will translate the two parameter version into four.
But rgba() colors come with a price. Earlier versions of Internet Explorer can't interpret them correctly. When browsers have trouble interpreting an attribute/value pair they ignore it. This means that any element with an rgba() background will be rendered as completely transparent. You can get around this by including another color attribute with a format older browsers understand:
.button {
background: #7f7f7f;
background: rgba(black, 0.5);
}.button {
background: #7f7f7f;
background: rgba(black, 0.5);
}If you have a color picker tool, you can grab the values of the mixed colors yourself and add the additional attribute/value pairs manually, but this can be quite tedius. Why not use the the mix() function to mix the colors for you?
.button {
background: mix(black, white, 50%);
background: rgba(black, 0.5);
}.button {
background: mix(black, white, 50%);
background: rgba(black, 0.5);
}But we can do better than this! Invoking some deeper magic, we can write a mixin that extracts the alpha component of a color, convert it to a percentage and use mix() to convert an rgba() color and a background color into the appropriate attribute/value pairs:
@mixin alpha-background-color($color, $background) {
$percent: alpha($color) * 100%;
$opaque: opacify($color, 1);
$solid-color: mix($opaque, $background, $percent);
background-color: $solid-color;
background-color: $color;
}@mixin alpha-background-color($color, $background) {
$percent: alpha($color) * 100%;
$opaque: opacify($color, 1);
$solid-color: mix($opaque, $background, $percent);
background-color: $solid-color;
background-color: $color;
}Now we can write our code like this:
.button {
@include alpha-background-color(rgba(black, 0.5), white);
}.button {
@include alpha-background-color(rgba(black, 0.5), white);
}Which greatly simplifies things. With just a bit more effort, you can use a little Sass interpolation to make a generic mixin that you can use to set any color attribute:
@mixin alpha-attribute($attribute, $color, $background) {
$percent: alpha($color) * 100%;
$opaque: opacify($color, 1);
$solid-color: mix($opaque, $background, $percent);
#{$attribute}: $solid-color;
#{$attribute}: $color;
}@mixin alpha-attribute($attribute, $color, $background) {
$percent: alpha($color) * 100%;
$opaque: opacify($color, 1);
$solid-color: mix($opaque, $background, $percent);
#{$attribute}: $solid-color;
#{$attribute}: $color;
}And finally, our updated code:
.button {
@include alpha-attribute("background-color", rgba(black, 0.5), white);
}.button {
@include alpha-attribute("background-color", rgba(black, 0.5), white);
}Handy! To see this in action on a couple of my own buttons, check out this code pen.
Originally published on The Sass Way.
Comments
You might also like…
Sass vs. SCSS: which syntax is better?
Since the creation of Sass, it has been plagued by many levels of controversy. It billed itself as "a better CSS" and added brand new features unheard of to CSS authors such as variables, nesting and mixins. Sass also introduced an entirely different indentation-oriented syntax and a brand new perspective on how to author CSS. Then the SCSS syntax (Sassy CSS) was introduced ...
How to structure a Sass project
One of the coolest features of crafting CSS with Sass is that you can build out a file structure that puts all your components in their right place. BUT the question is ... where is the right place? Is there a standard way to structure your Sass files?
AI, design, and the modern frontend on the Changelog & Friends podcast
Back in January, I posted on LinkedIn about using AI to build a documentation site for Opine. This prompted Adam Stokoviak to reach out about joining him on the Changelog & Friends podcast to talk about AI and the modern frontend. Adam and I have been friends for a very long time now, having collaborated on The Sass Way together. He's a great host, and really put me at ease as we talked about how I'm using AI in my own workflow as a designer. On the show we discussed how I'm integrating AI into my workflow as a designer. We also talked about Framer and Figma, Next.js, and other topics.
Why I built my own comment system instead of reaching for Disqus
The build vs. buy equation has radically shifted. I built a full comment system for my blog in a few hours with AI — moderation, magic link auth, spam protection — and I own every piece of it.
