Using Loops in SCSS (for, each, while)
Using Loops in SCSS (for, each, while)

Introduction to SCSS Loops
SCSS loops are used to generate repetitive CSS code automatically, saving time and effort for developers. Instead of manually writing similar styles multiple times, loops allow you to create dynamic and scalable stylesheets. They improve code readability and reduce duplication in large projects. SCSS provides three main types of loops: @for, @each, and @while. These loops are widely used in real-world projects like grid systems, spacing utilities, and theme styling.
Example:
@for $i from 1 through 3 {
.box-#{$i} {
width: 100px * $i;
}
}
Using @for Loop in SCSS
The @for loop in SCSS is used when you know the exact number of iterations required. It runs from a starting value to an ending value and can include or exclude the final number using through or to. This loop is especially helpful for generating classes like margins, paddings, or column layouts. It simplifies repetitive numeric-based styling tasks. By using @for, developers can maintain consistency and reduce manual coding errors.
Example:
@for $i from 1 through 5 {
.m-#{$i} {
margin: 5px * $i;
}
}
Using @each Loop in SCSS
The @each loop is designed to iterate over lists or maps in SCSS, making it highly flexible. It is commonly used when working with predefined values such as colors, font sizes, or spacing sets. This loop helps in applying styles dynamically to multiple items without repeating code. It is particularly useful for creating theme variations or utility classes. Using @each improves code reusability and structure.
Example:
$colors: red, blue, green;
@each $color in $colors {
.text-#{$color} {
color: $color;
}
}
Using @while Loop in SCSS
The @while loop executes repeatedly as long as a given condition is true, making it useful for dynamic logic-based styling. Unlike @for, the number of iterations is not fixed beforehand. Developers must manually update the condition variable inside the loop to prevent infinite execution. This loop is helpful when conditions control the styling output. However, it should be used carefully to maintain code clarity and performance.
Example:
$i: 1;
@while $i <= 3 {
.p-#{$i} {
padding: 10px * $i;
}
$i: $i + 1;
}
Looping with Maps in SCSS
Maps in SCSS store data in key-value pairs, and they can be efficiently used with the @each loop. This combination allows developers to manage structured data like font sizes, colors, or themes in a clean way. It helps in organizing styles logically and improves scalability in large applications. Using maps reduces hardcoding and enhances flexibility. This method is widely used in design systems and UI frameworks.
Example:
$sizes: (
small: 12px,
medium: 16px,
large: 20px
);
@each $name, $size in $sizes {
.text-#{$name} {
font-size: $size;
}
}
Nested Loops in SCSS
Nested loops in SCSS allow one loop to run inside another, making it possible to create complex and structured styles. This is especially useful for grid systems, layouts, and multi-level designs. It helps generate multiple combinations of classes efficiently. However, overusing nested loops can make the code harder to read and maintain. Therefore, they should be used only when necessary.
Example:
@for $i from 1 through 2 {
@for $j from 1 through 2 {
.grid-#{$i}-#{$j} {
grid-row: $i;
grid-column: $j;
}
}
}
Best Practices for Using SCSS Loops
When using SCSS loops, it is important to keep the code simple and maintainable. Avoid creating overly complex or deeply nested loops that can reduce readability. Use variables and maps to organize your data effectively. Always test the generated CSS output to ensure it works as expected. Following best practices ensures that loops improve efficiency without compromising code quality.
Example:
$spacing: 10px;
@for $i from 1 through 4 {
.gap-#{$i} {
gap: $spacing * $i;
}
}

