CSS Modules and Tailwind cover most projects, but teams arrive at Next.js with existing Sass code, styled-components habits or a preferred component library. Each works in the App Router with a few adjustments. After this lesson you will be able to set up Sass, make a runtime CSS-in-JS library stream its styles correctly, pick a zero-runtime alternative when appropriate, and integrate a component library.
Next.js supports Sass out of the box once the compiler is installed:
npm install --save-dev sassBoth .scss and .sass syntaxes work, and the module convention is the same as for CSS Modules:
// components/Card.module.scss
@use "sass:color";
$brand: #1d4ed8;
.card {
padding: 1rem;
border: 1px solid color.adjust($brand, $lightness: 40%);
&:hover { border-color: $brand; }
}import styles from "./Card.module.scss";
export function Card({ children }: { children: React.ReactNode }) {
return <div className={styles.card}>{children}</div>;
}Sass Modules work in Server and Client Components alike because the output is plain CSS. Shared variables and mixins can be made available everywhere through sassOptions in next.config.ts, for example sassOptions: { includePaths: ["./src/styles"] } so that @use "variables" resolves without relative paths.
Libraries such as styled-components and Emotion generate styles in JavaScript at render time. That model has two consequences in the App Router:
The second point is solved by a style registry, shown in the sample at the top of this lesson. useServerInsertedHTML from next/navigation inserts the collected <style> tags into the streamed response at the right moment. Wrap the root layout's children in the registry and enable the SWC transform for readable class names:
// next.config.ts
import type { NextConfig } from "next";
const nextConfig: NextConfig = {
compiler: { styledComponents: true },
};
export default nextConfig;Emotion, MUI and Chakra follow the same registry pattern. Because every styled element forces a client boundary, keep such components at the leaves of the tree.
If you like the co-located, typed authoring style of CSS-in-JS but want Server Component compatibility, choose a library that compiles styles away at build time:
| Option | How it works | Server Components | |---|---|---| | Tailwind CSS | Utility classes scanned at build | Yes | | CSS Modules / Sass Modules | Scoped class names | Yes | | vanilla-extract | TypeScript files compiled to static CSS | Yes | | Panda CSS | Style objects extracted at build | Yes | | styled-components / Emotion | Runtime style injection | Client only |
Libraries fall into two groups. Copy-in libraries such as shadcn/ui generate source files into your project (npx shadcn@latest init, then npx shadcn@latest add button); they use Tailwind and Radix primitives, so they work with Server Components except where a component is interactive. Package libraries such as MUI or Ant Design ship runtime CSS-in-JS and require the registry approach above plus their documented Next.js integration package.
"use client", wrap it once in your own file rather than marking every consumer.What must a styled-components setup add to work with streaming in the App Router?
sass and use .module.scss files exactly like CSS Modules; they work in any component.useServerInsertedHTML for streaming.compiler.styledComponents in next.config.ts for the SWC transform.Next lesson: Image and Font Optimization ā serve responsive images and self-hosted fonts with zero layout shift.