Sub topic 7
Adding dark mode
This chapter assumes you have a Vite and React project with Tailwind CSS v4 installed, and that you know how variants such as hover: and md: work. You should also be comfortable with React state and useEffect.
How the dark: variant works
Tailwind provides a dark: variant. Put it in front of any utility and that utility applies only in dark mode.
export function Panel() {
return (
<div className="bg-white text-gray-900 dark:bg-gray-900 dark:text-gray-100">
Hello
</div>
)
}By default, dark: uses the CSS media query prefers-color-scheme: dark. The page follows the operating system setting, and your code cannot change it. That is fine for simple sites. Most apps also want a toggle, so you need to change the strategy.
Switching to a class-based strategy
In Tailwind v4 you redefine the dark variant in your stylesheet. The rule below makes dark: apply whenever the element or an ancestor has the dark class.
@import "tailwindcss";
@custom-variant dark (&:where(.dark, .dark *));Now React can turn dark mode on by adding dark to the <html> element. The :where() wrapper keeps the selector's specificity at zero, so dark styles do not beat other rules by accident.
Building a toggle
The hook below keeps a dark class on <html> in sync with React state. It saves the choice in localStorage and falls back to the OS preference on the first visit.
import { useEffect, useState } from 'react'
function getInitialDark(): boolean {
const saved = localStorage.getItem('theme')
if (saved) return saved === 'dark'
return window.matchMedia('(prefers-color-scheme: dark)').matches
}
export function ThemeToggle() {
const [dark, setDark] = useState(getInitialDark)
useEffect(() => {
document.documentElement.classList.toggle('dark', dark)
localStorage.setItem('theme', dark ? 'dark' : 'light')
}, [dark])
return (
<button
onClick={() => setDark((d) => !d)}
className="rounded-md bg-gray-100 px-3 py-2 text-gray-900 hover:bg-gray-200 dark:bg-gray-800 dark:text-gray-100 dark:hover:bg-gray-700"
>
{dark ? 'Switch to light' : 'Switch to dark'}
</button>
)
}Passing getInitialDark to useState as a function means React reads localStorage once, not on every render.
Common errors
- Dark styles never apply. You added
dark:classes but forgot the@custom-variantline, or thedarkclass is on an element that is not an ancestor of the styled element. - Flash of the wrong theme. The effect runs after the first paint, so a dark-mode user may briefly see light mode. To avoid it, put a small inline script in
index.htmlthat adds thedarkclass before React loads. - Mixing strategies. With the class-based variant, changing the OS setting no longer has any effect unless your code reads it, as
getInitialDarkdoes once. - Missing pairs. If you set
bg-whitewithout adark:partner, that element stays white in dark mode. Check text, borders and backgrounds together.
Exercise
Exercise
You added dark:bg-gray-900 to a component and clicked your toggle, which adds the dark class to <html>. Nothing changes, but the OS is set to light mode. The stylesheet contains only @import "tailwindcss";. What is missing, and why does it matter?
Show answer
The @custom-variant dark (&:where(.dark, .dark *)); line is missing. Without it, dark: uses the prefers-color-scheme media query, so it follows the OS setting and ignores the dark class. With the OS in light mode, the dark styles never match.