Sass Partials & @use/@forward
Real projects split styles into many small files. Sass's module system — @use and @forward — loads those files, keeps their names in separate namespaces, and guarantees each file's CSS is output only once.
Partials
A partial is a Sass file whose name starts with an underscore, like _colors.scss. The underscore tells the compiler "don't turn this into its own CSS file — it is only meant to be loaded by other files". When you load it, you leave out the underscore and the extension.
scss/
_colors.scss ← partial
_buttons.scss ← partial
main.scss ← compiled to main.css@use
@use loads another module. Its variables, mixins and functions become available through a namespace — by default the last part of the file name.
// _colors.scss
@use "sass:color";
$brand: #cc6699;
$ink: #2d2a32;
@function tint($amount) {
@return color.mix(white, $brand, $amount);
}// main.scss
@use "colors";
.hero {
background: colors.$brand;
color: colors.$ink;
}Rules about @use:
@usemust come before any other rules (only@forwardand variable declarations may precede it).- A module is evaluated once, no matter how many files
@useit — its CSS is never duplicated. - Members are only visible in the file that
@uses them. There are no global leaks between files. - Members whose names start with
-or_are private to their module.
Changing the namespace
@use "colors" as c; // c.$brand
@use "colors" as *; // $brand, no namespace (use carefully)Built-in modules
Sass ships with built-in modules that you load the same way. The playground is a single file, but built-in modules work in it:
@use "sass:math";
@use "sass:color";
$brand: #cc6699;
.box {
width: math.div(100%, 3);
padding: math.round(13.6px);
background: color.adjust($brand, $lightness: 20%);
border: 2px solid $brand;
}.box {
width: 33.3333333333%;
padding: 14px;
background: rgb(90%, 70%, 80%);
border: 2px solid #cc6699;
}
| Module | What it contains |
|---|---|
sass:math |
div, round, floor, ceil, abs, min, max, pow, sqrt, percentage, $pi … |
sass:color |
adjust, scale, change, mix, channel, complement, grayscale … |
sass:string |
index, slice, insert, to-upper-case, unique-id … |
sass:list |
nth, length, append, join, index, separator … |
sass:map |
get, merge, keys, values, has-key, deep-merge … |
sass:selector |
nest, append, unify, parse … |
sass:meta |
type-of, inspect, load-css, module-variables … |
Configuring a module with with
If a module declares variables with !default, the loading file can override them once:
// _theme.scss
$primary: blue !default;
$radius: 4px !default;
.btn { background: $primary; border-radius: $radius; }// main.scss
@use "theme" with ($primary: #cc6699, $radius: 999px);Here's the same idea inside one file. Built-in modules can't be configured, but we can show how !default lets a value be supplied from outside:
// Imagine this first line comes from "@use 'theme' with (...)"
$primary: #cc6699;
// _theme.scss
$primary: blue !default;
$radius: 4px !default;
.btn {
background: $primary;
border-radius: $radius;
}.btn {
background: #cc6699;
border-radius: 4px;
}
@forward
@forward makes another module's members available to whoever loads this file, without using them here. It's how you build an "index" file that re-exports a library:
// abstracts/_index.scss
@forward "colors";
@forward "spacing";
@forward "mixins" show button-reset, focus-ring; // only these
@forward "typography" as type-*; // prefixed: type-$base// main.scss
@use "abstracts"; // loads abstracts/_index.scss
.card { padding: abstracts.$space-md; color: abstracts.$brand; }A folder with an _index.scss can be loaded by its folder name.
Goodbye @import
Older Sass code uses @import. It dumped everything into one global namespace and re-emitted CSS every time a file was imported. @import is deprecated and will be removed in Dart Sass 3.0. Migrate with the official tool: npx sass-migrator module --migrate-deps main.scss.
@import (old) |
@use (modern) |
|---|---|
| Global namespace, name collisions | Namespaced members (colors.$brand) |
| CSS output duplicated per import | Each module output once |
| No privacy | -private members |
| Configure by setting globals first | @use "x" with (...) |
Test yourself
What does the leading underscore in _buttons.scss mean?
- The file is private and cannot be loaded
- It is a partial that is not compiled to its own CSS file
- It must be loaded with
@import - It contains only mixins
After @use "colors";, how do you access $brand from that file?
- $brand
- colors.$brand
- $colors.brand
- colors-$brand
Which rule re-exports a module's members to files that load the current file?
- @use
- @import
- @forward
- @extend