Prerequisites
- Next.js 12+
Sass installed:
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:
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.