Built-In CSS Support

    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.

    To add a stylesheet to your application, import the CSS file within .

    For example, consider the following stylesheet named styles.css:

    Create a pages/_app.js file if not already present. Then, the styles.css file.

    1. import '../styles.css'
    2. // This default export is required in a new `pages/_app.js` file.
    3. export default function MyApp({ Component, pageProps }) {
    4. return <Component {...pageProps} />
    5. }

    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.

    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:

    1. // pages/_app.js
    2. import 'bootstrap/dist/css/bootstrap.css'
    3. export default function MyApp({ Component, pageProps }) {
    4. return <Component {...pageProps} />
    5. }

    For importing CSS required by a third party component, you can do so in your component. For example:

    1. // components/ExampleDialog.js
    2. import { useState } from 'react'
    3. import { Dialog } from '@reach/dialog'
    4. import VisuallyHidden from '@reach/visually-hidden'
    5. import '@reach/dialog/styles.css'
    6. function ExampleDialog(props) {
    7. const [showDialog, setShowDialog] = useState(false)
    8. const open = () => setShowDialog(true)
    9. const close = () => setShowDialog(false)
    10. return (
    11. <div>
    12. <button onClick={open}>Open Dialog</button>
    13. <Dialog isOpen={showDialog} onDismiss={close}>
    14. <button className="close-button" onClick={close}>
    15. <span aria-hidden>×</span>
    16. </button>
    17. <p>Hello there. I am a dialog</p>
    18. </Dialog>
    19. </div>
    20. )

    Next.js supports CSS Modules using the [name].module.css file naming convention.

    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:

    1. import styles from './Button.module.css'
    2. export function Button() {
    3. return (
    4. <button
    5. type="button"
    6. // Note how the "error" class is accessed as a property on the imported
    7. // `styles` object.
    8. className={styles.error}
    9. >
    10. Destroy
    11. </button>
    12. )
    13. }

    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:

    1. npm install --save-dev sass

    Sass support has the same benefits and restrictions as the built-in CSS support detailed above.

    For example to add includePaths:

    1. const path = require('path')
    2. module.exports = {
    3. sassOptions: {
    4. includePaths: [path.join(__dirname, 'styles')],
    5. },
    6. }

    Next.js supports Sass variables exported from CSS Module files.

    For example, using the exported primaryColor Sass variable:

    1. // pages/_app.js
    2. export default function MyApp({ Component, pageProps }) {
    3. return (
    4. <Layout color={variables.primaryColor}>
    5. <Component {...pageProps} />
    6. </Layout>
    7. )
    8. }

    CSS-in-JS

    Examples

    It’s possible to use any existing CSS-in-JS solution. The simplest one is inline styles:

    1. function HiThere() {
    2. return <p style={{ color: 'red' }}>hi there</p>
    3. }
    4. export default HiThere

    We bundle styled-jsx to provide support for isolated scoped CSS. The aim is to support “shadow CSS” similar to Web Components, which unfortunately .

    See the above examples for other popular CSS-in-JS solutions (like Styled Components).

    A component using styled-jsx looks like this:

    1. function HelloWorld() {
    2. return (
    3. <div>
    4. Hello world
    5. <p>scoped!</p>
    6. <style jsx>{`
    7. p {
    8. color: blue;
    9. }
    10. div {
    11. background: red;
    12. }
    13. @media (max-width: 600px) {
    14. div {
    15. background: blue;
    16. }
    17. }
    18. `}</style>
    19. <style global jsx>{`
    20. body {
    21. background: black;
    22. }
    23. `}</style>
    24. </div>
    25. )
    26. }

    Please see the styled-jsx documentation for more examples.

    Yes, if you disable JavaScript the CSS will still be loaded in the production build (next start). During development, we require JavaScript to be enabled to provide the best developer experience with .

    For more information on what to do next, we recommend the following sections: