Next.js allows you to manage global CSS by customizing the default App component to apply consistent styling across the application.
- Create a custom _app.js file inside the pages directory.
- Extend the default App component provided by Next.js.
- Import global CSS files in _app.js.
- Apply shared styles and configurations to the entire app.
Global CSS in Next.js
Global CSS refers to styles that affect the overall appearance of a web application across all components.
- Applies styles to the entire document or multiple components.
- Affects elements regardless of their location in the app.
- Defined outside individual components.
- Ensures consistent typography, layout, colors, and design.
Uses of global CSS
- Resetting Default Styles: To guarantee consistent rendering across browsers, reset or normalize default styles.
- Typography: Specifying the weights, widths, and styles of fonts for text components across the program.
- Layout: Specifying global layout attributes like padding, margins, and container and other layout component positions.
- Colors and Themes: Establishing color palettes, backdrops, and themes that are used uniformly across the program.
Rules for Using Global CSS in Next.js
Global CSS must be imported only in the custom Next.js App file (pages/_app.js). If a global CSS file is not imported in _app.js, Next.js throws an error due to a violation of its styling rules.
- An attempt is made to import a global CSS file outside of pages/_app.js, which is not allowed in Next.js.
- Global CSS is restricted to the custom App component to ensure proper style ordering and to prevent unintended side effects across the application.
Solutions for Applying Global CSS in Next.js
1. Global CSS File
Create one CSS file in style folder in your Next.js and import this file into your project main 'pages/app.js' file.
/* styles/style.css */
body {
text-align: center;
font-size: 20px;
color: brown;
}
// pages/_app.js
import "./../styles/style.css";
function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />;
}
export default MyApp;
// pages/index.js
export default function Home() {
return (
<div>
<h1>Hello, Global Css</h1>
</div>
);
}
Output:

2. Employing the CSS Module
CSS Modules in Next.js provide a way to apply styles locally to components, helping avoid global style conflicts.
- Styles are scoped to individual components by default.
- Supported natively in Next.js.
- Imported directly into files like index.js.
- Useful for maintaining modular and conflict-free styling.
/* styles/style.module.css */
.global-styles {
text-align: center;
font-family: "Roboto", sans-serif;
font-size: larger;
color: cornflowerblue;
}
//pages/_app.js
import styles from "./../styles/style.module.css";
function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />;
}
export default MyApp;
// pages/index.js
export default function Home() {
return (
<div>
<h1>Hello, Global Css</h1>
</div>
);
}
Output:

3. Straight Line Design:
Inline styling allows styles to be applied directly within JSX elements.
- Styles are written using the style attribute in JSX.
- Not recommended for defining global styles due to limited reusability.
// pages/index.js
import React from "react";
export default function Home() {
return (
<div style={{ backgroundColor: "pink", textAlign: "center" }}>
<h1>Hello, Global css</h1>
</div>
);
}
Output:
4. Using a third-party CSS-in-JS solution:
CSS-in-JS libraries allow you to define and manage styles directly within JavaScript files.
- Libraries like Styled Components and Emotion are commonly used.
- Styles can be created and applied inside components, including global styles.
5. Customize Webpack setup:
Next.js allows customization of Webpack configuration for advanced or complex CSS needs.
- Modify the next.config.js file to add custom plugins or settings.
- This approach is advanced and should be used carefully.
6. Verify versions and dependencies:
Keeping Next.js and its dependencies up to date helps prevent compatibility issues.
- Ensure all dependencies are compatible with your Next.js version.
- Outdated packages may cause unexpected errors.
7. Examine community resources and documentation:
Next.js offers extensive documentation and strong community support for troubleshooting and learning.
- Refer to official Next.js documentation for accurate guidance.
- Use community forums like Stack Overflow or GitHub issues for practical solutions.