We switch from @import to @use in Sass to organize styles better and avoid repeating code. @use helps keep things clear and safe.
@import to @use migration in SASS
Start learning this pattern below
Jump into concepts and practice - no test required
@use 'filename' [as namespace];
@use loads a Sass file once and gives you access to its variables, mixins, and functions.
You can add a namespace to keep things organized, like @use 'colors' as c;.
colors.scss file with default namespace colors.@use 'colors';
colors.scss but uses c as a short namespace.@use 'colors' as c;
mixins.scss and makes its contents available without a namespace.@use 'mixins' as *;
This example shows how to use variables and mixins from a colors.scss file using @use with a namespace c. It styles a button with colors and rounded corners.
@use 'colors' as c; .button { background-color: c.$primary-color; color: c.$text-color; @include c.rounded-corners; }
@import loads files multiple times and can cause conflicts; @use loads once.
Always use namespaces with @use to avoid name clashes, unless you use as *.
Update your old @import files gradually to @use for better Sass code.
@use replaces @import for safer and clearer Sass code.
It loads files once and uses namespaces to organize styles.
Use @use 'filename' as ns; to access variables and mixins with ns. prefix.
Practice
@use instead of @import in Sass?Solution
Step 1: Understand
@importbehavior@importloads files multiple times and merges all variables globally, which can cause conflicts.Step 2: Understand
@useimprovements@useloads 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 BQuick Check:
@use= safer, namespaced imports [OK]
@use loads once with namespaces [OK]- Thinking
@useimports CSS files directly - Assuming
@useruns JavaScript - Believing
@usecompiles Sass automatically
@import 'colors'; with @use and a namespace c?Solution
Step 1: Recall
The correct syntax is@usesyntax@use 'filename' as namespace;with quotes around filename andaskeyword.Step 2: Match options
@use 'colors' as c; matches the correct syntax exactly:@use 'colors' as c;.Final Answer:
@use 'colors' as c; -> Option AQuick Check:
Correct@usesyntax = @use 'colors' as c; [OK]
- Omitting quotes around filename
- Using 'namespace' instead of 'as'
- Placing namespace inside quotes
// _variables.scss
$primary-color: blue;
// styles.scss
@use 'variables' as vars;
.button {
color: vars.$primary-color;
}What color will the button text be in the compiled CSS?
Solution
Step 1: Understand variable access with
The variable@use$primary-coloris accessed with the namespace prefixvars.$primary-color.Step 2: Check variable value
In_variables.scss,$primary-coloris set toblue, so the button color will be blue.Final Answer:
blue -> Option AQuick Check:
Namespace prefix accesses variable value correctly [OK]
- Using variable without namespace prefix
- Expecting variable name as string output
- Assuming default color red
@import to @use?@use 'mixins';
.button {
@include border-radius(5px);
}Solution
Step 1: Understand
When using@usenamespace requirement@use, all variables and mixins must be accessed with the namespace prefix unless configured otherwise.Step 2: Identify missing prefix
The code calls@include border-radius(5px);without prefix. It should be@include mixins.border-radius(5px);.Final Answer:
Missing namespace prefix beforeborder-radiusmixin. -> Option CQuick Check:
@userequires namespace prefix [OK]
- Calling mixins without namespace prefix
- Confusing quotes usage in
@use - Thinking
@includeis disallowed
// _colors.scss
$primary: red;
// _theme.scss
@use 'colors' as c;
$primary: blue !default;
// styles.scss
@use 'theme' as t;
.button {
color: t.$primary;
}What color will the button text be and why?
Solution
Step 1: Understand
The!defaultbehavior!defaultflag sets a variable only if it is not already set.Step 2: Analyze variable values
$primaryincolorsisred. Intheme,$primaryis set toblue !default, so it will only be set if$primarywas not already set.Step 3: Check namespaces and usage
themeusescolorsasc, so$primaryfromcolorsis already set. Therefore,blue !defaultdoes not overridered.Final Answer:
red, because$primaryincolorsoverrides!defaultintheme. -> Option DQuick Check:
!defaultonly sets if variable is unset, so red stays [OK]
!default sets only if variable is unset [OK]- Assuming
!defaultalways overrides - Ignoring namespaces in variable access
- Thinking variables merge without prefixes
