SCSS flow control and logic

Generate styles with @if, @else, and loops to write flexible, efficient frontend stylesheets.

🎯 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;
    }
}

This code works as follows:

  • If $theme is light, use a light background and dark text.
  • If $theme is dark, 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;
    }
}

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;
    }
}

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;
}

This generates .item-1 through .item-3, with increasing left margins.

Use @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);
    }
}

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;
    }
}

You now have multiple button color variants that are easier to maintain.

Generating styles programmatically simplifies maintenance and consistent visual changes, helping you work more efficiently.

❓ 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

  1. Use @for in the following template to generate .margin-top-1 through .margin-top-10, each with a different margin-top value.
@for $i from 1 through 10 {
    // Add your code here
}
  1. Use @each to iterate over a color map and generate .bg-color-name background 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!