Performance: @each loop over lists
This affects CSS generation time and the final CSS file size, impacting page load speed and rendering.
@each $color in (red, blue, green) { .btn-#{$color} { background-color: $color; } } /* Use CSS variables or utility classes for other colors to reduce duplication */
@each $color in (red, blue, green, yellow, orange, purple, pink, brown, black, white) { .btn-#{$color} { background-color: $color; } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Large @each loop generating many classes | 0 (compile time only) | 0 | High due to large CSS | [X] Bad |
| Small @each loop with few classes | 0 (compile time only) | 0 | Low CSS size, faster paint | [OK] Good |
@each loop do in Sass when used with a list?@each@each loop is designed to run a block of code for every item in a list.@each loops = repeat styles [OK]@each repeats code for every list item [OK]@each modifies the list itself@each with list functions like map(red, green, blue) using @each in Sass?@each syntax@each $variable in (list) to loop over items.@each $color in (red, green, blue) which is correct. Options A, B, and D use wrong keywords or miss the $ sign.@each $var in (list) syntax [OK]@each $size in (small, medium, large) {
.btn-#{$size} {
font-size: if($size == small, 1rem, if($size == medium, 1.5rem, 2rem));
}
}if: small -> 1rem, medium -> 1.5rem, large -> 2rem.@each loop:@each $color (red, green, blue) {
.text-#{$color} {
color: $color;
}
}@each loop syntaxin between the variable and the list.in after $color, causing a syntax error.@each $var in (list) needs 'in' [OK](10px, 20px, 0, 30px), how can you use @each to create margin classes but skip the zero value?@each loop goes through each value including zero.@if to skip zero@if $m != 0 to only create classes for non-zero margins.@if inside @each to filter items [OK]Pass the test and this pattern moves unknown