The @forward directive lets you share styles from one Sass file to another easily. It helps organize your styles by re-exporting them.
@forward directive for re-exporting in SASS
@forward "filename";
Use quotes around the filename, just like with @use.
The filename should be a Sass partial or full Sass file without the extension.
_colors.scss, making them available to other files that @use this file.@forward "colors";
_buttons.scss, so other files can use them by @use-ing this file.@forward "buttons";
_mixins.scss for reuse.@forward "mixins";
This example shows a main Sass file forwarding variables from _colors.scss. The button style uses the forwarded $primary-color variable.
@forward "colors"; .button { background-color: $primary-color; color: white; padding: 1rem 2rem; border-radius: 0.5rem; } /* _colors.scss file content: $primary-color: #3498db; */
Forwarding is different from importing: @forward re-exports styles for others to use, while @use imports styles for the current file.
You can forward multiple files in one file to create a style library.
Use partial files (starting with underscore) for files you forward.
@forward shares styles from one Sass file to another.
It helps organize and reuse styles without repeating imports.
Use it to create clean, maintainable style libraries.
Practice
@forward directive in Sass?Solution
Step 1: Understand the role of
The@forward@forwarddirective allows one Sass file to share its styles with another file, acting like a re-export.Step 2: Compare with other options
Options B, C, and D describe different Sass features: importing CSS, compiling Sass, and defining variables, which are not the main purpose of@forward.Final Answer:
To share styles from one Sass file to another without importing repeatedly -> Option AQuick Check:
@forwardshares styles = A [OK]
- Confusing @forward with @import
- Thinking @forward compiles Sass
- Assuming @forward defines variables
_buttons.scss from _index.scss?Solution
Step 1: Identify correct syntax for @forward
The@forwarddirective uses the file name without underscore and extension, so@forward "buttons";is correct.Step 2: Eliminate other options
@forward "_buttons.scss"; incorrectly includes underscore and extension. @import "buttons"; uses@import, and @use "buttons"; uses@use, both different directives.Final Answer:
@forward "buttons"; -> Option CQuick Check:
Forward syntax excludes underscore and extension = D [OK]
- Including underscore or .scss extension in @forward
- Confusing @forward with @import or @use
- Using wrong quotes or missing semicolon
// _colors.scss
$primary: blue;
// _index.scss
@forward "colors";
// main.scss
@use "index";
.button {
color: index.$primary;
}What color will the button text be when compiled to CSS?
Solution
Step 1: Understand @forward and @use interaction
The@forward "colors";in_index.scssre-exports mixins, functions, and styles fromcolors, but NOT variables. Variables are never forwarded.Step 2: Check variable access
index.$primaryis undefined because variables aren't forwarded. Compilation fails with an undefined variable error.Final Answer:
undefined variable error -> Option AQuick Check:
@forward does NOT forward variables = A [OK]
- Thinking @forward forwards variables
- Thinking @forward imports variables directly
- Confusing variable name with string
_index.scss:@forward "buttons"; @forward "buttons";
What will happen when compiling this Sass file?
Solution
Step 1: Identify duplicate @forward usage
Using@forwardtwice for the same file causes a compilation error because Sass forbids forwarding the same module multiple times.Step 2: Understand Sass error behavior
Sass will stop compiling and show an error about duplicate forwarding, so no styles are output.Final Answer:
Compilation error due to duplicate @forward -> Option BQuick Check:
Duplicate @forward causes error = B [OK]
- Assuming duplicate forwards are ignored
- Expecting warnings instead of errors
- Confusing @forward with multiple imports
// _colors.scss
$primary: blue;
// _buttons.scss
@forward "colors";
// _index.scss
@forward "buttons";
// main.scss
@use "index";
.button-special {
color: index.$primary;
}Why does
main.scss fail to compile with an undefined variable error for index.$primary?Solution
Step 1: Trace variable forwarding chain
The variable$primaryis defined in_colors.scss._buttons.scssforwardscolorsbut does not re-forward$primaryexplicitly to_index.scss.Step 2: Understand forwarding visibility
Since_buttons.scssforwardscolors, but_index.scssonly forwardsbuttons,$primaryis not visible tomain.scssviaindexnamespace.Final Answer:
Because$primaryis not forwarded from_buttons.scssexplicitly -> Option DQuick Check:
Forward chain must include variable = A [OK]
- Assuming forwarding is automatic through all layers
- Confusing @use with @forward behavior
- Thinking @forward shares all contents without explicit forwarding
Ready to prove it?
Pass the test and this pattern moves unknown
