@each Loop Over Maps in Sass
📖 Scenario: You are designing a simple website theme. You want to create CSS classes for different button colors using Sass. Each button color has a name and a hex color code.
🎯 Goal: Build a Sass stylesheet that uses an @each loop to create CSS classes for buttons with background colors from a map.
📋 What You'll Learn
Create a Sass map called
$button-colors with three entries: primary with #3498db, secondary with #2ecc71, and danger with #e74c3c.Create a variable
$padding set to 1rem.Use an
@each loop to generate CSS classes named .btn-primary, .btn-secondary, and .btn-danger with the correct background colors and padding.Add a final style rule for
.btn that sets the text color to white and border radius to 0.25rem.💡 Why This Matters
🌍 Real World
Using Sass maps and loops helps you write less code and keep your styles consistent when designing websites with multiple color themes.
💼 Career
Front-end developers often use Sass to manage complex styles efficiently, especially when working on large projects with many design variations.
Progress0 / 4 steps