📖 Scenario: You are creating styles for a website. You want to use Sass string functions to manipulate text values for colors and fonts in your styles.
🎯 Goal: Build a Sass stylesheet that uses built-in string functions to change text case, check string length, and extract parts of strings.
📋 What You'll Learn
Create a Sass variable with a string value
Create a helper variable for a string length threshold
Use Sass string functions to transform and check the string
Output the final CSS rules using the transformed strings
💡 Why This Matters
🌍 Real World
Web developers often need to manipulate text values in stylesheets for dynamic theming or responsive design.
💼 Career
Knowing Sass string functions helps front-end developers write cleaner, more maintainable CSS with dynamic text styling.
Progress0 / 4 steps
1
Create a Sass variable with a string
Create a Sass variable called $font-name and set it to the string "OpenSans".
SASS
Hint
Use $font-name: "OpenSans"; to create the variable.
2
Add a length threshold variable
Create a Sass variable called $min-length and set it to the number 7.
SASS
Hint
Use $min-length: 7; to create the variable.
3
Use string functions to check length and uppercase
Create a variable called $font-name-length that stores the length of $font-name using the Sass str-length() function. Then create a variable called $font-name-upper that stores the uppercase version of $font-name using the Sass to-upper-case() function.
SASS
Hint
Use str-length($font-name) and to-upper-case($font-name) to create the variables.
4
Output CSS using the transformed strings
Write a CSS rule for body that sets font-family to $font-name-upper. Then write a CSS rule for h1 that sets content to the first 4 characters of $font-name using the Sass str-slice() function.
SASS
Hint
Use font-family: $font-name-upper; in body and content: str-slice($font-name, 1, 4); in h1::after.
Practice
(1/5)
1. Which Sass built-in string function returns the length of a string? length("hello")
easy
A. Returns the string in uppercase
B. Returns the number of characters in the string
C. Returns the string reversed
D. Returns the first character of the string
Solution
Step 1: Understand the length() function
The length() function in Sass returns how many characters are in a string.
Step 2: Apply to the example string
For the string "hello", the length is 5 characters.
Final Answer:
Returns the number of characters in the string -> Option B
Quick Check:
length("hello") = 5 [OK]
Hint: Remember length() counts characters in a string [OK]
Common Mistakes:
Confusing length() with uppercase conversion
Thinking length() returns the first character
Mixing length() with reverse functions
2. Which of the following is the correct syntax to convert a string to uppercase in Sass?
easy
A. uppercase("text")
B. to-upper-case("text")
C. toUpperCase("text")
D. to-upper("text")
Solution
Step 1: Recall Sass string uppercase function
The correct Sass function to convert a string to uppercase is uppercase().
Step 2: Check syntax correctness
The function is called as uppercase("text"), which matches uppercase("text").
Final Answer:
uppercase("text") -> Option A
Quick Check:
uppercase("text") converts to "TEXT" [OK]
Hint: Use uppercase() exactly as shown for capital letters [OK]
Common Mistakes:
Using camelCase like toUpperCase() which is not Sass syntax
Adding dashes incorrectly in function names
Confusing with CSS text-transform property
3. What is the output of this Sass code?
$str: "Sass";
$result: str-insert($str, "3", 3);
What is the value of $result?
medium
A. "Sass3"
B. "Sas3s"
C. "S3ass"
D. "Sa3ss"
Solution
Step 1: Understand str-insert() function
This function inserts a string into another string at a given index (1-based).
Step 2: Apply insertion at position 3
Original string is "Sass". Insert "3" at position 3 means after the second character: "Sa" + "3" + "ss" = "Sa3ss".
Final Answer:
"Sa3ss" -> Option D
Quick Check:
str-insert("Sass", "3", 3) = "Sa3ss" [OK]
Hint: Remember str-insert uses 1-based index for insertion [OK]