3 Topics
Chapter•100 Minutes

Sass Integration with React and Next.js

Integrate SCSS into React applications, configure Next.js Sass support, use CSS Modules, manage global styles, and establish scalable component styling practices.

Using Sass in React Applications

React applications can use Sass to organize global styles, shared design tokens, and component-specific stylesheets. A typical setup installs the sass package and imports an SCSS entry point from the application's JavaScript entry point or root component.

Global styles commonly include resets, typography, theme tokens, and broad layout conventions. Component styles should generally remain focused on the component they describe.

React does not require Sass. It is a stylesheet compilation choice that works alongside the framework's component model. A project can use global SCSS, Sass modules, CSS Modules, or a combination when boundaries are clear.

Import Global SCSS into React
// src/main.jsx
import React from "react";
import { createRoot } from "react-dom/client";
import App from "./App";
import "./styles/main.scss";

createRoot(document.getElementById("root")).render(
  <React.StrictMode>
    <App />
  </React.StrictMode>
);
💡 The stylesheet import asks the configured build tool to process the SCSS file. In a typical Vite-based React project, installing sass enables SCSS imports without a separate manual compilation command. The exact entry-point filename may differ between projects. Keep global styles centralized and avoid importing the same global stylesheet redundantly across many components.
Key Takeaways
  • Install Sass as a project dependency.
  • Import global SCSS from the application's root entry point.
  • Keep global styles and component-specific styles separate.
  • React does not execute Sass; the build pipeline compiles it.
Knowledge Check

What does importing an SCSS file in a typical configured React build pipeline do?

SCSS Modules and Component Style Isolation

CSS Modules scope class names locally to a module and expose generated class-name mappings to JavaScript. SCSS Modules combine this isolation model with Sass preprocessing.

A component can import a file such as Button.module.scss and reference the generated class names through the imported object. This reduces accidental global class-name collisions and makes component ownership clearer.

Global resets and theme tokens may still be shared through deliberate global styles or Sass modules. Keep global side effects out of ordinary component modules when possible.

CSS Modules and Sass modules are different concepts: Sass modules organize Sass code and its public members, while CSS Modules provide locally scoped CSS class names through a build-tool integration.

Button.module.scss
.button {
  padding: 0.75rem 1rem;
  border: 0;
  border-radius: 0.5rem;
  cursor: pointer;
}

.primary {
  color: white;
  background: #2457c5;
}

.danger {
  color: white;
  background: #c62828;
}
💡 The Sass compiler processes this stylesheet, and the CSS Modules integration creates locally scoped class names. The JavaScript import exposes those generated names through the styles object. This approach is useful when a component should own its styling without relying on globally unique hand-written class names.
Knowledge Check

What is the primary purpose of a CSS Module?

Using Sass in Next.js

Next.js supports Sass through the sass package. After installing it, you can import SCSS stylesheets using the conventions supported by your Next.js router and version.

With the Pages Router, global stylesheets are typically imported through pages/_app.js or pages/_app.tsx. Component-specific Sass Modules use filenames such as Button.module.scss.

With the App Router, global styles can be imported from the root layout or appropriate application entry points, while component modules can be imported by components that use them. Server and Client Component boundaries still apply to JavaScript behavior, but ordinary stylesheet imports are handled by the framework's build pipeline.

Do not assume that every CSS feature, import location, or build customization behaves identically across Next.js versions. Follow the documentation for the router and version used by the project.

Specifications & Requirements
  • Package:sass
  • Pages Router global styles:Typically pages/_app.js or pages/_app.tsx
  • App Router global styles:Typically the root layout or an appropriate application entry point
  • Component isolation:*.module.scss
Install Sass in Next.js
npm install sass
💡 Install Sass in the Next.js project. The framework can then process supported .scss and .module.scss files through its build pipeline. For a Pages Router project, global SCSS can be imported from pages/_app.js. For component isolation, use .module.scss files. No separate Sass watcher is normally required when Next.js is already managing the development build.
Knowledge Check

Which filename convention is used for a component-specific Sass Module in Next.js?