Strings let you store text in your styles. Concatenation means joining strings together to make new text.
String types and concatenation in SASS
$string1: "Hello"; $string2: 'World'; $combined: $string1 + " " + $string2;
You can use either double quotes "" or single quotes '' for strings.
Use the + operator to join strings together.
$greeting: "Hi" + " there!";
$font-size: 16 + "px";
$full-name: 'John' + ' ' + 'Doe';
This Sass code creates two strings and joins them with a space. Then it sets the combined string as the content of the ::before pseudo-element of the body, so the text "Good Morning" appears at the top of the page.
@use "sass:string"; $first: "Good"; $second: 'Morning'; $combined: $first + " " + $second; body::before { content: $combined; display: block; font-family: Arial, sans-serif; font-size: 1.5rem; color: #333; }
Remember, the content property only shows text when used with ::before or ::after pseudo-elements.
Strings in Sass keep their quotes unless you use functions to remove them.
Strings store text in Sass and can use single or double quotes.
Use the + operator to join strings together.
Concatenation helps build dynamic style values easily.
Practice
Solution
Step 1: Understand string syntax in Sass
Strings must be enclosed in matching single or double quotes.Step 2: Check each option for correct quotes
content: "Hello World"; uses double quotes correctly. content: Hello World; lacks quotes. content: 'Hello World; has unmatched quotes. content: Hello+World; uses plus sign incorrectly.Final Answer:
content: "Hello World"; -> Option AQuick Check:
Strings need quotes = D [OK]
- Forgetting quotes around strings
- Using unmatched quotes
- Trying to concatenate without operator
'Hello' and 'World' in Sass?Solution
Step 1: Recall Sass string concatenation operator
Sass uses the plus sign (+) to join strings.Step 2: Verify each option's operator
content: 'Hello' + 'World'; uses + correctly. Options A, B, D use invalid operators for strings.Final Answer:
content: 'Hello' + 'World'; -> Option DQuick Check:
Use + to join strings = C [OK]
- Using & or . instead of + for concatenation
- Trying to subtract strings
- Missing quotes around strings
$greeting: 'Hi'; $name: 'Alice'; content: $greeting + ', ' + $name + '!';
Solution
Step 1: Understand string concatenation with variables
Variables $greeting and $name hold strings. Concatenation joins them with ', ' and '!'.Step 2: Combine strings step-by-step
'Hi' + ', ' = 'Hi, ' then + 'Alice' = 'Hi, Alice' then + '!' = 'Hi, Alice!'Final Answer:
Hi, Alice! -> Option AQuick Check:
Concatenate with + gives 'Hi, Alice!' [OK]
- Forgetting spaces in concatenation
- Treating + as literal text
- Missing quotes around punctuation
$part1: 'Good'; $part2: 'Morning'; content: $part1 + $part2;
Solution
Step 1: Check concatenation result
Concatenating 'Good' + 'Morning' results in 'GoodMorning' without space.Step 2: Identify missing space issue
To get 'Good Morning', a space string ' ' must be added between variables.Final Answer:
Missing space between words in concatenation -> Option BQuick Check:
Concatenation needs spaces explicitly [OK]
- Assuming + adds spaces automatically
- Not adding space string between variables
- Confusing syntax errors with logic errors
'btn-' with a variable $color that can be 'red' or 'blue'. Which Sass code correctly creates the class name .btn-red or .btn-blue?Solution
Step 1: Understand string interpolation in selectors
Sass uses #{} to insert variable values inside strings or selectors.Step 2: Check each option's syntax
.btn-#{$color} { color: $color; } uses .btn-#{$color} which correctly joins 'btn-' and variable. .btn-" + $color + " { color: $color; } uses + which is invalid in selectors. .btn-{$color} { color: $color; } misses #{} so variable won't be replaced. .btn-" #{$color} " { color: $color; } has invalid quotes placement.Final Answer:
.btn-#{$color} { color: $color; } -> Option CQuick Check:
Use #{} for variable interpolation in selectors [OK]
- Trying to use + for selector concatenation
- Forgetting #{} for interpolation
- Incorrect quote placement around variables
Ready to prove it?
Pass the test and this pattern moves unknown
