Prerequisites

  • Next.js 12+

Sass installed:

npm install sass

Scope 1: Setup Global SCSS

πŸ“ File Structure

/styles  └── globals.scss

  └── variables.scss

πŸ”— Import in _app.tsx

// pages/_app.tsximport ‘../styles/globals.scss’;

 

export default function App({ Component, pageProps }) {

Β Β return <Component {…pageProps} />;

}

βœ… Best For: Reset styles, typography, body-level defaults.

Scope 2: Use SCSS Modules Per Component

🎯 Why Use This?

Avoid style leakage, keep styles local to components.

πŸ›  Example

/components/  └── Button.module.scss

 

// components/Button.tsximport styles from ‘./Button.module.scss’;

 

export default function Button() {

Β Β return <button className={styles.primary}>Click me</button>;

}

βœ… Best For: Encapsulated, reusable components.

🧩 Scope 3: Share Variables & Mixins Across Files

πŸ›  Example

// styles/_variables.scss$primary-color: #0070f3;

πŸ›  Use Anywhere

// styles/globals.scss or Button.module.scss@import ‘./variables’;

 

.button {

Β Β color: $primary-color;

}

🚨 Important: @import is deprecated in Sass. Migrate to @use or @forward for modern workflows.

Also Read:

πŸš€ Optional: Auto-Import SCSS Variables Everywhere

You can avoid manually importing _variables.scss in every file.

βœ… With sass-resources-loader (needs Webpack override):

1. Install dependencies:

npm install sass-resources-loader

2. Customize Webpack:

// next.config.jsconst path = require(‘path’);

 

module.exports = {

Β Β webpack(config) {

Β Β Β Β config.module.rules.push({

Β Β Β Β Β Β test: /\.scss$/,

Β Β Β Β Β Β use: [

Β Β Β Β Β Β Β Β {

Β Β Β Β Β Β Β Β Β Β loader: ‘sass-resources-loader’,

Β Β Β Β Β Β Β Β Β Β options: {

Β Β Β Β Β Β Β Β Β Β Β Β resources: path.resolve(__dirname, ‘styles/variables.scss’),

Β Β Β Β Β Β Β Β Β Β },

Β Β Β Β Β Β Β Β },

Β Β Β Β Β Β ],

Β Β Β Β });

Β Β Β Β return config;

Β Β },

};

βœ… Tips

  • Use globals.scss for app-wide layout or reset.
  • Use .module.scss for isolated styling.
  • Extract variables/mixins into shared SCSS files and import where needed.
  • For larger teams, automate variable imports using sass-resources-loader or adopt @use/@forward syntax.