What is the order of CSS precedence in Next.js?

CSS precedence follows normal cascade rules: specificity first, then source order. In Next.js, source order is determined by import order: stylesheets imported earlier are inserted earlier, and later imports win at equal specificity. Layouts load before their pages, so page and component styles can override layout and global styles.

How Next.js orders CSS

  1. CSS imported in the root layout (usually globals.css).
  2. CSS imported in nested layouts, from outer to inner.
  3. CSS imported by the page.
  4. CSS imported by components, in the order they are imported within each file.

In production, CSS is bundled into chunks per route, keeping this relative order. In development, styles are injected as <style> tags and hot-reloaded, so order can differ slightly; always verify with next build && next start.

Making order predictable

  • Import global styles first in the root layout, before any component imports.
  • Keep global CSS small and component CSS scoped with CSS Modules or Tailwind.
  • Avoid importing the same CSS file from many places with different neighbors.
  • Use cascade layers (@layer reset, base, components, utilities;) to make precedence explicit regardless of load order. Tailwind v4 relies on layers.
  • experimental.cssChunking controls how CSS files are merged into chunks (true by default, 'strict' keeps import order exact at the cost of more files).

Specificity still wins

A class in a CSS Module does not beat an ID selector in global CSS. !important overrides everything except other !important rules with higher precedence; avoid it.

Debugging

Use the browser DevTools "Styles" panel to see which rule wins and which file it came from.

Code example

tsx
1// app/layout.tsx
2import "./globals.css"; // 1st: resets, tokens (@layer base)
3import "@acme/ui/styles.css"; // 2nd: library styles
4
5export default function RootLayout({ children }: { children: React.ReactNode }) {
6 return <html lang="en"><body>{children}</body></html>;
7}
8
9// app/shop/layout.tsx
10// import "./shop.css"; // 3rd: section styles
11
12// app/shop/page.tsx
13// import styles from "./page.module.css"; // 4th: page styles override earlier rules
14
15/* app/globals.css - explicit cascade layers */
16// @layer reset, base, components, utilities;
17// @layer base { button { border-radius: 4px; } }
18// @layer components { .btn { border-radius: 9999px; } } /* wins over base regardless of file order */
19
20// next.config.ts: experimental: { cssChunking: "strict" } // keep exact import order

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.

Open in study app