Adding a Global Stylesheet
Last updated
Was this helpful?
Last updated
Was this helpful?
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, 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 .
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.
Next.js supports CSS Modules using the [name].module.css
file naming convention.
CSS Modules locally scope CSS by automatically creating a unique class name. This allows you to use the same CSS class name in different files without worrying about collisions.
This behavior makes CSS Modules the ideal way to include component-level CSS. CSS Module files can be imported anywhere in your application.
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 an optional feature and are only enabled for files with the .module.css
extension. Regular <link>
stylesheets and global CSS files are still supported.
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.
Next.js allows you to import Sass using both the .scss
and .sass
extensions. You can use component-level Sass via CSS Modules and the .module.scss
or .module.sass
extension.
Before you can use Next.js' built-in Sass support, be sure to install sass
:
Sass support has the same benefits and restrictions as the built-in CSS support detailed above.
If you want to configure the Sass compiler you can do so by using sassOptions
in next.config.js
.
For example to add includePaths
:
To support importing .less
or .styl
files you can use the following plugins:
If using the less plugin, don't forget to add a dependency on less as well, otherwise you'll see an error like:
Examples
It's possible to use any existing CSS-in-JS solution. The simplest one is inline styles:
See the above examples for other popular CSS-in-JS solutions (like Styled Components).
A component using styled-jsx
looks like this:
For more information on what to do next, we recommend the following sections:
We bundle to provide support for isolated scoped CSS. The aim is to support "shadow CSS" similar to Web Components, which unfortunately .
Please see the for more examples.