Reputation: 736
The following is my current code which occurs compile error.
$color-1: #353a4b;
$color-2: #5563a9;
$color-3: #7184e7;
$color-4: #c788e6;
$color-5: #f76789;
@for $i from 1 through 5 {
.bg-color-#{$i} {
background-color: $color-#{$i};
}
.fg-color-#{$i} {
color: $color-#{$i};
}
}
This gives me the following error
SassError: Undefined variable: "$color-".
I would like to define dynamic styles using sass @for.
.bg-color-1 {
background-color: $color-1;
}
.bg-color-2 {
background-color: $color-2;
}
...
Could anyone can help me, please? Thanks in advance
Upvotes: 0
Views: 1062
Reputation: 2105
One proper solution would be to map
the variables
and then use an @each
loop to access them. Codepen.
$colors: (
color-1: #353a4b,
color-2: #5563a9,
color-3: #7184e7,
color-4: #c788e6,
color-5: #f76789,
);
@each $color in $colors {
$index: index($colors, $color);
$color: nth($color, 2);
.bg-color-#{$index} {
background-color: $color;
}
.fg-color-#{$index} {
color: $color;
}
}
Upvotes: 2