Discover how a tiny syntax trick can save you hours of repetitive CSS work!
Why Variable interpolation with #{} in SASS? - Purpose & Use Cases
Imagine you want to create multiple CSS classes with similar names but different colors, like .btn-red, .btn-blue, .btn-green, and you write each class manually.
Writing each class by hand is slow and boring. If you want to change the color names or add new ones, you must rewrite or copy-paste many lines, risking mistakes and wasting time.
Variable interpolation with #{} lets you insert variables inside selectors or property names, so you can write one template and generate many classes automatically.
.btn-red { color: red; }
.btn-blue { color: blue; }$colors: red blue; @each $color in $colors { .btn-#{$color} { color: $color; } }
This lets you create flexible, reusable styles that adapt easily when you change variables, saving time and avoiding errors.
When building a website with many button styles, you can define colors once and generate all button classes dynamically, making updates fast and consistent.
Manual CSS duplication is slow and error-prone.
Variable interpolation #{} inserts variables into selectors or properties.
This makes your styles flexible, reusable, and easy to update.
Practice
#{} syntax do in Sass?Solution
Step 1: Understand the purpose of
The#{}in Sass#{}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 DQuick Check:
#{variable} inserts variable value [OK]
- Thinking #{ } creates variables
- Confusing interpolation with comments
- Using #{ } to define mixins
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 AQuick Check:
Use .class-#{$var} for interpolation [OK]
- Omitting # in interpolation
- Omitting curly braces {}
- Using $ without interpolation
$color: red;
$size: large;
.button-#{$size} {
color: #{$color};
}What CSS will this generate?
Solution
Step 1: Replace variables inside interpolation
The variable$sizeis 'large', so.button-#{$size}becomes.button-large. The variable$coloris 'red', socolor: #{$color}becomescolor: red.Step 2: Write the final CSS output
After interpolation, the CSS is.button-large { color: red; }.Final Answer:
.button-large { color: red; } -> Option CQuick Check:
Interpolation replaces variables with values [OK]
- Leaving interpolation syntax in output
- Not replacing variables correctly
- Confusing variable names with values
$name: "header";
.#$name {
font-weight: bold;
}Solution
Step 1: Check variable usage in selector
The selector uses.$namebut misses#{}for interpolation, so Sass treats it as a literal string, causing an error.Step 2: Correct usage
It should be.#{$name}to insert the variable value inside the selector.Final Answer:
Missing interpolation syntax #{ } around $name in selector -> Option AQuick Check:
Use .#{$var} to interpolate variables in selectors [OK]
- Using $var without #{ } in selectors
- Assuming variables work without interpolation
- Confusing property errors with selector errors
$sizes: small, medium, large;
@each $size in $sizes {
.btn-#{$size} {
font-size: if($size == small, 1rem, if($size == medium, 1.5rem, 2rem));
}
}What will be the font-size for
.btn-large in the generated CSS?Solution
Step 1: Understand the loop and condition
The loop iterates over sizes: small, medium, large. For each, it creates a class with font-size based on the condition.Step 2: Evaluate font-size for large
Forlarge, the condition$size == smallis false,$size == mediumis false, so it uses the else value2rem.Final Answer:
2rem -> Option BQuick Check:
Conditional font-size for large = 2rem [OK]
- Assuming large equals small or medium
- Not evaluating nested if correctly
- Confusing variable interpolation with condition logic
Ready to prove it?
Pass the test and this pattern moves unknown
