📖 Scenario: You are creating a simple webpage style where you want to use math functions from the sass:math module to calculate sizes dynamically.
🎯 Goal: Build a Sass stylesheet that uses the sass:math module to calculate a font size and a container width based on given base values.
📋 What You'll Learn
Import the sass:math module
Create a base font size variable
Create a scale factor variable
Calculate a larger font size using math.mul()
Calculate a container width using math.div()
💡 Why This Matters
🌍 Real World
Using the sass:math module helps you write cleaner, more maintainable styles by calculating sizes dynamically instead of hardcoding values.
💼 Career
Many front-end developer roles require knowledge of Sass and its modules to create scalable and flexible CSS for websites and apps.
Progress0 / 4 steps
1
Import sass:math module and set base variables
Write an @use statement to import the sass:math module as math. Then create a variable called $base-font-size and set it to 1.2rem. Also create a variable called $scale-factor and set it to 1.5.
SASS
Hint
Use @use "sass:math" as math; to import the module. Then create variables with $ and assign the exact values.
2
Calculate larger font size using math.mul()
Create a variable called $large-font-size and set it to the result of multiplying $base-font-size by $scale-factor using math.mul().
SASS
Hint
Use math.mul() with the two variables inside the parentheses.
3
Calculate container width using math.div()
Create a variable called $container-width and set it to the result of dividing 1200px by $scale-factor using math.div().
SASS
Hint
Use math.div() with 1200px and $scale-factor as arguments.
4
Use variables in CSS rules
Write CSS rules for body and .container. Set font-size of body to $large-font-size. Set width of .container to $container-width. Use the variables exactly as written.
SASS
Hint
Write CSS selectors body and .container with their properties using the variables.
Practice
(1/5)
1. What is the main purpose of the sass:math module in Sass?
easy
A. To provide useful math functions like rounding and max/min.
B. To add color manipulation functions.
C. To create animations in Sass.
D. To handle file imports automatically.
Solution
Step 1: Understand the module's role
The sass:math module offers math functions such as rounding, max, min, and angle conversions.
Step 2: Compare options with module features
Only To provide useful math functions like rounding and max/min. correctly describes these math functions; others mention unrelated features.
Final Answer:
To provide useful math functions like rounding and max/min. -> Option A
Quick Check:
sass:math = math functions [OK]
Hint: Remember sass:math is for math functions only [OK]
Common Mistakes:
Confusing sass:math with color or animation modules
Thinking sass:math handles file imports
Assuming sass:math adds new CSS properties
2. Which of the following is the correct way to import the sass:math module in a Sass file?
easy
A. @import 'sass:math';
B. @include 'sass:math';
C. @require 'sass:math';
D. @use 'sass:math';
Solution
Step 1: Recall Sass module import syntax
Sass uses @use to import modules like sass:math.
Step 2: Check each option's validity
@import is deprecated for modules, @require and @include are invalid for imports.
B. Using math.deg instead of math.rad to convert degrees to radians.
C. Cannot assign units like deg to variables.
D. Missing semicolon after $angle declaration.
Solution
Step 1: Understand math.deg and math.rad functions
math.deg() converts radians to degrees; math.rad() converts degrees to radians.
Step 2: Check code's intent and function used
The variable $angle is in degrees, but math.deg() is used, which expects radians. The correct function is math.rad().
Final Answer:
Using math.deg instead of math.rad to convert degrees to radians. -> Option B
Quick Check:
Degrees to radians = math.rad [OK]
Hint: Use math.rad for degrees to radians, math.deg for reverse [OK]
Common Mistakes:
Mixing up math.deg and math.rad functions
Thinking units like deg can't be assigned
Assuming missing semicolon causes error
5. You want to create a Sass variable $max-width that is the maximum of three values: 300px, 25rem, and 400px. Which code correctly uses sass:math to find this maximum, assuming 1rem = 16px?
hard
A. @use 'sass:math';
$max-width: math.max(300px, 25rem, 400px);
B. @use 'sass:math';
$max-width: math.max(300px, 400px, 25rem);
C. @use 'sass:math';
$max-width: math.max(300px, 25 * 16px, 400px);
D. @use 'sass:math';
$max-width: math.max(300px, math.div(25 * 16, 1), 400px);
Solution
Step 1: Understand unit conversion for comparison
To compare 25rem with px, convert 25rem to px: 25 * 16px = 400px.
Step 2: Use math.max with consistent units
@use 'sass:math';
$max-width: math.max(300px, 25 * 16px, 400px); converts 25rem to 400px by multiplying 25 * 16px, then finds max among 300px, 400px, 400px.