The sass:string module helps you work with text in your stylesheets. It lets you change, check, and combine words easily.
sass:string module
@use 'sass:string'; string.function-name(arguments...)
Always start by loading the module with @use 'sass:string';.
Functions like string.length() or string.uppercase() help you work with text.
@use 'sass:string'; $string: string.uppercase('hello');
@use 'sass:string'; $length: string.length('Sass');
@use 'sass:string'; $joined: string.join(('good', ' ', 'morning'), '');
@use 'sass:string'; $contains: string.index('hello world', 'world');
This example uses the sass:string module to change text to uppercase, count characters, and join words. The results appear on the page using CSS content property.
@use 'sass:string'; $greeting: 'hello'; $shout: string.uppercase($greeting); $length: string.length($greeting); $full: string.join(($greeting, ' ', 'Sass learner!'), ''); body { font-family: Arial, sans-serif; padding: 1rem; } h1 { color: blue; content: $shout; } p { font-size: 1.2rem; color: gray; } /* Using CSS custom property to show length */ :root { --greeting-length: #{$length}; } footer::after { content: 'Length of greeting: ' var(--greeting-length); display: block; margin-top: 1rem; font-style: italic; color: darkgreen; } /* Show full greeting in a paragraph */ p::before { content: "#{$full}"; }
Remember to always @use 'sass:string'; before calling its functions.
Functions return new strings; they do not change the original text.
You can combine these functions to create dynamic and flexible styles.
The sass:string module helps you work with text in Sass.
Use it to change case, join, find, or measure strings.
It makes your styles smarter and easier to manage.
Practice
sass:string module in Sass?Solution
Step 1: Understand the module's role
Thesass:stringmodule 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 CQuick Check:
sass:string = text manipulation [OK]
- Confusing string module with color or layout modules
- Thinking it handles animations
- Assuming it manages Sass variables
string module in Sass?Solution
Step 1: Recall Sass module import syntax
Modern Sass uses@useto import modules, so@use 'sass:string';is correct.Step 2: Identify incorrect options
@importis deprecated for modules,@requireand@includeare invalid for importing modules.Final Answer:
@use 'sass:string'; -> Option AQuick Check:
Use @use for modules [OK]
- Using @import instead of @use
- Trying @require or @include which are invalid
- Forgetting quotes around module name
@use 'sass:string';
$word: 'Hello';
$result: string.to-upper-case($word);
.test {
content: $result;
}Solution
Step 1: Understand the function used
string.to-upper-case()converts all letters in the string to uppercase.Step 2: Apply function to input
Input is 'Hello', output becomes 'HELLO'. The CSS rule sets content to 'HELLO'.Final Answer:
.test { content: 'HELLO'; } -> Option AQuick Check:
to-upper-case('Hello') = 'HELLO' [OK]
- Confusing to-upper-case with to-lower-case
- Expecting original string unchanged
- Thinking function is undefined
string module:@use 'sass:string';
$greeting: 'Hi';
$new: string.append($greeting, 123);
.test {
content: $new;
}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 BQuick Check:
string.append needs strings, not numbers [OK]
- Passing numbers without converting to strings
- Confusing function names
- Incorrect module import syntax
sass:string module to achieve this?Solution
Step 1: Understand the goal
The function must uppercase the first letter and lowercase the rest of the string.Step 2: Analyze each option
@use 'sass:string'; @function capitalize($text) { @return string.to-upper-case(string.slice($text, 1, 1)) + string.to-lower-case(string.slice($text, 2)); } correctly slices the first character, uppercases it, then slices the rest and lowercases it, joining both parts.
@use 'sass:string'; @function capitalize($text) { @return string.to-lower-case(string.slice($text, 1, 1)) + string.to-upper-case(string.slice($text, 2)); } reverses cases incorrectly.
@use 'sass:string'; @function capitalize($text) { @return string.to-upper-case($text); } uppercases whole string.
@use 'sass:string'; @function capitalize($text) { @return string.to-lower-case($text); } lowercases whole string.Final Answer:
Capitalizes the first letter and lowercases the rest -> Option DQuick Check:
First letter uppercase + rest lowercase = slice(1,1) upper + slice(2) lower [OK]
- Applying case functions to whole string only
- Mixing up upper and lower case parts
- Not slicing string correctly
Ready to prove it?
Pass the test and this pattern moves unknown
