@each Loop Over Lists in Sass
📖 Scenario: You are creating a simple style for a website that highlights different sections with distinct background colors. You want to use Sass to loop over a list of colors and apply each color to a different section.
🎯 Goal: Build a Sass stylesheet that uses an @each loop to assign background colors from a list to multiple sections with classes .section-1, .section-2, and .section-3.
📋 What You'll Learn
Create a Sass list variable named
$colors with exactly these colors: red, green, blueCreate a variable
$section-count set to 3Use an
@each loop with variables $color and $index to loop over $colorsInside the loop, style the class
.section-#{$index} with the background color set to $color💡 Why This Matters
🌍 Real World
Using loops in Sass helps you write less repetitive CSS and easily manage styles for multiple elements with similar patterns.
💼 Career
Front-end developers often use Sass loops to create scalable and maintainable stylesheets for websites and applications.
Progress0 / 4 steps