NextJS multiple themes using Tailwind V4
What Makes Tailwind Great?
Tailwind has taken over modern web development, by changing our approach to styling content. Instead of writing custom CSS classes and struggling with naming conventions, Tailwind provides atomic utility classes that you apply directly to your HTML elements.
The Power of Co-location
One of Tailwind’s greatest strengths is co-location – your styles live right next to your content in your markup. This approach eliminates the constant context switching between HTML and CSS files, making development faster and more intuitive. When you see <div className="bg-blue-500 text-white p-4 rounded-lg">, you immediately understand what this element looks like without hunting through stylesheets.
No More Naming Headaches
Gone are the days of wrestling with class naming, with Tailwind, you don’t need to invent class names like .sidebar-navigation-item – you simply use utilities like bg-primary text-white border-l-4 border-accent.
Consistent Design Systems
Tailwind enforces consistency by providing a well-thought-out design system out of the box. Instead of arbitrary values scattered throughout your CSS, you use standardised and descriptive classes for spacing, colours, typography, and more.
What’s Revolutionary About Tailwind CSS v4?
Tailwind CSS v4, released in January 2025, represents a complete ground-up rewrite that embraces modern CSS features and eliminates the traditional JavaScript configuration file approach.
CSS-First Configuration
The biggest change in v4 is the move from JavaScript-based configuration to CSS-first configuration. No more tailwind.config.js files! Everything is now configured directly in your CSS using modern CSS features:
@import "tailwindcss";
@theme {
--font-display: "Satoshi", "sans-serif";
--breakpoint-3xl: 1920px;
--color-brand-100: oklch(0.99 0 0);
--color-brand-500: oklch(0.84 0.18 117.33);
--ease-smooth: cubic-bezier(0.3, 0, 0, 1);
}Blazingly Fast Performance
Tailwind v4 delivers incredible performance improvements:
| v3.4 | v3.4 | v4.0 | Improvement |
|---|---|---|---|
| Full build | 378ms | 100ms | 3.78x |
| Incremental rebuild with new CSS | 44ms | 5ms | 8.8x |
| Incremental rebuild with no new CSS | 35ms | 192µs | 182x |
Setting Up Multiple Themes with next-themes and Tailwind v4
Let’s build a complete theming solution using the new Tailwind v4 approach.
Installation and Setup
First, create a new Next.js project and install the required dependencies:
pnpx create-next-app@latest multiple-theme-v4
#CLI Options
√ Would you like to use TypeScript? ... Yes
√ Which linter would you like to use? » Biome # your choice of linter
√ Would you like to use Tailwind CSS? ... Yes
√ Would you like your code inside a `src/` directory? ... No
√ Would you like to use App Router? (recommended) ... Yes
√ Would you like to use Turbopack? (recommended) ... Yes
√ Would you like to customize the import alias (`@/*` by default)? ... Nocd ./multiple-theme-v4
pnpm add next-themes lucide-reactSetup Tailwind v4 with Theme Support
Now open your project with your favourite editor and you should find that your globals.css file (app/globals.css) look somewhat like this:
/* app/globals.css */
@import "tailwindcss";
@theme inline {
--color-background: var(--background);
--color-foreground: var(--foreground);
--font-sans: var(--font-geist-sans);
--font-mono: var(--font-geist-mono);
}
:root {
--background: #ffffff;
--foreground: #171717;
}
body {
background: var(--background);
color: var(--foreground);
font-family: Arial, Helvetica, sans-serif;
}Creating the first themes: Dark and Pink
/* app/globals.css */
@import "tailwindcss";
@theme inline {
--color-background: var(--background);
--color-foreground: var(--foreground);
--font-sans: var(--font-geist-sans);
--font-mono: var(--font-geist-mono);
--radius-sm: calc(var(--radius) - 4px);
--radius-md: calc(var(--radius) - 2px);
--radius-lg: var(--radius);
--radius-xl: calc(var(--radius) + 4px);
--color-muted: var(--muted);
}
:root {
--background: #ffffff;
--foreground: #171717;
--muted: #f3f3f3;
--radius: 0.625rem; /* Not strictly necessary but makes things nicer */
}
.dark {
--background: #171717;
--foreground: #ffffff;
--muted: #232323;
}
.pink {
--background: #3f002a;
--foreground: #b664a4;
--muted: #5a2d47;
}
body {
background: var(--background);
color: var(--foreground);
font-family: Arial, Helvetica, sans-serif;
}Create a Theme Provider
Create components/theme-provider.tsx:
'use client'
import {
ThemeProvider as NextThemesProvider,
type ThemeProviderProps,
} from 'next-themes'
export function ThemeProvider({ children, ...props }: ThemeProviderProps) {
return <NextThemesProvider
themes={["light", "dark", "pink"]}
defaultTheme='dark'
attribute="class"
{...props}
>{children}</NextThemesProvider>
}Now add the theme provider to the root layout.tsx
import type { Metadata } from "next";
import { Geist, Geist_Mono } from "next/font/google";
import "./globals.css";
import { ThemeProvider } from "@/components/theme-provider";
const geistSans = Geist({
variable: "--font-geist-sans",
subsets: ["latin"],
});
const geistMono = Geist_Mono({
variable: "--font-geist-mono",
subsets: ["latin"],
});
export const metadata: Metadata = {
title: "Create Next App",
description: "Generated by create next app",
};
export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return (
<html lang="en" suppressHydrationWarning>
<body
className={`${geistSans.variable} ${geistMono.variable} antialiased`}
>
<ThemeProvider>
{children}
</ThemeProvider>
</body>
</html>
);
}— NOTE —
You may notice some hydration warnings, if so add suppressHydrationWarning to the root html tag:
<html lang="en" suppressHydrationWarning >Create a Theme Switcher Component
Create components/theme-switcher.tsx:
"use client";
import { useEffect, useState } from "react";
import { useTheme } from "next-themes";
import { Moon, Sun, Monitor, Palette } from "lucide-react";
export function ThemeSwitcher() {
const { theme, setTheme, themes } = useTheme();
const [mounted, setMounted] = useState(false);
useEffect(() => {
setMounted(true);
}, []);
if (!mounted) {
return <div className="flex gap-2 p-1 bg-muted rounded-lg">
{themes.map((themeOption) => (
<button
key={themeOption}
type="button"
className={"flex items-center gap-2 px-3 py-1.5 rounded-md text-sm font-medium border transition-colors duration-200 box-border border-transparent text-muted-foreground hover:text-foreground hover:bg-background"}
>
<span className="inline-flex justify-center items-center w-5 h-5">
?
</span>
<span className="capitalize">{themeOption}</span>
</button>
))}
</div>
}
const themeIcons: Record<string, React.ReactNode> = {
light: <Sun className="h-4 w-4" />,
dark: <Moon className="h-4 w-4" />,
pink: <Palette className="h-4 w-4" />,
system: <Monitor className="h-4 w-4" />,
};
return (
<div className="flex gap-2 p-1 bg-muted rounded-lg">
{themes.map((themeOption) => (
<button
key={themeOption}
type="button"
onClick={() => setTheme(themeOption)}
className={`flex items-center gap-2 px-3 py-1.5 rounded-md text-sm font-medium border transition-colors duration-200 box-border
${theme === themeOption
? "bg-primary text-primary-foreground shadow-sm border-primary"
: "border-transparent text-muted-foreground hover:text-foreground hover:bg-background"
}`}
>
<span className="inline-flex justify-center items-center w-5 h-5">
{themeIcons[themeOption]}
</span>
<span className="capitalize">{themeOption}</span>
</button>
))}
</div>
);
}Setup Layout with Theme Provider
Update your app/layout.tsx:
import type { Metadata } from "next";
import { Geist, Geist_Mono } from "next/font/google";
import "./globals.css";
import { ThemeProvider } from "@/components/theme-provider";
const geistSans = Geist({ variable: "--font-geist-sans", subsets: ["latin"] });
const geistMono = Geist_Mono({ variable: "--font-geist-mono", subsets: ["latin"] });
export const metadata: Metadata = {
title: "Multi-Theme App with Tailwind v4",
description: "A Next.js app showcasing multiple themes with next-themes and Tailwind CSS v4",
};
export default function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
return (
<html lang="en" suppressHydrationWarning>
<body className={`${inter.className} bg-background text-foreground antialiased`}>
<ThemeProvider>
{children}
</ThemeProvider>
</body>
</html>
);
}Create a Demo Page
Update your app/page.tsx:
import Image from "next/image";
import { ThemeSwitcher } from "@/components/theme-switcher";
export default function Home() {
return (
<div className="font-sans grid grid-rows-[20px_1fr_20px] items-center justify-items-center min-h-screen p-8 pb-20 gap-16 sm:p-20">
<main className="flex flex-col gap-[32px] row-start-2 items-center sm:items-start">
<Image
className="dark:invert"
src="/next.svg"
alt="Next.js logo"
width={180}
height={38}
priority
/>
<ThemeSwitcher />
{/* rest of page */}
);
}Done!
Now you know how to create multiple unique themes, lets see how it looks!
Dark Theme
![[tw-themes-dark.png|600]]
Pink Theme
![[tw-themes-pink.png|600]]
Light Theme
![[tw-themes-light.png|600]]
Advanced Theme Features
Dark Themes and Light Themes
Now you may have noticed that depending on what your theme colours are, the images (NEXT.JS and the Vercel Logo) may not have enough contrast or are the same colour as the background
Conclusion
Tailwind CSS v4 represents a massive leap forward in the simplicity of utility-first styling, and with next-themes creating multiple themes for your users to pick from, is easier than ever.
Key benefits of this approach:
- minimal JavaScript configuration - everything in CSS
- Incredible performance
- Full design system integration - seamless theme switching
- SSR compatible - works perfectly with Next.js