Sass Flow Control
Sass @each
Photo Credit to CodeToFun
đ Introduction
The @each
directive in Sass is a powerful tool for iterating over lists and maps, allowing you to apply styles or perform operations based on each item.
It simplifies the process of generating repetitive CSS rules and helps in managing complex styling logic efficiently.
đĄ Syntax
The syntax for the @each
directive allows you to loop through a list or map and execute a block of code for each item.
Iterating Over a List
@each $item in $list {
// Styles or operations using $item
}
Iterating Over a Map
@each $key, $value in $map {
// Styles or operations using $key and $value
}
đĸ Parameters
- $list: The list of items you want to loop through.
- $map: The map of key-value pairs you want to loop through.
- $key: The key in the map (used when iterating over maps).
- $value: The value in the map (used when iterating over maps).
âŠī¸ Return Value
The @each
directive does not return a value. Instead, it executes a block of code for each item in the list or map.
đ Example Usage
Here are some examples demonstrating how to use the @each
directive in Sass.
đ Example 1: Iterating Over a List
$colors: red, green, blue;
.button {
@each $color in $colors {
&.#{$color} {
background-color: $color;
}
}
}
In this example, a class is created for each color in the $colors list, setting the background color accordingly.
đ Example 2: Iterating Over a Map
$sizes: (
small: 12px,
medium: 16px,
large: 20px
);
.text {
@each $key, $value in $sizes {
&-#{$key} {
font-size: $value;
}
}
}
Here, classes are generated for each size defined in the $sizes map, with the font size set according to the value.
đ Example 3: Dynamic Class Names
$themes: (
light: #fff,
dark: #333
);
@each $theme, $color in $themes {
.theme-#{$theme} {
background-color: $color;
color: $theme == light ? #000 : #fff;
}
}
This example creates theme classes with dynamic background and text colors based on the $themes map.
â ī¸ Common Pitfalls
Incorrect Use of @each with Non-Iterable Values:
Ensure that the value passed to
@each
is either a list or a map. Using other types of values will result in an error.The following is Incorrect:
example.scssCopied@each $item in 'string' { // This will not work }
The following is Correct:
example.scssCopied$list: red, green, blue; @each $item in $list { // This works }
Overwriting Variables Inside the Loop:
Be cautious when using the same variable names inside nested
@each
loops or other loops. It might lead to unexpected results or errors.example.scssCopied$colors: red, green, blue; @each $color in $colors { $color: lighten($color, 20%); // $color is overwritten here }
Solution:
example.scssCopied$colors: red, green, blue; @each $color in $colors { $light-color: lighten($color, 20%); // Use $light-color instead of overwriting $color }
Forgetting to Include & for Nested Selectors:
When using
@each
within nested selectors, remember to include & to ensure proper nesting of the generated styles.The following is Incorrect:
example.scssCopied.container { @each $size in $sizes { .size-#{$size} { width: $size; } } }
The following is Correct:
example.scssCopied.container { @each $size in $sizes { &.size-#{$size} { width: $size; } } }
đ Conclusion
The @each
directive in Sass provides a powerful way to handle repetitive styling tasks efficiently. By allowing you to iterate over lists and maps, @each
simplifies the process of generating CSS rules and enhances the maintainability of your code. Understanding and applying this directive effectively can greatly streamline your styling workflow.
Experiment with @each
in different scenarios to leverage its full potential and make your Sass code more dynamic and manageable.
đ¨âđģ Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (Sass @each), please comment here. I will help you immediately.