TExcelTAD
Home › Sass › Sass Partials & @use/@forward

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.

scss
// _colors.scss
@use "sass:color";

$brand: #cc6699;
$ink: #2d2a32;

@function tint($amount) {
  @return color.mix(white, $brand, $amount);
}
scss
// main.scss
@use "colors";

.hero {
  background: colors.$brand;
  color: colors.$ink;
}

Rules about @use:

  • @use must come before any other rules (only @forward and variable declarations may precede it).
  • A module is evaluated once, no matter how many files @use it — 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

scss
@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:

scss · try it yourself
@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:

scss
// _theme.scss
$primary: blue !default;
$radius: 4px !default;
.btn { background: $primary; border-radius: $radius; }
scss
// 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:

scss · try it yourself
// 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:

scss
// abstracts/_index.scss
@forward "colors";
@forward "spacing";
@forward "mixins" show button-reset, focus-ring;   // only these
@forward "typography" as type-*;                   // prefixed: type-$base
scss
// 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?

  1. The file is private and cannot be loaded
  2. It is a partial that is not compiled to its own CSS file
  3. It must be loaded with @import
  4. It contains only mixins

After @use "colors";, how do you access $brand from that file?

  1. $brand
  2. colors.$brand
  3. $colors.brand
  4. colors-$brand

Which rule re-exports a module's members to files that load the current file?

  1. @use
  2. @import
  3. @forward
  4. @extend