CSS
Style your Next.js Application with CSS Modules, Global Styles, and external stylesheets.
Examples
Next.js supports multiple ways of handling CSS, including:
CSS Modules
Next.js has built-in support for CSS Modules using the .module.css
extension.
CSS Modules locally scope CSS by automatically creating a unique class name. This allows you to use the same class name in different files without worrying about collisions. This behavior makes CSS Modules the ideal way to include component-level CSS.
Example
For example, consider a reusable Button
component in the components/
folder:
First, create components/Button.module.css
with the following content:
Then, create components/Button.js
, importing and using the above CSS file:
CSS Modules are only enabled for files with the .module.css
and .module.sass
extensions.
In production, all CSS Module files will be automatically concatenated into many minified and code-split .css
files.
These .css
files represent hot execution paths in your application, ensuring the minimal amount of CSS is loaded for your application to paint.
Global Styles
To add a stylesheet to your application, import the CSS file within pages/_app.js
.
For example, consider the following stylesheet named styles.css
:
Create a pages/_app.js
file if not already present.
Then, import
the styles.css
file.
These styles (styles.css
) will apply to all pages and components in your application.
Due to the global nature of stylesheets, and to avoid conflicts, you may only import them inside pages/_app.js
.
In development, expressing stylesheets this way allows your styles to be hot reloaded as you edit them—meaning you can keep application state.
In production, all CSS files will be automatically concatenated into a single minified .css
file. The order that the CSS is concatenated will match the order the CSS is imported into the _app.js
file. Pay special attention to imported JS modules that include their own CSS; the JS module's CSS will be concatenated following the same ordering rules as imported CSS files. For example:
External Stylesheets
Next.js allows you to import CSS files from a JavaScript file.
This is possible because Next.js extends the concept of import
beyond JavaScript.
Import styles from node_modules
Since Next.js 9.5.4, importing a CSS file from node_modules
is permitted anywhere in your application.
For global stylesheets, like bootstrap
or nprogress
, you should import the file inside pages/_app.js
.
For example:
For importing CSS required by a third-party component, you can do so in your component. For example:
Additional Features
Next.js includes additional features to improve the authoring experience of adding styles:
- When running locally with
next dev
, local stylesheets (either global or CSS modules) will take advantage of Fast Refresh to instantly reflect changes as edits are saved. - When building for production with
next build
, CSS files will be bundled into fewer minified.css
files to reduce the number of network requests needed to retrieve styles. - If you disable JavaScript, styles will still be loaded in the production build (
next start
). However, JavaScript is still required fornext dev
to enable Fast Refresh.