Skip to content
Styling & OptimizationChapter 25 / 34Beginner5 min read

Styling: CSS Modules & Tailwind

Set up Next.js Tailwind CSS and CSS Modules, learn global CSS, and see how Tailwind v4 differs from scoped styles, with App Router examples.

On this page

1. What is styling?

Next.js supports several approaches: Global CSS, CSS Modules, Tailwind CSS, Sass and CSS-in-JS. In this chapter we look at the two most common ones.

  • Tailwind CSS v4: small utility classes like flex, p-4 and text-xl written directly in className.
  • CSS Modules: a .module.css file whose classes are scoped to the component that imports it.

2. Why does it matter?

With plain global CSS, every class is shared across the whole app. In a large app, if the name .button is used in two places, the styles clash and the bug is hard to find.

Global CSS onlyTailwind + CSS Modules
Every class is global across the appClasses are scoped to a component or are utility based
Name collisions are commonCSS Modules generate unique class names
Unused CSS stays in the bundleTailwind only generates the classes you use
Design values differ from file to fileTailwind theme keeps spacing and colors consistent

3. Where is it used?

SituationBetter choice
Layout, spacing, colors, responsive designTailwind utility classes
Complex animation or very specific CSSCSS Module
Reset, fonts, CSS variables, base stylesapp/globals.css (Global CSS)
CSS from a third-party libraryImport in the root layout

This is also the Next.js team's recommendation: do most of your styling with Tailwind, and use CSS Modules where utilities are not enough.

4. How does it work?

Tailwind v4 runs as a PostCSS plugin. You import Tailwind in globals.css, and at build time Tailwind scans your files and generates only the classes you actually used.

Tailwind v4 flow
@tailwindcss/postcss in postcss.config.mjs
@import "tailwindcss" in globals.css
Root layout imports globals.css
The build scans which classes are used
A small, minified CSS file goes to the browser

In CSS Modules, a .card class becomes something like page_card__x7a2b after the build. That is why a class with the same name in two different files never clashes.

5. Code example

Tailwind CSS v4 setup

bash
npm install -D tailwindcss @tailwindcss/postcss
js
export default {
  plugins: {
    "@tailwindcss/postcss": {},
  },
};

The code above goes in postcss.config.mjs. Now in app/globals.css:

css
@import "tailwindcss";

@theme {
  --color-brand: #0f766e;
}
tsx
import "./globals.css";

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body className="bg-white text-slate-900">{children}</body>
    </html>
  );
}

Now you can use utility classes in any component:

tsx
export default function Hero() {
  return (
    <section className="mx-auto max-w-3xl px-4 py-16 text-center">
      <h1 className="text-4xl font-bold text-brand md:text-5xl">ZnPedia</h1>
    </section>
  );
}

CSS Module example

css
.card {
  border-radius: 12px;
  padding: 24px;
  transition: transform 200ms ease;
}

.card:hover {
  transform: translateY(-4px);
}
tsx
import styles from "./card.module.css";

export function Card({ title }: { title: string }) {
  return <article className={styles.card}>{title}</article>;
}

How does the code work?

ItemMeaning
@tailwindcss/postcssThe PostCSS plugin for Tailwind v4, no separate tailwind.config.js needed
@import "tailwindcss"Loads Tailwind's base, components and utilities
@themeDesign tokens inside CSS itself, --color-brand creates the text-brand class
card.module.cssThe .module.css extension is what makes the file a CSS Module
styles.cardA reference to the generated unique class name

6. Common mistakes

Mistake: Copying a Tailwind v3 setup into v4

Why? In v4, the @tailwind base, components, utilities directives and the old tailwindcss PostCSS plugin do not work, so the styles never load.

Better: Install the @tailwindcss/postcss package and write only @import tailwindcss in globals.css.

Mistake: Importing global CSS in every component

Why? Global styles are not removed on navigation, so the CSS of different pages can clash with each other.

Better: Import global CSS only in the root layout, and use a CSS Module or Tailwind for component-specific styles.

Mistake: Building dynamic class names by joining strings

Why? Tailwind scans files as plain text, so a class built from bg- plus a variable is invisible to it and the CSS is never generated.

Better: Write full class names, for example bg-teal-600 if isActive, otherwise bg-slate-200, so the scanner can find them.

Mistake: Accessing a kebab-case class with dot syntax in a CSS Module

Why? styles.my-card becomes a minus operator in JavaScript and you get undefined.

Better: Name the class in camelCase like myCard, or use bracket syntax.

Interview tip

If you are asked "How do CSS Modules prevent naming collisions?", the answer is: at build time every class gets a unique name based on the file path and a hash, and the imported object maps the original name to the generated name.

7. What to learn before and after

Final quick revision

PointDetail
Tailwind v4 installtailwindcss and @tailwindcss/postcss
CSS entry@import "tailwindcss" in globals.css
Theme tokens@theme block inside CSS
Scoped CSS.module.css file and styles.className
Global CSSImport only in the root layout