📖 Scenario: You are organizing styles for a website. You want to keep your Sass files neat by grouping related styles into separate files and then combining them into one main file. This helps you manage colors and buttons easily.
🎯 Goal: Create multiple Sass partial files for colors and buttons, then use the @forward directive in an index file to re-export these styles. Finally, import the index file in the main Sass file to apply all styles.
📋 What You'll Learn
Create a partial Sass file named _colors.scss with two color variables.
Create a partial Sass file named _buttons.scss with a button style using a color variable.
Create an _index.scss file that uses @forward to re-export _colors.scss and _buttons.scss.
Create a main Sass file styles.scss that imports _index.scss.
The final CSS should style a button with the defined colors.
💡 Why This Matters
🌍 Real World
Organizing Sass files in a large project to keep styles modular and maintainable.
💼 Career
Front-end developers often use <code>@forward</code> to manage style libraries and share variables and mixins across components.
Progress0 / 4 steps
1
Create _colors.scss with color variables
Create a partial Sass file named _colors.scss and define two color variables: $primary-color: #3498db; and $secondary-color: #2ecc71;.
SASS
Hint
Use the syntax $variable-name: value; to create variables in Sass.
2
Create _buttons.scss with a button style using $primary-color
Create a partial Sass file named _buttons.scss, import colors using @use "colors" as *;, and write a style for .btn that sets background-color to $primary-color and color to white.
SASS
Hint
Use @use "colors" as *;.btn { background-color: $primary-color; color: white; } to import variables and style the button.
3
Create _index.scss to re-export _colors.scss and _buttons.scss using @forward
Create a partial Sass file named _index.scss. Use the @forward directive to re-export _colors.scss and _buttons.scss.
SASS
Hint
Use @forward "filename"; to re-export partials without the underscore or extension.
4
Create styles.scss to import _index.scss
Create a Sass file named styles.scss and import _index.scss using @use. This will apply all styles from colors and buttons.
SASS
Hint
Use @use "index"; to import the re-exported styles.
Practice
(1/5)
1. What is the main purpose of the @forward directive in Sass?
easy
A. To share styles from one Sass file to another without importing repeatedly
B. To import CSS files directly into Sass
C. To compile Sass files into CSS
D. To define variables inside a Sass file
Solution
Step 1: Understand the role of @forward
The @forward directive 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 A
Quick Check:
@forward shares styles = A [OK]
Hint: Remember: @forward re-exports styles, not imports or compiles [OK]
Common Mistakes:
Confusing @forward with @import
Thinking @forward compiles Sass
Assuming @forward defines variables
2. Which of the following is the correct syntax to forward a Sass file named _buttons.scss from _index.scss?
easy
A. @use "buttons";
B. @forward "_buttons.scss";
C. @forward "buttons";
D. @import "buttons";
Solution
Step 1: Identify correct syntax for @forward
The @forward directive 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 C
Quick Check:
Forward syntax excludes underscore and extension = D [OK]
Hint: Use file name without underscore or extension in @forward [OK]
Common Mistakes:
Including underscore or .scss extension in @forward