Sass
Control Directives
Generate styles with @for, @each, and @if.
By EZ4Code Team
controlloopsconditionals
Code
$columns: 12;
// @for loops
@for $i from 1 through $columns {
.col-#{$i} { width: percentage($i / $columns); }
}
// @each over a list or map
$icons: user heart cart;
@each $name in $icons {
.icon-#{$name} { background-image: url("/img/#{$name}.png"); }
}
// @if / @else
@mixin size($value) {
@if $value == large { font-size: 20px; }
@else if $value == small { font-size: 12px; }
@else { font-size: 16px; }
}Explanation
Control directives make Sass programmable. @for generates numbered classes like a grid system, @each iterates lists or maps for repeated patterns, and @if/@else branches logic. These remove tedious repetition while keeping the output static and predictable.
More Sass Snippets
Variables
Store colors, spacing, and breakpoints in Sass variables.
Nesting
Mirror HTML structure and use the parent selector with &.
Mixins
Create reusable style groups with arguments and defaults.
Extend & Placeholders
Share styles across selectors with @extend and %placeholders.
Partials & @use
Split stylesheets into partials and load them with @use.
Functions
Define custom functions that return computed values.