📖 Scenario: You are creating a simple style sheet for a website that has multiple button colors. You want to use Sass variables and variable interpolation to generate CSS classes dynamically.
🎯 Goal: Build a Sass file that uses variable interpolation with #{} to create CSS classes for buttons with different colors.
📋 What You'll Learn
Create a Sass list of color names
Create a variable for the CSS class prefix
Use a @each loop with variable interpolation #{} to generate CSS classes
Add a style rule inside each generated class to set the background color
💡 Why This Matters
🌍 Real World
Variable interpolation helps you write less repetitive CSS by generating classes dynamically, which is useful in theming and design systems.
💼 Career
Front-end developers use Sass and variable interpolation to maintain scalable and maintainable stylesheets in professional web projects.
Progress0 / 4 steps
1
Create a list of colors
Create a Sass list called $colors with these exact values: red, green, and blue.
SASS
Hint
Use parentheses or commas to create a list in Sass, like $list: (item1, item2);.
2
Create a class prefix variable
Create a variable called $btn-prefix and set it to the string btn.
SASS
Hint
Assign a string value to a variable like $var-name: 'value';.
3
Use @each loop with variable interpolation
Write an @each loop that iterates over $colors with the variable $color. Inside the loop, create a CSS class selector using variable interpolation with .#{$btn-prefix}-#{$color}.
SASS
Hint
Use @each $item in $list { ... } and inside use .#{$var} to insert variable values in selectors.
4
Add background color style inside each class
Inside the @each loop, add a CSS property background-color and set it to the current $color variable.
SASS
Hint
Inside the selector block, write background-color: $color; to apply the color.
Practice
(1/5)
1. What does the #{} syntax do in Sass?
easy
A. It defines a mixin.
B. It comments out a line of code.
C. It creates a new variable.
D. It inserts the value of a variable inside a string or selector.
Solution
Step 1: Understand the purpose of #{} in Sass
The #{} syntax is used to insert or interpolate variables inside strings or selectors, making styles dynamic.
Step 2: Compare with other options
Commenting, variable creation, and mixin definition use different syntax, so they are incorrect here.
Final Answer:
It inserts the value of a variable inside a string or selector. -> Option D
Quick Check:
#{variable} inserts variable value [OK]
Hint: Remember #{ } means insert variable value inside text [OK]
Common Mistakes:
Thinking #{ } creates variables
Confusing interpolation with comments
Using #{ } to define mixins
2. Which of the following is the correct way to use variable interpolation in a selector in Sass?
easy
A. .button-#{$size} { color: blue; }
B. .button-$size { color: blue; }
C. .button-{$size} { color: blue; }
D. .button-#size { color: blue; }
Solution
Step 1: Identify correct interpolation syntax
Variable interpolation requires #{} around the variable name to insert its value inside selectors.
Step 2: Check each option
.button-#{$size} { color: blue; } uses .#{$size} correctly. Options A, C, and D miss the # or curly braces or both, so they are invalid.
Final Answer:
.button-#{$size} { color: blue; } -> Option A
Quick Check:
Use .class-#{$var} for interpolation [OK]
Hint: Use #{} around variables inside selectors [OK]