Global Styles in Next.js

cassidoo

Cassidy Williams

Posted on December 10, 2020

Global Styles in Next.js

Welcome to Blogvent, day 9!

In Next.js, you have TONS of support for pretty much every styling option you’d like to use. CSS Modules, Styled JSX, Sass, Less, Stylus, Styled Components, Emotion… I could go on! If you want to style a component, Next.js has you covered.

One thing that often trips people up though is adding global styles. But luckily, the framework has you covered there, too!

In your pages/ directory, add an _app.js file if you don’t already have one. Yours might look something like this:

// pages/_app.js

function Application({ Component, pageProps }) {
  return <Component {...pageProps} />
}

export default Application
Enter fullscreen mode Exit fullscreen mode

Now, to add some simple global styles to your application, it’s as simple as importing it at this level!

import '../styles/globals.css'

function Application({ Component, pageProps }) {
  return <Component {...pageProps} />
}

export default Application
Enter fullscreen mode Exit fullscreen mode

It’s done!

You now have a global stylesheet applied to your Next.js application. If you’d like to see it in a starter application, you can check out this repo here, or deploy the starter directly to Netlify with one click:

Deploy to Netlify

If you’d like to see this applied in a more advanced application, you can check out the repo for Jamstack Explorers!

💖 💪 🙅 🚩
cassidoo
Cassidy Williams

Posted on December 10, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related