Next.js CSS Styling
In Next.js, there are multiple ways to add styles to your application, from simple global stylesheets to powerful CSS-in-JS libraries. This chapter introduces several of the most common methods, helping you choose the most suitable styling solution for your project needs.
1. Global Styles
Global styles apply to the entire application and are typically used for defining base styles, resetting default styles, or importing third-party style libraries.
How to Add Global Stylesheets
- Create a style file: Create a CSS file in the
stylesdirectory, for examplestyles/globals.css. - Import in
_app.js: Import the stylesheet in thepages/_app.jsfile. This is the only place where you can import global styles.
styles/globals.css example:
Note: Global styles affect all pages and components, so use them carefully to avoid style conflicts.
2. Component-Level CSS
To avoid naming conflicts and scope issues with global styles, Next.js has built-in support for CSS Modules. CSS Modules can scope CSS to a single component.
How to Use CSS Modules
- Create a modular CSS file: Create a CSS file ending with
.module.css, for examplecomponents/Button.module.css. - Import in the component: Import the CSS file in your component file and use it like an object.
components/Button.module.css example:
components/Button.js example:
When using CSS Modules, Next.js automatically generates a unique hash for each class name, ensuring styles only apply to the component that imports them. For example, styles.error might be compiled to Button_error__1a2b3c.
3. Sass/SCSS Support
Next.js has built-in support for Sass. Before using it, you need to install the sass package:
After installation, you can use .scss or .sass files just like regular CSS files.
Global Sass/SCSS
You can import a globals.scss file into pages/_app.js:
Component-Level Sass/SCSS
You can also combine Sass with CSS Modules by naming files with .module.scss or .module.sass:
components/Alert.module.scss example:
components/Alert.js example:
4. CSS-in-JS
CSS-in-JS is a technique that writes CSS directly in JavaScript code. This approach provides powerful dynamic styling capabilities and componentization benefits. Next.js supports all major CSS-in-JS libraries, such as:
- Styled-components
- Emotion
These libraries typically require some additional configuration to achieve server-side rendering (SSR) in Next.js.
Using Styled-components Example
-
Install dependencies:
-
Configure
_document.js: To make Styled-components render correctly on the server, you need to create a custompages/_document.jsfile to collect styles. -
Create Styled Component:
Summary
Next.js provides flexible and diverse styling solutions that you can choose based on project needs and team preferences:
- Global Styles: Suitable for base styles and third-party libraries.
- CSS Modules: Recommended for component-level styles, providing local scope and avoiding conflicts.
- Sass/SCSS: If you prefer preprocessors, Next.js provides out-of-the-box support.
- CSS-in-JS: Suitable for complex applications requiring dynamic styles and high componentization.
In the next chapter, we'll explore how to integrate and use Tailwind CSS, a powerful utility-first CSS framework, in Next.js projects.