Check out "Do you speak JavaScript?" - my latest video course on advanced JavaScript.
Language APIs, Popular Concepts, Design Patterns, Advanced Techniques In the Browser

SASS: interpolation in a name of variable

I just answered on that question at StackOverflow. That's actually asked a lot last few months.

The problem

In SASS we could make interpolation like that:

@mixin set-border($side) {
    border-#{$side}: solid 1px #000;
}
.header {
    @include set-border("top");
}

This is compiled to:

.header { border-top: solid 1px #000; }

The idea is to use a variable and construct another thing. However it is currently not possible to use interpolation in names of mixins or variables. For example, the following will not work.

$bg-dark: #000;
$bg-white: #FFF;
@mixin set-styles($arg) {
    background-color: $bg-#{$arg};
}
.header {
    @include set-styles("dark");
}

If you try to compile this you will get

Sass Error: Undefined variable: "$bg-".

The solution

However, there is a solution for that. You may use placeholders. The interpolation works there. So, instead of variables you define placeholders.

%bg-dark {
    background-color: #000;
}
%bg-white{
    background-color: #FFF;
}
@mixin set-styles($arg) {
    @extend %bg-#{$arg};
}
.header {
    @include set-styles("dark");
}

The compilation passes and the result is:

.header { background-color: #000; }
If you enjoy this post, share it on Twitter, Facebook or LinkedIn.