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.
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.
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.
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;
}
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;
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.
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;
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;
global Prop: Always use the global prop in styled-jsx to ensure styles are applied globally.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.
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;
}
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;
:global Selector: Use the :global selector to apply styles globally within a CSS module.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.
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
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: [],
};
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;
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;
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.
By leveraging these tools and techniques, you can create well-styled Next.js applications that are maintainable and scalable.