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

20 / 73 topics
16CSS Modules17Global Styles18Sass Support19Styled JSX20Theming with Next.js
Tutorials/Next.js/Theming with Next.js
▲Next.js

Theming with Next.js

Updated 2026-04-20
3 min read

Theming with Next.js

Theming is a crucial aspect of modern web development, allowing you to create visually consistent and customizable user interfaces. In this tutorial, we'll explore how to implement theming in your Next.js applications using various approaches, including CSS Modules, styled-components, and Tailwind CSS.

Introduction to Theming

Theming involves defining a set of design tokens such as colors, typography, spacing, and layout that can be applied consistently across your application. This approach not only enhances the visual consistency but also makes it easier to maintain and update the look and feel of your app.

Prerequisites

Before we dive into theming, ensure you have the following:

  • Basic knowledge of Next.js
  • Node.js installed on your machine
  • A code editor (e.g., VSCode)

Setting Up a Next.js Project

If you haven't already set up a Next.js project, you can do so by running the following commands in your terminal:

npx create-next-app@latest my-theming-app
cd my-theming-app
npm run dev

This will start a development server at http://localhost:3000.

Approach 1: CSS Modules

CSS Modules allow you to scope styles to specific components, preventing style conflicts and making theming more manageable.

Step 1: Create a Theme File

Create a file named theme.js in the styles directory:

// styles/theme.js
export const colors = {
  primary: '#0070f3',
  secondary: '#0056b3',
  background: '#fff',
};

export const fonts = {
  body: 'Arial, sans-serif',
};

export const spacing = {
  small: '8px',
  medium: '16px',
  large: '24px',
};

Step 2: Create a Styled Component

Create a component that uses the theme:

// components/ThemedButton.js
import styles from '../styles/Button.module.css';
import { colors } from '../styles/theme';

const ThemedButton = ({ children }) => {
  return (
    <button className={styles.button} style={{ backgroundColor: colors.primary }}>
      {children}
    </button>
  );
};

export default ThemedButton;

Step 3: Use the Styled Component

Use the ThemedButton component in your pages:

// pages/index.js
import ThemedButton from '../components/ThemedButton';

const Home = () => {
  return (
    <div>
      <h1>Welcome to Next.js Theming</h1>
      <ThemedButton>Click Me</ThemedButton>
    </div>
  );
};

export default Home;

Approach 2: styled-components

styled-components is a popular library for styling React components. It allows you to write CSS directly in your JavaScript files and automatically handles class names.

Step 1: Install styled-components

Run the following command to install styled-components:

npm install styled-components

Step 2: Create a Theme Provider

Create a file named ThemeProvider.js in the components directory:

// components/ThemeProvider.js
import React from 'react';
import { ThemeProvider } from 'styled-components';

const theme = {
  colors: {
    primary: '#0070f3',
    secondary: '#0056b3',
    background: '#fff',
  },
  fonts: {
    body: 'Arial, sans-serif',
  },
  spacing: {
    small: '8px',
    medium: '16px',
    large: '24px',
  },
};

const ThemeProviderWrapper = ({ children }) => {
  return <ThemeProvider theme={theme}>{children}</ThemeProvider>;
};

export default ThemeProviderWrapper;

Step 3: Use the Theme Provider

Wrap your application with the ThemeProvider in _app.js:

// pages/_app.js
import '../styles/globals.css';
import ThemeProvider from '../components/ThemeProvider';

function MyApp({ Component, pageProps }) {
  return (
    <ThemeProvider>
      <Component {...pageProps} />
    </ThemeProvider>
  );
}

export default MyApp;

Step 4: Create a Styled Component

Create a styled component that uses the theme:

// components/ThemedButton.js
import styled from 'styled-components';

const Button = styled.button`
  background-color: ${({ theme }) => theme.colors.primary};
  color: white;
  padding: ${({ theme }) => theme.spacing.medium} ${({ theme }) => theme.spacing.large};
  font-family: ${({ theme }) => theme.fonts.body};
`;

const ThemedButton = ({ children }) => {
  return <Button>{children}</Button>;
};

export default ThemedButton;

Step 5: Use the Styled Component

Use the ThemedButton component in your pages:

// pages/index.js
import ThemedButton from '../components/ThemedButton';

const Home = () => {
  return (
    <div>
      <h1>Welcome to Next.js Theming</h1>
      <ThemedButton>Click Me</ThemedButton>
    </div>
  );
};

export default Home;

Approach 3: Tailwind CSS

Tailwind CSS is a utility-first CSS framework that allows you to rapidly build custom designs without ever leaving your HTML. It integrates well with Next.js and provides a powerful theming system.

Step 1: Install Tailwind CSS

Run the following commands to install Tailwind CSS:

npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p

Step 2: Configure Tailwind CSS

Edit tailwind.config.js to define your theme:

// tailwind.config.js
module.exports = {
  content: [
    './pages/**/*.{js,ts,jsx,tsx}',
    './components/**/*.{js,ts,jsx,tsx}',
  ],
  theme: {
    extend: {
      colors: {
        primary: '#0070f3',
        secondary: '#0056b3',
      },
      spacing: {
        '128': '32rem',
      },
    },
  },
  plugins: [],
};

Step 3: Create a Theme Provider

Create a file named ThemeProvider.js in the components directory:

// components/ThemeProvider.js
import React from 'react';
import { ThemeProvider } from 'styled-components';

const theme = {
  colors: {
    primary: '#0070f3',
    secondary: '#0056b3',
  },
};

const ThemeProviderWrapper = ({ children }) => {
  return <ThemeProvider theme={theme}>{children}</ThemeProvider>;
};

export default ThemeProviderWrapper;

Step 4: Use the Theme Provider

Wrap your application with the ThemeProvider in _app.js:

// pages/_app.js
import '../styles/globals.css';
import ThemeProvider from '../components/ThemeProvider';

function MyApp({ Component, pageProps }) {
  return (
    <ThemeProvider>
      <Component {...pageProps} />
    </ThemeProvider>
  );
}

export default MyApp;

Step 5: Use Tailwind CSS in Components

Use Tailwind CSS classes in your components:

// components/ThemedButton.js
const ThemedButton = ({ children }) => {
  return (
    <button className="bg-primary text-white px-4 py-2 font-bold">
      {children}
    </button>
  );
};

export default ThemedButton;

Step 6: Use the Styled Component

Use the ThemedButton component in your pages:

// pages/index.js
import ThemedButton from '../components/ThemedButton';

const Home = () => {
  return (
    <div>
      <h1>Welcome to Next.js Theming</h1>
      <ThemedButton>Click Me</ThemedButton>
    </div>
  );
};

export default Home;

Best Practices

  1. Separation of Concerns: Keep your styles and logic separate. Use CSS Modules or styled-components for component-specific styling.
  2. Consistent Naming: Use consistent naming conventions for your theme tokens to maintain readability and consistency.
  3. Responsive Design: Ensure that your themes are responsive and adapt to different screen sizes.
  4. Performance Optimization: Minimize the number of styles loaded by using CSS Modules or scoped styles.
  5. Accessibility: Consider accessibility when defining your theme, ensuring sufficient contrast ratios and keyboard navigability.

Conclusion

Theming is a powerful tool for creating visually consistent and customizable user interfaces in Next.js applications. By leveraging CSS Modules, styled-components, or Tailwind CSS, you can implement theming effectively and efficiently. Choose the approach that best fits your project's needs and preferences, and enjoy building beautiful and maintainable web applications.

Feel free to experiment with these approaches and adapt them to suit your specific requirements. Happy coding!


PreviousStyled JSXNext Layout Component

Recommended Gear

Styled JSXLayout Component