SCSS flow control and logic
🎯 Introduction
SCSS flow control lets you generate styles programmatically for more flexible, efficient, and structured stylesheets. This article covers conditionals and loops, with practical examples such as generating a grid system and color variants, so you can use SCSS logic to generate styles.
🌀 Conditionals in SCSS: @if and @else
In SCSS, @if and @else provide conditional logic similar to if-else statements in other programming languages. They let you output different styles depending on a condition.
$theme: dark;
body {
@if $theme == light {
background-color: #fff;
color: #000;
} @else if $theme == dark {
background-color: #000;
color: #fff;
} @else {
background-color: gray;
color: black;
}
}
body {
background-color: #000;
color: #fff;
}
This code works as follows:
- If
$themeislight, use a light background and dark text. - If
$themeisdark, use a dark background and light text. - Otherwise, use a gray background and black text.
@if/@else lets you generate different styles based on variables, which is useful for themes and responsive design.🔄 Loops in SCSS: @for, @each, and @while
Loops automate style generation. SCSS supports three loop types for different situations.
1. The @for loop
@for iterates over a numeric range to generate styles.
@for $i from 1 through 3 {
.col-#{$i} {
flex-grow: 1;
}
}
.col-1 {
flex-grow: 1;
}
.col-2 {
flex-grow: 1;
}
.col-3 {
flex-grow: 1;
}
This generates three classes, from .col-1 through .col-3.
2. The @each loop
@each iterates over a list or map (key-value pairs), making it useful for color variants and collections of styles.
$colors: (
primary: #007bff,
success: #28a745,
danger: #dc3545,
);
@each $name, $color in $colors {
.text-#{$name} {
color: $color;
}
}
.text-primary {
color: #007bff;
}
.text-success {
color: #28a745;
}
.text-danger {
color: #dc3545;
}
This generates .text-primary, .text-success, and .text-danger, each with its corresponding color.
3. The @while loop
@while repeats while a condition is true. It is useful for more complex loop logic, though less commonly needed.
$i: 1;
@while $i <= 3 {
.item-#{$i} {
margin-left: 10px * $i;
}
$i: $i + 1;
}
.item-1 {
margin-left: 10px;
}
.item-2 {
margin-left: 20px;
}
.item-3 {
margin-left: 30px;
}
This generates .item-1 through .item-3, with increasing left margins.
@for and @each for most style generation tasks. @while is useful when you need more flexible control.🌈 Practice: generating a grid system and color variants
Generating a grid system
Grid systems are a common foundation for frontend layouts. Many frameworks provide a 12-column grid, for example. SCSS loops let you generate the corresponding classes yourself.
$columns: 12;
.container {
max-width: 1200px;
margin: 0 auto;
}
.row {
display: flex;
flex-wrap: wrap;
}
@for $i from 1 through $columns {
.col-#{$i} {
flex: 0 0 calc($i / $columns);
max-width: calc($i / $columns);
}
}
.container {
max-width: 1200px;
margin: 0 auto;
}
.row {
display: flex;
flex-wrap: wrap;
}
.col-1 {
flex: 0 0 0.0833333333;
max-width: 0.0833333333;
}
.col-2 {
flex: 0 0 0.1666666667;
max-width: 0.1666666667;
}
.col-3 {
flex: 0 0 0.25;
max-width: 0.25;
}
.col-4 {
flex: 0 0 0.3333333333;
max-width: 0.3333333333;
}
.col-5 {
flex: 0 0 0.4166666667;
max-width: 0.4166666667;
}
.col-6 {
flex: 0 0 0.5;
max-width: 0.5;
}
.col-7 {
flex: 0 0 0.5833333333;
max-width: 0.5833333333;
}
.col-8 {
flex: 0 0 0.6666666667;
max-width: 0.6666666667;
}
.col-9 {
flex: 0 0 0.75;
max-width: 0.75;
}
.col-10 {
flex: 0 0 0.8333333333;
max-width: 0.8333333333;
}
.col-11 {
flex: 0 0 0.9166666667;
max-width: 0.9166666667;
}
.col-12 {
flex: 0 0 1;
max-width: 1;
}
This generates .col-1 through .col-12, with each class assigning a width and maximum width based on its grid proportion.
Generating color variants
Projects often need multiple color variants, such as different button colors. Use @each to generate the corresponding styles.
$button-colors: (
primary: #007bff,
secondary: #6c757d,
success: #28a745,
danger: #dc3545,
warning: #ffc107,
);
@each $name, $color in $button-colors {
.btn-#{$name} {
background-color: $color;
color: white;
padding: 0.5em 1em;
border-radius: 4px;
}
}
.btn-primary {
background-color: #007bff;
color: white;
padding: 0.5em 1em;
border-radius: 4px;
}
.btn-secondary {
background-color: #6c757d;
color: white;
padding: 0.5em 1em;
border-radius: 4px;
}
.btn-success {
background-color: #28a745;
color: white;
padding: 0.5em 1em;
border-radius: 4px;
}
.btn-danger {
background-color: #dc3545;
color: white;
padding: 0.5em 1em;
border-radius: 4px;
}
.btn-warning {
background-color: #ffc107;
color: white;
padding: 0.5em 1em;
border-radius: 4px;
}
You now have multiple button color variants that are easier to maintain.
❓ Questions and answers
Q1: What is the difference between @each and @for in SCSS?
@for iterates over numeric ranges and suits sequential class names. @each iterates over lists or maps and suits generating styles from their values or key-value pairs.
Q2: Why generate a grid system with SCSS loops?
Writing many width classes manually is repetitive and error-prone. Loops keep the source concise and consistent.
🧪 Exercises
- Use
@forin the following template to generate.margin-top-1through.margin-top-10, each with a differentmargin-topvalue.
@for $i from 1 through 10 {
// Add your code here
}
- Use
@eachto iterate over a color map and generate.bg-color-namebackground classes. Make each color lighter on:hover.
$bg-colors: (
info: #17a2b8,
light: #f8f9fa,
dark: #343a40,
);
// Add your code here
🎉 You now understand SCSS conditionals, loops, and practical style generation! Use them in your projects to create efficient, flexible stylesheets!
