Hi folks,
Welcome again! I hope you are doing well. I am thrilled to see you here. So Today, we will discuss the use of @for in Sass.
Introduction
Before going forward, let’s recap our previous blog Mastering CSS with Sass: Leveraging the Power of @for in Sass for Dynamic Styling: here and In this blog, we will look into the power of @for directive in dynamic styling.
Unveiling the Potential of Sass’s @for Directive:
@for
Directive, often overlooked, empowers efficient and versatile stylesheet creation. This directive adds styles in a loop, similar to a for
loop in JavaScript.
It is used in two ways: “start through end” or “start to end”.
The main difference is that the “start to end” excludes the end number as part of the count, and the “start through end” includes the end number as part of the count.
The Magic of @for:
Introducing iteration, the @for
directive generates repetitive code effortlessly. From complex grids to varied color schemes, @for
offers structure to handle repetitions.
Without @for Directive
<style type='text/scss'>
.text-1{
font-size:10;
}
.text-2{
font-size:20;
}
.text-3{
font-size:30;
}
</style>
<p class="text-1">Hello</p>
<p class="text-2">Hello</p>
<p class="text-3">Hello</p>
Output:

With @for Directive
<style type='text/scss'>
@for $i from 1 through 3 {
.text-#{$i}{
font-size:10*$i;
}
}
</style>
<p class="text-1">Hello</p>
<p class="text-2">Hello</p>
<p class="text-3">Hello</p>
The #{$i}
part is the syntax to combine a variable (i
) with text to make a string.
Output:

Elevating Styling Dynamics:
Effortless Grid Systems:
The @for directive simplifies the creation of effortless grid systems. It aids in generating columns with precise measurements for widths and gutters, ensuring adaptability across various screen sizes. This streamlines the process, resulting in grid systems that are both versatile and easy to implement.
Dynamic Color Schemes:
The @for directive proves invaluable for crafting dynamic color schemes. By iteratively adjusting color variations, it enables the systematic generation of harmonious palettes. This empowers designers to create visually consistent and appealing color schemes, adding a dynamic touch to their projects.
Streamlined Typography:
The @for directive streamlines typography management by facilitating the generation of font-size declarations, line heights, and spacing values in an organized manner. This approach enhances readability and visual hierarchy, contributing to a polished typographic rhythm throughout the design.
Conclusion:
Sass’s @for
directive unlocks dynamic styling dimensions. Embrace its capabilities to efficiently handle tasks, craft intricate design elements, and adapt styles to diverse contexts. As you explore the @for
loop’s potential, your web development arsenal expands, creating styles that captivate and adapt flawlessly.
Hey, let’s stay in touch!
If you liked this blog, please share it with your friends and colleagues. Connect with FE studio on LinkedIn to read more about such topics.