The @use directive helps you bring styles from one Sass file into another safely and clearly.
@use directive for imports in SASS
@use 'filename';
The filename is usually a relative path to another Sass file without the .scss extension.
By default, @use loads the file once and namespaces its contents to avoid conflicts.
colors.scss file and you can access its variables or mixins with the colors. prefix.@use 'colors';
buttons.scss but uses btn. as a short prefix for its contents.@use 'buttons' as btn;
typography.scss and overrides the $font-size variable with 16px.@use 'typography' with ($font-size: 16px);
This example imports a colors.scss file that defines color variables. Then it uses those colors to style a button with background and text colors.
@use 'colors'; .button { background-color: colors.$primary-color; color: colors.$text-color; padding: 1rem 2rem; border-radius: 0.5rem; }
@use replaces the older @import and is better for avoiding style conflicts.
Always use the namespace (like colors.) to access variables or mixins from the imported file.
You can customize variables when importing using the with keyword.
@use imports Sass files with a clear namespace.
It helps keep styles organized and avoids conflicts.
You can rename the namespace or customize variables when importing.
Practice
@use directive in Sass?Solution
Step 1: Understand the role of
The@use@usedirective imports Sass files and applies a namespace to their contents.Step 2: Compare with other import methods
Unlike@import,@useavoids naming conflicts by requiring namespace prefixes.Final Answer:
To import Sass files with a namespace to avoid conflicts -> Option AQuick Check:
@useadds namespace = C [OK]
@use always adds a namespace [OK]- Thinking
@usecopies styles without namespace - Confusing
@usewith JavaScript execution - Believing
@useexports variables
colors.scss using @use?Solution
Step 1: Check correct quoting and file extension
The@usedirective requires the file path as a string without the extension, usually quoted.Step 2: Validate options
@use 'colors'; uses quotes and omits the extension, which is correct. Options B and C include the extension incorrectly or wrong quotes. @use colors; lacks quotes.Final Answer:
@use 'colors'; -> Option AQuick Check:
Quotes + no extension = D [OK]
@use [OK]- Including the .scss extension inside
@use - Not using quotes around the file name
- Using double quotes inconsistently
h1 text?@use 'colors';
h1 {
color: colors.$primary;
}// colors.scss $primary: blue; $secondary: green;
Solution
Step 1: Identify the imported variables
The@use 'colors';imports variables with the namespacecolors. The variable$primaryis set toblue.Step 2: Check the usage in
The color is set toh1colors.$primary, which refers toblue.Final Answer:
blue -> Option DQuick Check:
Namespace + variable = blue [OK]
- Forgetting to use the namespace prefix
- Confusing $primary with $secondary
- Assuming default color without import
@use 'theme';
h1 {
color: $main-color;
}// theme.scss $main-color: red;
Solution
Step 1: Understand
By default, variables must be accessed with the namespace prefix unless@usenamespace behavioras *is used to remove it.Step 2: Check the code usage
The code uses@use 'theme';withoutas *, so variables must be accessed with thetheme.prefix, but$main-coloris used without it, causing an undefined variable error.Step 3: Identify the actual problem
Withoutas *, the prefix is required, so Variables must be accessed with namespace prefix unless configured identifies the issue.Final Answer:
Variables must be accessed with namespace prefix unless configured -> Option CQuick Check:
Default namespace prefix required = A [OK]
as * to access variables directly [OK]- Thinking
as *is invalid syntax - Forgetting to prefix variables without
as * - Including .scss extension in
@use
variables.scss but rename its namespace to vars and override the variable $font-size to 1.5rem. Which @use syntax is correct?Solution
Step 1: Recall
The correct syntax is@usesyntax for renaming and configuring@use 'file' as namespace with (variable overrides);.Step 2: Match options to syntax
@use 'variables' as vars with ($font-size: 1.5rem); matches the correct order and syntax. Options B, C, and D have incorrect order or keywords.Final Answer:
@use 'variables' as vars with ($font-size: 1.5rem); -> Option BQuick Check:
Namespace + with (overrides) = A [OK]
as then with for renaming and overrides [OK]- Swapping
asandwithorder - Using invalid keywords like
rename - Omitting parentheses around overrides
Ready to prove it?
Pass the test and this pattern moves unknown
