In the previous article we looked at partials, the small underscore-prefixed files you break your styles into. This one is about the other half of that workflow: how those files get assembled back into a single stylesheet. Splitting code into pieces only helps if you have a clean way to combine them, and in SCSS that job belongs to the import system. Understanding how imports work, and how to structure them, is what turns a folder full of partials into a coherent, compilable whole.
What an Import Does
An import pulls the contents of one SCSS file into another. That is the entire mechanism, but it is what makes modular stylesheets possible. You write your variables in one file, your mixins in another, your component styles in a third, and then a single main file imports all of them. When the compiler processes that main file, it follows each import, inlines the referenced file’s contents in order, and produces one combined CSS output.
The payoff is organisation without fragmentation. Your source is split into logical pieces that are easy to navigate and maintain, but what ships to the browser is still a single, unified stylesheet. You get the developer-friendly structure on your side and the performance-friendly single file on the delivery side.
The Basic Syntax
Imports use the @import directive. When you import a partial, you leave off both the leading underscore and the file extension, because the compiler resolves those automatically:
// main.scss@import 'variables'; // pulls in _variables.scss@import 'mixins'; // pulls in _mixins.scss@import 'header'; // pulls in _header.scss@import 'footer'; // pulls in _footer.scss/* Global styles can follow the imports */body { font-family: $font-stack; background-color: $background-color;}
Notice the order matters. variables and mixins are imported first, before any rules that depend on them. The body rule at the bottom can reference $font-stack and $background-color precisely because the variables file was imported earlier in the same file. The compiler reads top to bottom, so anything a rule depends on needs to be imported above it.
It is worth flagging that @import is being phased out of Sass in favour of @use and @forward, which manage namespaces more cleanly and avoid the global-scope issues that @import carries. You will still encounter @import constantly in existing projects, and the organising principle is identical across both, but for new work @use is the forward-looking choice.
Structuring Your Imports
The most important architectural decision is to have a single entry point. One main file, conventionally main.scss, imports everything else and is the only file you compile into final CSS. Every partial flows into it. This gives you one readable index of the entire stylesheet. Glancing at the list of imports at the top of main.scss should tell anyone how the project’s styles are organised and in what order they load.
The order of imports inside that entry point should follow dependency. Foundational things that other files rely on come first, and the more specific, dependent styles come later. A reliable structure looks like this:
// main.scss// 1. Configuration and tools (no CSS output, just definitions)@import 'variables';@import 'mixins';// 2. Base styles: resets, typography, element defaults@import 'base';// 3. Layout: grid systems, containers, structural styles@import 'layout';// 4. Components: buttons, cards, forms, and the like@import 'components';
The logic of this ordering is that variables and mixins define no CSS on their own but are needed by everything below them, so they go first. Base styles establish global defaults. Layout builds the structural scaffolding. Components style the individual pieces that sit within that structure. Following dependency order both prevents errors, since a file can use anything imported above it, and produces a predictable cascade in the compiled output.
Group related styles into partials that match this structure: _variables.scss for values, _mixins.scss for reusable blocks, _base.scssfor resets and typography, _layout.scss for grid and structure, _components.scss for the discrete UI pieces. The exact division is a matter of taste and project size, but the principle is constant. Each file should have a clear, single responsibility you can name.
Avoiding the Common Pitfall
Import each partial exactly once, from the main entry point. The temptation, especially with the older @import, is to import a partial wherever it seems needed, including from inside other partials. This leads to the same file being inlined multiple times, which duplicates its rules in the output and can create genuine conflicts where styles override each other unexpectedly. A clean architecture has partials that define styles and a single main file that assembles them, with no partial importing another. Keep the imports centralised and the dependency chain stays easy to reason about.
The Takeaway
SCSS imports are the assembly step that makes a modular stylesheet work. The @import directive inlines one file into another, a single main entry point gathers all your partials, and the order of imports should follow dependency, with definitions first and specific components last. Compiled, it all collapses into one CSS file, giving you organised source and efficient delivery at the same time. As with partials, the directive itself is shifting from @import toward @use and @forward, but the discipline that makes imports valuable, a single entry point, logical grouping, and no redundant imports, carries over unchanged.
See you soon.
[…] Imports: https://datalad.co.uk/assembling-a-stylesheet-from-many-files-with-scss-imports/ […]
[…] then bring these into a main stylesheet. Historically this was done with the @import directive, and you reference each partial without the leading underscore and without the file […]