08 April 2023

Understanding the Differences between @use and @forward in SASS

sasssasscss

This post is updated at 2023/04/10

SASS is a popular CSS preprocessor that has gained significant popularity among front-end developers in recent years. It offers a variety of features and functionalities that help streamline the development process, and two of its most popular features are @use and @forward.

In this post, we will explore the differences between @use and @forward, their use cases, and how they can help make your SASS code more organized and efficient.

@use Directive

@use is a relatively new addition to the SASS language that provides a way to import variables, mixins, functions, and even entire modules from other files. Here's how it works:

1// _variables.scss 2$primary-color: #007bff; 3$secondary-color: #6c757d; 4 5// styles.scss 6@use 'variables'; 7 8body { 9 background-color: variables.$primary-color; 10 color: variables.$secondary-color; 11} 12

In this example, we create a new file called _variables.scss and define two variables in it: $primary-color and $secondary-color. We then import these variables into our styles.scss file using the @use directive.

One important thing to note is that when using @use, you must specify the namespace for the imported variables, mixins, or functions. In our example, we use variables.$primary-color and variables.$secondary-color to access the imported variables.

@forward Directive

@forward is another SASS directive that allows you to share variables, mixins, functions, and modules between SASS files, but in a slightly different way than @use.

1// _variables.scss 2$primary-color: #007bff; 3$secondary-color: #6c757d; 4 5// _mixins.scss 6@mixin button { 7 display: inline-block; 8 padding: 0.5rem 1rem; 9 font-size: 1rem; 10 font-weight: 500; 11 text-align: center; 12 color: #fff; 13 background-color: $primary-color; 14 border-radius: 0.25rem; 15} 16 17// styles.scss 18@forward 'variables'; 19@use 'mixins'; 20 21.button { 22 @include mixins.button; 23} 24

In this example, we create two separate files, _variables.scss and _mixins.scss, each with their own set of variables and mixins. We then import the variables from _variables.scss into styles.scss using @forward.

Unlike @use, @forward allows you to forward the entire module or a subset of its content to another SASS file, without requiring you to specify a namespace for each variable or mixin you want to use.

In addition, @forward also allows you to import content from one file and export it from another, making it a powerful tool for sharing and reusing code across multiple files.

Conclusion

In conclusion, @use and @forward are both powerful tools that can help you organize your SASS code and make it more efficient.

Use @use when you want to import variables, mixins, or functions from another file, and use @forward when you want to share entire modules or a subset of their content between SASS files.

By understanding the differences between these two directives, you can take advantage of the full range of functionality that SASS has to offer, and write cleaner, more maintainable code.

Let's Talk

My inbox is always open. Whether you have a question or just want to say hi, I’ll try my best to get back to you! Also you can find me on Github, Linkedin, Twitter and Telegram.