📖 Scenario: You are creating a style sheet for a website that needs to display text in a special way. You want to use Sass's string module to change and combine text strings dynamically.
🎯 Goal: Build a Sass stylesheet that uses the string module functions to manipulate text strings and apply the results as CSS content.
📋 What You'll Learn
Use the Sass string module functions
Create variables with exact string values
Concatenate strings using string.concat()
Extract parts of strings using string.slice()
Use the manipulated strings in CSS content properties
💡 Why This Matters
🌍 Real World
Manipulating text strings in Sass helps create dynamic styles, such as custom content or theme-based text changes, without changing HTML.
💼 Career
Knowing how to use Sass modules like string is useful for front-end developers to write cleaner, more maintainable stylesheets that adapt to different content needs.
Progress0 / 4 steps
1
Create initial string variables
Create two string variables called $greeting with the value "Hello" and $name with the value "World".
SASS
Hint
Use $variable-name: "value"; to create string variables.
2
Add a separator variable
Add a string variable called $separator with the value ", " (a comma and a space).
SASS
Hint
Remember to include the comma and space inside quotes.
3
Concatenate strings using string.concat()
Use the Sass string.concat() function to create a new variable called $full-greeting that combines $greeting, $separator, and $name in that order.
SASS
Hint
Import the string module with @use "sass:string"; before using string.concat().
4
Use string.slice() and apply content in CSS
Create a variable called $short-greeting that uses string.slice() to get the first 5 characters of $full-greeting. Then create a CSS rule for p::before that sets the content property to $short-greeting.
SASS
Hint
Remember that string.slice() uses 1-based indexing and the end index is inclusive.
Practice
(1/5)
1. What is the main purpose of the sass:string module in Sass?
easy
A. To handle layout grids and flexbox
B. To create animations using keyframes
C. To manipulate and work with text strings in Sass stylesheets
D. To manage colors and gradients
Solution
Step 1: Understand the module's role
The sass:string module provides functions to work with text strings, such as changing case or joining strings.
Step 2: Compare with other options
Options A, B, and C relate to layout, animations, and colors, which are not the focus of the string module.
Final Answer:
To manipulate and work with text strings in Sass stylesheets -> Option C
Quick Check:
sass:string = text manipulation [OK]
Hint: Remember sass:string is for text, not colors or layout [OK]
Common Mistakes:
Confusing string module with color or layout modules
Thinking it handles animations
Assuming it manages Sass variables
2. Which of the following is the correct way to import the string module in Sass?
easy
A. @use 'sass:string';
B. @import 'string';
C. @require 'sass:string';
D. @include 'string';
Solution
Step 1: Recall Sass module import syntax
Modern Sass uses @use to import modules, so @use 'sass:string'; is correct.
Step 2: Identify incorrect options
@import is deprecated for modules, @require and @include are invalid for importing modules.
Final Answer:
@use 'sass:string'; -> Option A
Quick Check:
Use @use for modules [OK]
Hint: Use @use 'sass:string'; to import string module [OK]
C. Module name should be 'string-module' not 'sass:string'
D. Function name should be string.concat, not string.append
Solution
Step 1: Check function usage
string.append() expects string arguments, but 123 is a number, causing a type error.
Step 2: Verify other syntax
Semicolons are correct, module name and function name are valid as per Sass string module.
Final Answer:
Cannot append a number directly to a string -> Option B
Quick Check:
string.append needs strings, not numbers [OK]
Hint: Append only strings, convert numbers first [OK]
Common Mistakes:
Passing numbers without converting to strings
Confusing function names
Incorrect module import syntax
5. You want to create a Sass function that takes a string and returns it with the first letter capitalized and the rest lowercase. Which of these code snippets correctly uses the sass:string module to achieve this?
hard
A. @use 'sass:string';
@function capitalize($text) {
@return string.to-lower-case($text);
}