CSS Modules
Locally scoped styles with auto-generated class names.
Global CSS
Apply styles across every route from a single file.
External stylesheets
Import CSS from npm packages directly in the App Router.
Tailwind CSS
Utility-first CSS framework built for Next.js.
CSS Modules
CSS Modules locally scope CSS by generating unique class names. This lets you reuse the same class name across different files without naming collisions. Create a file with the.module.css extension and import it into any component inside the app directory:
CSS Module files must end in
.module.css. Regular .css files are treated as global CSS and will not produce unique class names.Global CSS
Global CSS applies styles across your entire application. Create aglobals.css file and import it in your root layout to apply the styles to every route:
External stylesheets
Stylesheets published by external packages can be imported anywhere in theapp directory, including inside colocated components:
CSS ordering and merging
Next.js optimizes CSS during production builds by automatically chunking (merging) stylesheets. The order of your CSS depends on the order you import styles in your code. For example,base-button.module.css will be ordered before page.module.css since <BaseButton> is imported first:
Recommendations
To keep CSS ordering predictable:- Contain CSS imports to a single JavaScript or TypeScript entry file where possible.
- Import global styles and Tailwind stylesheets at the root of your application.
- Use Tailwind CSS for most styling needs.
- Use CSS Modules for component-specific styles when Tailwind utilities aren’t sufficient.
- Use a consistent naming convention:
<name>.module.cssrather than<name>.tsx. - Extract shared styles into shared components to avoid duplicate imports.
- Disable linters or formatters that auto-sort imports, such as ESLint’s
sort-importsrule. - Use the
cssChunkingoption innext.config.jsto control how CSS is chunked.
Development vs production
| Environment | Behavior |
|---|---|
next dev | CSS updates apply instantly via Fast Refresh |
next build | All CSS is concatenated into minified, code-split .css files |
CSS ordering can behave differently in development. Always verify the final CSS order by checking a production build with
next build.