Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Typography Scale Generation with Sass
📖 Scenario: You are designing a website and want to create a consistent typography scale using Sass. This scale will help you apply harmonious font sizes across headings and paragraphs.
🎯 Goal: Build a Sass setup that generates a typography scale using a base font size and a scale ratio. You will create variables and a loop to produce font sizes for different heading levels.
📋 What You'll Learn
Create a base font size variable called $base-font-size with the value 1rem.
Create a scale ratio variable called $scale-ratio with the value 1.25.
Use a Sass @for loop with a variable $i from 1 to 6 to generate font sizes for headings h1 to h6.
Inside the loop, calculate the font size by multiplying $base-font-size by $scale-ratio raised to the power of (6 - $i).
Assign the calculated font size to each heading selector h#{$i}.
💡 Why This Matters
🌍 Real World
Typography scales help designers keep font sizes consistent and visually balanced across a website, improving readability and aesthetics.
💼 Career
Front-end developers and UI designers often use Sass to manage styles efficiently and create scalable design systems.
Progress0 / 4 steps
1
Set up base font size variable
Create a Sass variable called $base-font-size and set it to 1rem.
SASS
Hint
Use the syntax $variable-name: value; to create a Sass variable.
2
Add scale ratio variable
Create a Sass variable called $scale-ratio and set it to 1.25.
SASS
Hint
Remember to end the line with a semicolon.
3
Write a loop to generate heading font sizes
Write a Sass @for loop using the variable $i from 1 through 6.
SASS
Hint
Use @for $i from 1 through 6 { ... } to loop from 1 to 6.
4
Calculate and assign font sizes inside the loop
Inside the @for loop, calculate the font size by multiplying $base-font-size by $scale-ratio raised to the power of (6 - $i). Assign this font size to the heading selector h#{$i} using the font-size property.
SASS
Hint
Use $scale-ratio ** (6 - $i) to raise the scale ratio to the power of (6 - $i).
Wrap the CSS selector and property inside the loop.
Practice
(1/5)
1. What is the main purpose of a typography scale in Sass?
easy
A. To create random font sizes for variety
B. To add animations to text elements
C. To change colors of text dynamically
D. To keep font sizes balanced and consistent across a website
Solution
Step 1: Understand typography scale concept
A typography scale is used to keep font sizes balanced and consistent, avoiding random sizes.
Step 2: Identify the correct purpose
Options A, C, and D describe unrelated tasks like randomness, colors, or animations, which are not the purpose of typography scales.
Final Answer:
To keep font sizes balanced and consistent across a website -> Option D
Quick Check:
Typography scale = balanced font sizes [OK]
Hint: Typography scales keep font sizes consistent and balanced [OK]
Common Mistakes:
Thinking typography scales create random sizes
Confusing typography scale with color or animation features
Assuming typography scales are for layout spacing
2. Which of the following is the correct Sass function syntax to calculate a typography scale size with base size $base, ratio $ratio, and step $step?
easy
A. font-size: $base * $ratio ^ $step;
B. font-size: $base * math.pow($ratio, $step);
C. font-size: $base * ($ratio ** $step);
D. font-size: $base * pow($ratio, $step);
Solution
Step 1: Recall Sass math function syntax
Sass uses the math module for functions like pow(), so the correct syntax is math.pow($ratio, $step).
Step 2: Check each option
font-size: $base * pow($ratio, $step); uses pow() without math module, which is invalid. font-size: $base * $ratio ^ $step; uses ^ which is not Sass syntax. font-size: $base * ($ratio ** $step); uses ** which is not Sass syntax for exponentiation. font-size: $base * math.pow($ratio, $step); correctly uses math.pow($ratio, $step).
Final Answer:
font-size: $base * math.pow($ratio, $step); -> Option B
Quick Check:
Sass math.pow() = correct syntax [OK]
Hint: Use math.pow() with math module for exponent in Sass [OK]
In Sass, pow() is inside the math module, so it must be called as math.pow().
Step 2: Verify other syntax
Parameter names are fine, @return is correct Sass syntax, and function syntax is valid.
Final Answer:
Missing math module prefix for pow function -> Option A
Quick Check:
Use math.pow() not pow() alone [OK]
Hint: Always prefix pow() with math. in Sass functions [OK]
Common Mistakes:
Calling pow() without math prefix
Confusing @return with return keyword
Thinking function syntax is wrong
5. You want to generate a typography scale in Sass that skips step 0 and starts from step 1, doubling the font size each step from a base of 1rem. Which Sass code correctly generates the size for step 3?