SASS Mixin-使用变量填充类名

SASS Mixin-使用变量填充类名,sass,Sass,我把几种颜色设置为类 .black {background: $black;}; .red {background: $red;} .yellow {background: $yellow;} .grey {background: $grey;} .white {background: $white;} .blue {background: $blue;} .full-black {background: #000;} .light-black {background: #222;} 但是我想

我把几种颜色设置为类

.black {background: $black;};
.red {background: $red;}
.yellow {background: $yellow;}
.grey {background: $grey;}
.white {background: $white;}
.blue {background: $blue;}
.full-black {background: #000;}
.light-black {background: #222;}
但是我想创建一个mixin,它接受类名并自动创建类名并填充背景色,这样我就不必每次都键入这个了

我试过这样的东西

@mixin colours ($black,$full-black,$light-black,$red,$blue,$yellow,$white,$grey) {
    .(colours): background:{colours};
}

但无法找出正确的代码。

您应该使用关联数组定义颜色名称和代码:

$colors: ("emerald": "#2ecc71", "carrot": "#e67e22");

@each $name, $hexa in $colors {
    .#{$name} {
      color: #{$hexa};
    }
}

我制作了一个简单的代码笔,向您展示:

您是否也在用更少的语言寻找答案?请澄清。你不需要一个混音,但每个,看看我的回答;