Skip to content

Sass Partials

As a stylesheet grows, keeping everything in one file becomes unmanageable. This lesson explains Sass partials, files meant to be loaded into other files rather than compiled on their own, and how to organize a project around them.

What Is a Sass Partial?

A partial is a Sass file whose name starts with an underscore, such as _variables.scss or _buttons.scss. The leading underscore tells the Sass compiler not to generate a separate CSS file for it; it exists only to be loaded into other files.

Partials are the foundation of any organized Sass architecture: instead of one giant stylesheet, you split styles into small, focused files, colors, typography, buttons, forms, and combine them through an entry file.

// _buttons.scss (a partial, never compiled directly)
.btn {
  padding: 0.6rem 1.2rem;
  border-radius: 6px;
}

// main.scss (the entry file)
@use 'buttons';

Running the compiler on main.scss produces main.css; _buttons.scss is never compiled into its own _buttons.css file.

Partial Naming and Loading

_filename.scss        // the partial file itself
@use 'filename';      // loaded without the underscore or extension
  • Partial filenames start with an underscore: _variables.scss.
  • When loading a partial with @use or @forward, omit both the underscore and the file extension.
  • Only entry files (files without a leading underscore) should be passed directly to the Sass compiler.
  • Most build tools automatically skip partials when scanning a folder for entry files.

Sass Partials Cheatsheet

Naming and loading patterns for organizing a project with partials.

File Purpose Loaded With
_variables.scss Colors, spacing, typography tokens @use 'variables';
_mixins.scss Reusable mixins @use 'mixins';
_functions.scss Custom Sass functions @use 'functions';
_reset.scss Base/reset styles @use 'reset';
_buttons.scss Button component styles @use 'buttons';
_index.scss Folder barrel file (forwards siblings) @use 'components';
main.scss Entry file, actually compiled Passed directly to the compiler

Why Use Partials

Partials keep related styles together in small, readable files instead of one massive stylesheet. They also prevent the compiler from generating dozens of unwanted, unused CSS files, one per component, if every file were treated as an entry point.

  • Easier to navigate: a _buttons.scss file only contains button-related styles.
  • Easier to review in pull requests, since changes are scoped to a specific concern.
  • Prevents accidental duplicate CSS output from files that were never meant to compile alone.

Organizing Partials in Folders

Partials are commonly grouped into folders by responsibility: abstracts/ for variables and functions, base/ for resets, components/ for individual UI pieces, and so on. This structure is the foundation of the 7-1 pattern covered later in this course.

styles/
  abstracts/
    _variables.scss
    _mixins.scss
  base/
    _reset.scss
    _typography.scss
  components/
    _buttons.scss
    _cards.scss
  main.scss

Index Partials as Barrel Files

A common pattern is an _index.scss partial inside a folder that forwards every sibling file, so the rest of the project can load the whole folder with a single @use or @forward statement.

// components/_index.scss
@forward 'buttons';
@forward 'cards';

// main.scss
@use 'components';

Sass automatically looks for _index.scss (or _index.sass) when you @use a folder path.

Common Mistakes

  • Forgetting the leading underscore, causing the compiler to generate an unwanted standalone CSS file for what should be a partial.
  • Including the underscore or file extension when loading a partial with @use, which is unnecessary and unsupported.
  • Putting unrelated styles into one giant partial instead of splitting by responsibility.
  • Not using an _index.scss barrel file for folders with many related partials, forcing every consumer to load each file individually.

Key Takeaways

  • Partial files start with an underscore and are never compiled into their own CSS output.
  • Load a partial with @use 'name';, omitting both the underscore and the file extension.
  • Group related partials into folders (variables, mixins, components) for a scalable structure.
  • An _index.scss barrel file can forward an entire folder through a single @use statement.

Pro Tip

Name every reusable Sass file as a partial (with a leading underscore) unless it is specifically meant to be an entry point compiled directly, this avoids accidental duplicate CSS output as your project grows.