SASS/SCSS / Operators and Control Structures

Looping Through Variables with @each and @for

This tutorial will show you how to use the @each and @for directives in SASS/SCSS to create loops in your stylesheets.

Tutorial 3 of 5 5 resources in this section

Section overview

5 resources

Teaches the use of operators and control structures to add logic to stylesheets.

1. Introduction

In this tutorial, we will explore the power of SASS/SCSS by learning how to loop through variables using the @each and @for directives. These directives are helpful when you want to generate repetitive CSS with different values.

By the end of this tutorial, you will learn:

  • How to use the @each directive in SASS/SCSS
  • How to use the @for directive in SASS/SCSS
  • How to apply these directives in practical scenarios

Prerequisites:

  • Basic knowledge of CSS
  • Basic knowledge of SASS/SCSS

2. Step-by-Step Guide

Understanding @each and @for Directives

@each Directive:

The @each directive takes a list or map and generates styles for each item. It's useful for writing repetitive CSS where each repetition has a different value.

@for Directive:

The @for directive creates styles in a loop, just like a regular programming loop. It takes a start and end value and iterates over that range.

Best Practices and Tips:

  • Use meaningful names for your variables.
  • Keep your lists and maps simple and easy to understand.
  • Try to use these directives when you find repetitive patterns in your CSS.

3. Code Examples

Example 1: Using @each directive

$colors: red, blue, green;

@each $color in $colors {
  .text-#{$color} {
    color: $color;
  }
}

In this example, we have a list of color names. The @each directive loops over each color and generates a CSS class for each one. The output CSS will be:

.text-red {
  color: red;
}

.text-blue {
  color: blue;
}

.text-green {
  color: green;
}

Example 2: Using @for directive

@for $i from 1 through 3 {
  .col-#{$i} {
    width: $i * 33.333%;
  }
}

In this example, the @for directive is used to generate grid classes. The loop runs from 1 through 3 and generates a CSS class for each iteration. The output CSS will be:

.col-1 {
  width: 33.333%;
}

.col-2 {
  width: 66.666%;
}

.col-3 {
  width: 100%;
}

4. Summary

In this tutorial, we learned how to use the @each and @for directives in SASS/SCSS to create loops in your stylesheets. We also saw practical examples of how to use these directives.

Next steps for learning would be to dive deeper into SASS/SCSS features like functions, mixins, and conditionals.

Additional resources:

5. Practice Exercises

Exercise 1:

Generate a series of padding classes (p-1, p-2, ..., p-5) where each class increases the padding by 5px.

Solution:

@for $i from 1 through 5 {
  .p-#{$i} {
    padding: $i * 5px;
  }
}

In this solution, we loop from 1 through 5 and for each iteration, we generate a padding class that increases the padding by 5px.

Exercise 2:

Using the @each directive, create a series of background color classes given a list of colors.

Solution:

$colors: red, blue, green, yellow, orange;

@each $color in $colors {
  .bg-#{$color} {
    background-color: $color;
  }
}

In this solution, we loop through each color in the list and generate a background color class for each one.

Keep practicing these concepts to get more comfortable with SASS/SCSS. Happy coding!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

JSON Formatter & Validator

Beautify, minify, and validate JSON data.

Use tool

Text Diff Checker

Compare two pieces of text to find differences.

Use tool

QR Code Generator

Generate QR codes for URLs, text, or contact info.

Use tool

Word Counter

Count words, characters, sentences, and paragraphs in real-time.

Use tool

Interest/EMI Calculator

Calculate interest and EMI for loans and investments.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help