📖 Scenario: You are creating a simple style sheet for a webpage. You want to use Sass's built-in math module to perform some calculations for your styles.
🎯 Goal: Build a Sass file that imports the built-in math module using @use and uses it to set a CSS custom property with a calculated value.
📋 What You'll Learn
Use the @use rule to import the built-in math module.
Create a CSS custom property --half-width that is half of 200px using math.div().
Write valid Sass code that compiles to CSS with the custom property inside the :root selector.
💡 Why This Matters
🌍 Real World
Using built-in Sass modules helps you write cleaner and more powerful stylesheets by reusing common functions like math calculations.
💼 Career
Knowing how to import and use Sass modules is important for front-end developers working with advanced CSS preprocessors to build scalable and maintainable styles.
Progress0 / 4 steps
1
Create the Sass file with a :root selector
Write a Sass file that starts with a :root selector block. Inside it, create a CSS custom property called --width and set it to 200px.
SASS
Hint
Use :root { --width: 200px; } to define the custom property.
2
Import the built-in math module with @use
Add a line at the top of the Sass file to import the built-in math module using @use "sass:math";.
SASS
Hint
Use @use "sass:math"; at the very top of the file.
3
Create a new custom property --half-width using math.div()
Inside the :root selector, add a new CSS custom property called --half-width. Set its value to half of 200px by using math.div(200, 2) with math.div().
SASS
Hint
Use interpolation #{} to insert the result of math.div(200, 2) and add px after it.
4
Complete the Sass file with correct syntax
Ensure the Sass file starts with @use "sass:math"; and contains the :root selector with both --width and --half-width custom properties defined correctly.
SASS
Hint
Check that the file has the import and both custom properties inside :root.
Practice
(1/5)
1. What does the Sass @use rule do when importing built-in modules?
easy
A. It imports CSS files directly into Sass without any namespace.
B. It imports a built-in Sass module to use its features with a namespace.
C. It compiles Sass files into CSS automatically.
D. It removes unused CSS rules from the stylesheet.
Solution
Step 1: Understand the purpose of @use
The @use rule imports Sass modules and gives access to their functions and variables under a namespace.
Step 2: Differentiate from other Sass features
Unlike @import, @use does not import raw CSS but Sass modules with scoped namespaces.
Final Answer:
It imports a built-in Sass module to use its features with a namespace. -> Option B
Quick Check:
@use imports modules with namespace [OK]
Hint: Remember: @use always adds a namespace to imported modules [OK]
Common Mistakes:
Confusing @use with @import for CSS files
Thinking @use compiles Sass to CSS
Assuming @use removes unused CSS
2. Which of the following is the correct syntax to import the built-in Sass math module using @use?
easy
A. @use "math";
B. @import "sass:math";
C. @use "sass:math";
D. @use math;
Solution
Step 1: Recall the correct syntax for built-in modules
Built-in Sass modules are imported with @use "sass:module-name"; syntax.
Step 2: Check each option
@use "sass:math"; uses the correct syntax with quotes and the prefix sass:. Others miss quotes, prefix, or use @import.
Final Answer:
@use "sass:math"; -> Option C
Quick Check:
Built-in modules need @use "sass:module"; [OK]
Hint: Built-in modules always use "sass:" prefix inside quotes [OK]