@for loop (through vs to) in Sass
📖 Scenario: You are creating a simple color palette for a website. You want to generate CSS classes for background colors using Sass loops.
🎯 Goal: Build a Sass stylesheet that uses @for loops with both through and to keywords to generate CSS classes for background colors with numbered suffixes.
📋 What You'll Learn
Create a Sass list variable with exactly 5 color hex codes.
Create a variable
color-count set to the number 5.Use an
@for loop with through to generate CSS classes named .bg-color-1 through .bg-color-5 with background colors from the list.Use a second
@for loop with to to generate CSS classes named .text-color-1 through .text-color-4 with text colors from the list.💡 Why This Matters
🌍 Real World
Using Sass loops helps you write less repetitive CSS code when you have many similar styles, like color palettes or spacing scales.
💼 Career
Front-end developers often use Sass loops to maintain scalable and maintainable stylesheets, saving time and reducing errors.
Progress0 / 4 steps