codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
▲

Next.js

17 / 73 topics
16CSS Modules17Global Styles18Sass Support19Styled JSX20Theming with Next.js
Tutorials/Next.js/Global Styles
▲Next.js

Global Styles

Updated 2026-04-20
3 min read

Global Styles in Next.js

Styling is a crucial aspect of any web application, and Next.js provides several ways to manage styles efficiently. In this section, we will explore how to implement global styles in your Next.js applications. We'll cover various methods, including CSS Modules, styled-jsx, and third-party libraries like Tailwind CSS.

Introduction

Global styles are styles that apply across the entire application, affecting all components. Managing these styles can be challenging, especially as your application grows. Next.js offers several strategies to handle global styles effectively.

Using CSS Files

The simplest way to add global styles in Next.js is by using a regular CSS file. You can import this file into your _app.js or _app.tsx file, which is the entry point for your Next.js application.

Step-by-Step Guide

  1. Create a CSS File:

    Create a new CSS file in your project directory. For example, styles/globals.css.

    /* styles/globals.css */
    body {
      font-family: Arial, sans-serif;
      margin: 0;
      padding: 0;
    }
    
    h1 {
      color: #333;
    }
    
  2. Import the CSS File in _app.js:

    Import the global styles into your _app.js or _app.tsx file.

    // pages/_app.js
    import '../styles/globals.css';
    
    function MyApp({ Component, pageProps }) {
      return <Component {...pageProps} />;
    }
    
    export default MyApp;
    

Best Practices

  • Keep Global Styles Minimal: Only include styles that are truly global. Avoid adding component-specific styles here.
  • Use Descriptive Class Names: Use clear and descriptive class names to maintain readability and avoid conflicts.

Using styled-jsx

styled-jsx is a built-in CSS solution in Next.js that allows you to write scoped CSS directly within your components. While it's primarily used for local styles, you can also use it for global styles by importing them into _app.js.

Step-by-Step Guide

  1. Create a Global Styles Component:

    Create a new file for your global styles component.

    // components/GlobalStyles.jsx
    import React from 'react';
    
    const GlobalStyles = () => (
      <style jsx global>{`
        body {
          font-family: Arial, sans-serif;
          margin: 0;
          padding: 0;
        }
    
        h1 {
          color: #333;
        }
      `}</style>
    );
    
    export default GlobalStyles;
    
  2. Import the Component in _app.js:

    Import and use the global styles component in your _app.js file.

    // pages/_app.js
    import '../styles/globals.css';
    import GlobalStyles from '../components/GlobalStyles';
    
    function MyApp({ Component, pageProps }) {
      return (
        <>
          <GlobalStyles />
          <Component {...pageProps} />
        </>
      );
    }
    
    export default MyApp;
    

Best Practices

  • Use the global Prop: Always use the global prop in styled-jsx to ensure styles are applied globally.
  • Keep Styles Scoped When Possible: Use local styles for component-specific styling to maintain a clean separation of concerns.

Using CSS Modules

CSS Modules allow you to write scoped CSS, which helps prevent style conflicts between components. While they are primarily used for local styles, you can also use them for global styles by importing them into _app.js.

Step-by-Step Guide

  1. Create a Global Styles Module:

    Create a new CSS module file with the .module.css extension.

    /* styles/globals.module.css */
    :global(body) {
      font-family: Arial, sans-serif;
      margin: 0;
      padding: 0;
    }
    
    :global(h1) {
      color: #333;
    }
    
  2. Import the Module in _app.js:

    Import and use the global styles module in your _app.js file.

    // pages/_app.js
    import '../styles/globals.css';
    import globalStyles from '../styles/globals.module.css';
    
    function MyApp({ Component, pageProps }) {
      return (
        <>
          <style jsx>{`
            body {
              composes: ${globalStyles.body};
            }
            h1 {
              composes: ${globalStyles.h1};
            }
          `}</style>
          <Component {...pageProps} />
        </>
      );
    }
    
    export default MyApp;
    

Best Practices

  • Use the :global Selector: Use the :global selector to apply styles globally within a CSS module.
  • Avoid Overuse of Global Styles: Try to keep global styles to a minimum and use CSS Modules for component-specific styling.

Using Third-Party Libraries

Third-party libraries like Tailwind CSS can be integrated into Next.js to manage global styles efficiently. These libraries provide utility-first classes that you can apply directly in your components.

Step-by-Step Guide

  1. Install Tailwind CSS:

    Install Tailwind CSS and its dependencies using npm or yarn.

    npm install -D tailwindcss@latest postcss@latest autoprefixer@latest
    npx tailwindcss init -p
    
  2. Configure Tailwind CSS:

    Configure Tailwind CSS by editing the tailwind.config.js file.

    // tailwind.config.js
    module.exports = {
      content: [
        './pages/**/*.{js,ts,jsx,tsx}',
        './components/**/*.{js,ts,jsx,tsx}',
      ],
      theme: {
        extend: {},
      },
      plugins: [],
    };
    
  3. Create a Global Styles File:

    Create a new CSS file to include Tailwind's base, components, and utilities.

    /* styles/globals.css */
    @tailwind base;
    @tailwind components;
    @tailwind utilities;
    
  4. Import the CSS File in _app.js:

    Import the global styles into your _app.js file.

    // pages/_app.js
    import '../styles/globals.css';
    
    function MyApp({ Component, pageProps }) {
      return <Component {...pageProps} />;
    }
    
    export default MyApp;
    

Best Practices

  • Utilize Utility Classes: Use Tailwind's utility classes to quickly style your components.
  • Customize Tailwind Configuration: Customize the Tailwind configuration to fit your design system.

Conclusion

Next.js provides multiple ways to handle global styles, each with its own advantages. Whether you prefer using CSS files, styled-jsx, CSS Modules, or third-party libraries like Tailwind CSS, Next.js offers flexible solutions to manage your application's styling needs effectively. By following best practices and choosing the right approach for your project, you can maintain a clean and efficient styling strategy in your Next.js applications.

Additional Resources

  • Next.js Documentation on Styling
  • styled-jsx Documentation
  • Tailwind CSS Documentation

By leveraging these tools and techniques, you can create well-styled Next.js applications that are maintainable and scalable.


PreviousCSS ModulesNext Sass Support

Recommended Gear

CSS ModulesSass Support