How to Import Files Using SCSS (@import & @use)

Importing files in SCSS helps you organize your styles into smaller, manageable pieces and reuse them across your project. The older @import rule allows you to include one SCSS file into another, but it is now deprecated and not recommended for modern projects. Instead, you should use @use, which loads files with better scoping and avoids global conflicts. With @use, you can access variables, mixins, and functions using a namespace, improving clarity and maintainability. Overall, using @use makes your SCSS structure more modular, scalable, and future-proof.

Why Import SCSS Files?

Importing helps you split your styles into smaller, manageable files (like variables, mixins, components). This improves organization and maintainability.

Example Structure:

scss/
├── _variables.scss
├── _mixins.scss
├── style.scss

Using @import (Old Method)

@import allows you to include SCSS files into another file. However, it is now deprecated and not recommended for modern projects.

Example:

@import ‘variables’;
@import ‘mixins’;

body {
background: $primary-color;
}

Note: @import loads everything globally, which can cause conflicts.

Using @use (Modern Method)

@use is the recommended way. It loads files as modules and avoids global conflicts by using namespaces.

Example:

@use ‘variables’;

body {
background: variables.$primary-color;
}

Using @use as (Alias)

You can shorten namespaces using as to make your code cleaner and easier to read.

Example:

@use ‘variables’ as v;

h1 {
color: v.$primary-color;
}

Combining Multiple Files (Best Practice)

Create a main SCSS file that imports all partials using @use to keep your project structured.

Example:

@use ‘variables’;
@use ‘mixins’;
@use ‘buttons’;

.container {
@include mixins.flex-center;
}