Sass/SCSS Control Directives
Using conditionals and loops in Sass
If Statement
@mixin theme($isDark) {
@if $isDark { # if condition
background: black;
color: white;
} @else { # else clause
background: white;
color: black;
}
}
Each Loop
$colors: (red, blue, green);
@each $color in $colors { # loop through list
.text-#{$color} {
color: $color;
}
}
Each with Maps
$theme-colors: (
primary: #3498db,
success: #2ecc71
);
@each $name, $color in $theme-colors { # loop map
.btn-#{$name} {
background: $color;
}
}
For Loop
@for $i from 1 through 3 { # loop 1 to 3 (inclusive)
.col-#{$i} {
width: 100% / $i;
}
}
While Loop
$i: 1;
@while $i < 4 { # while condition true
.item-#{$i} {
width: 100px * $i;
}
$i: $i + 1;
}