Create a Color Shade Generator with @while Loop in Sass
📖 Scenario: You want to create a set of color shades for a website theme. Instead of writing each shade manually, you will use Sass's @while loop to generate these shades automatically.
🎯 Goal: Build a Sass stylesheet that uses an @while loop to create five different shades of a base color. Each shade will be a darker version of the base color, and each shade will be assigned to a CSS class named .shade-1 through .shade-5.
📋 What You'll Learn
Create a variable
$base-color with the value #3498db (a blue color).Create a variable
$shade-count set to 5 to represent how many shades to generate.Use an
@while loop with a counter variable $i starting at 1.Inside the loop, generate a CSS class named
.shade-#{$i}.Each class should set the
background-color to a darker shade of $base-color by 10% * $i using the darken() function.Increment the counter
$i inside the loop until it exceeds $shade-count.💡 Why This Matters
🌍 Real World
Web designers often need to create color palettes with multiple shades for buttons, backgrounds, and highlights. Using loops in Sass saves time and reduces errors.
💼 Career
Knowing how to use loops and variables in Sass is a valuable skill for front-end developers and UI designers to create scalable and maintainable stylesheets.
Progress0 / 4 steps