Skip to main content
Next.js supports several approaches to styling. You can use them individually or in combination.

CSS Modules

CSS Modules scope CSS locally by automatically generating unique class names. Create a file with the .module.css extension:
components/Button.module.css
Import it as an object:
components/Button.tsx
The generated class name is unique (e.g., Button_button__abc12), so there are no naming conflicts between components.

Global CSS

Import global CSS files in pages/_app.tsx. They apply to every page in your application.
styles/globals.css
pages/_app.tsx
Global CSS can only be imported inside pages/_app.tsx. Importing global CSS anywhere else will cause an error.

styled-jsx

styled-jsx is built into Next.js. It provides scoped CSS within a component using a <style jsx> tag:
pages/index.tsx
The styles are scoped to the component—they do not leak to children or other components.

Global styles with styled-jsx

Add the global attribute to apply styles globally from within a component:
The App Router does not include styled-jsx by default. CSS Modules and Tailwind CSS are the recommended styling options for new projects.

Sass

Next.js has built-in support for Sass (.scss and .sass files). Install the sass package first:
Then use .module.scss for scoped styles or import .scss files globally in _app.tsx:

Configuring Sass options

You can pass options to the Sass compiler in next.config.js:
next.config.js

CSS-in-JS

CSS-in-JS libraries that require server-side rendering (such as styled-components or Emotion) need additional setup for the Pages Router. They require customizing pages/_document.tsx to inject styles on the server.

styled-components

Install styled-components:
Create a .babelrc to enable the plugin:
.babelrc
Customize _document.tsx to collect and inject styles during SSR:
pages/_document.tsx
Now you can use styled-components throughout your pages:
components/Button.tsx

Emotion

Emotion requires a similar _document.tsx customization. Refer to the Emotion SSR documentation for the exact setup.
CSS-in-JS libraries that require renderPage customization in _document.tsx will not work with the App Router without additional changes. CSS Modules or Tailwind CSS are preferred for App Router projects.

Tailwind CSS

To use Tailwind CSS in a Pages Router project, install it and configure PostCSS:
Update tailwind.config.js to include your files:
tailwind.config.js
Add the Tailwind directives to your global CSS file and import it in _app.tsx:
styles/globals.css