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
- CSS imported in the root layout (usually
globals.css). - CSS imported in nested layouts, from outer to inner.
- CSS imported by the page.
- 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.cssChunkingcontrols how CSS files are merged into chunks (trueby 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
1// app/layout.tsx2import "./globals.css"; // 1st: resets, tokens (@layer base)3import "@acme/ui/styles.css"; // 2nd: library styles45export default function RootLayout({ children }: { children: React.ReactNode }) {6 return <html lang="en"><body>{children}</body></html>;7}89// app/shop/layout.tsx10// import "./shop.css"; // 3rd: section styles1112// app/shop/page.tsx13// import styles from "./page.module.css"; // 4th: page styles override earlier rules1415/* 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 */1920// 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.