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
@import to @use Migration in Sass
📖 Scenario: You have a small Sass project that uses the old @import rule to include styles from other files. You want to update it to use the modern @use rule, which is the recommended way to load Sass files now.This will help your styles be more organized and avoid problems with variables and mixins being loaded multiple times.
🎯 Goal: Convert a Sass file that uses @import to use @use instead. You will create a variables file, import it with @use, and then use the variables in your main style file.
📋 What You'll Learn
Create a Sass partial file named _variables.scss with some color variables.
Create a main Sass file named styles.scss that uses @use to load _variables.scss.
Use the variables from _variables.scss in styles.scss with the correct namespace.
Do not use @import anywhere in the final code.
💡 Why This Matters
🌍 Real World
Modern Sass projects use @use instead of @import to avoid duplicate styles and variable conflicts. This helps keep stylesheets clean and maintainable.
💼 Career
Front-end developers often need to update legacy Sass codebases to use @use and @forward for better modularity and performance.
Progress0 / 4 steps
1
Create _variables.scss with color variables
Create a Sass partial file named _variables.scss and define these variables exactly: $primary-color: #3498db; and $secondary-color: #2ecc71;
SASS
Hint
Remember to start the file name with an underscore (_) to make it a partial.
2
Create styles.scss and add @use for variables
Create a Sass file named styles.scss and add the line @use 'variables'; at the top to load the variables partial.
SASS
Hint
The @use rule loads the partial and adds a namespace by default.
3
Use variables from variables namespace in styles.scss
In styles.scss, add a CSS rule for body that sets background-color to variables.$primary-color and color to variables.$secondary-color.
SASS
Hint
Use the namespace variables. before variable names when using @use.
4
Remove any @import and finalize the Sass files
Make sure there is no @import in styles.scss or _variables.scss. The files should only use @use and variable definitions as before.
SASS
Hint
The modern Sass way is to use @use instead of @import. Make sure no @import remains.
Practice
(1/5)
1. What is the main advantage of using @use instead of @import in Sass?
easy
A. It allows importing CSS files directly without processing.
B. It loads files only once and uses namespaces to avoid conflicts.
C. It automatically compiles Sass to CSS without a compiler.
D. It enables inline JavaScript execution inside Sass files.
Solution
Step 1: Understand @import behavior
@import loads files multiple times and merges all variables globally, which can cause conflicts.
Step 2: Understand @use improvements
@use loads files only once and requires a namespace prefix, preventing variable and mixin conflicts.
Final Answer:
It loads files only once and uses namespaces to avoid conflicts. -> Option B
Quick Check:
@use = safer, namespaced imports [OK]
Hint: Remember: @use loads once with namespaces [OK]
Common Mistakes:
Thinking @use imports CSS files directly
Assuming @use runs JavaScript
Believing @use compiles Sass automatically
2. Which of the following is the correct syntax to replace @import 'colors'; with @use and a namespace c?
easy
A. @use 'colors' as c;
B. @use colors as 'c';
C. @use 'colors' namespace c;
D. @use 'colors' with c;
Solution
Step 1: Recall @use syntax
The correct syntax is @use 'filename' as namespace; with quotes around filename and as keyword.
Step 2: Match options
@use 'colors' as c; matches the correct syntax exactly: @use 'colors' as c;.
Final Answer:
@use 'colors' as c; -> Option A
Quick Check:
Correct @use syntax = @use 'colors' as c; [OK]
Hint: Use quotes and 'as' keyword for namespace [OK]