Dark Mode
Many people browse with their operating system set to a dark theme. Tailwind's dark: variant lets you write the dark version of a style right next to the light one: bg-white dark:bg-slate-900.
The default: follow the system
Out of the box, dark: uses the prefers-color-scheme media query. If the visitor's OS or browser is in dark mode, the dark: classes apply automatically.
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
</head>
<body class="bg-slate-100 p-6 dark:bg-slate-950">
<div class="max-w-sm rounded-xl bg-white p-5 shadow-md dark:bg-slate-800 dark:shadow-none dark:ring-1 dark:ring-white/10">
<h2 class="text-lg font-bold text-slate-900 dark:text-white">System theme</h2>
<p class="mt-1 text-sm text-slate-600 dark:text-slate-300">
Switch your operating system between light and dark mode, then run this again.
</p>
</div>
</body>
</html>A manual toggle with @custom-variant
Most sites also offer a toggle button. To make dark: depend on a class instead of the media query, redefine the variant in your CSS:
@import "tailwindcss";
@custom-variant dark (&:where(.dark, .dark *));Now dark: styles apply whenever the element, or any ancestor, has the dark class — usually <html class="dark">. In the browser build you put the same line in a <style type="text/tailwindcss"> block:
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
<style type="text/tailwindcss">
@custom-variant dark (&:where(.dark, .dark *));
</style>
</head>
<body class="min-h-screen bg-amber-50 p-6 text-amber-950 transition-colors dark:bg-indigo-950 dark:text-indigo-100">
<button id="toggle" class="rounded-full bg-amber-950 px-4 py-2 text-sm font-semibold text-amber-50 dark:bg-indigo-200 dark:text-indigo-950">
<span class="dark:hidden">🌙 Go dark</span>
<span class="hidden dark:inline">☀️ Go light</span>
</button>
<h1 class="mt-4 text-2xl font-bold">Manual theme toggle</h1>
<p class="mt-1 text-sm opacity-80">The button adds or removes the <code>dark</code> class on <html>.</p>
<script>
document.getElementById('toggle').addEventListener('click', () => {
document.documentElement.classList.toggle('dark');
});
</script>
</body>
</html>Using a data attribute instead
Some apps prefer data-theme="dark". Just change the selector:
@custom-variant dark (&:where([data-theme=dark], [data-theme=dark] *));Remembering the choice
A real site stores the visitor's choice and applies it before the page paints, to avoid a flash of the wrong theme. Put a tiny script in the <head>:
<script>
const saved = localStorage.getItem('theme');
const systemDark = matchMedia('(prefers-color-scheme: dark)').matches;
document.documentElement.classList.toggle('dark', saved === 'dark' || (!saved && systemDark));
</script>| Approach | Setup | Who decides |
|---|---|---|
Default dark: |
nothing | the operating system |
| Class strategy | @custom-variant dark (&:where(.dark, .dark *)); |
your toggle (.dark on <html>) |
| Data-attribute strategy | @custom-variant dark (&:where([data-theme=dark], [data-theme=dark] *)); |
your toggle (data-theme) |
Note: In v3 you switched strategies with
darkMode: 'class'intailwind.config.js. In v4 the@custom-variantdirective replaces that option.
Designing a good dark theme
Dark mode is more than inverting colours:
| Light | Dark | Why |
|---|---|---|
bg-white |
bg-slate-900 / bg-gray-900 |
pure black is harsh; use a very dark neutral |
text-gray-900 |
text-gray-100 |
high contrast for headings |
text-gray-600 |
text-gray-400 |
softer body text |
border-gray-200 |
border-white/10 |
subtle, translucent borders |
shadow-lg |
ring-1 ring-white/10 |
shadows are invisible on dark backgrounds |
bg-sky-600 |
bg-sky-500 |
slightly brighter accents |
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
<style type="text/tailwindcss">
@custom-variant dark (&:where(.dark, .dark *));
</style>
</head>
<body class="grid grid-cols-2 gap-4 bg-gray-200 p-4">
<div class="rounded-xl border border-gray-200 bg-white p-4 shadow-lg dark:border-white/10 dark:bg-slate-900 dark:shadow-none">
<p class="font-semibold text-gray-900 dark:text-gray-100">Light card</p>
<p class="text-sm text-gray-600 dark:text-gray-400">Outside any .dark element</p>
</div>
<div class="dark">
<div class="rounded-xl border border-gray-200 bg-white p-4 shadow-lg dark:border-white/10 dark:bg-slate-900 dark:shadow-none">
<p class="font-semibold text-gray-900 dark:text-gray-100">Dark card</p>
<p class="text-sm text-gray-600 dark:text-gray-400">Inside a .dark wrapper</p>
</div>
</div>
</body>
</html>Because the class strategy matches .dark *, you can even switch part of a page to dark by wrapping it in an element with the dark class.
Tip: Both cards above have identical classes. Only the wrapper with
class="dark"decides which version you see — that is exactly how a site-wide toggle on<html>works.
Test yourself
By default, when do dark: utilities apply in Tailwind v4?
- When
<html>has classdark - When the user's system prefers a dark colour scheme
- Always after 6 pm
- Never, until configured
Which directive makes dark: depend on a .dark class in v4?
darkMode: 'class'@custom-variant dark (&:where(.dark, .dark *));@theme dark { }@apply dark;
Why replace shadow-lg with something like ring-1 ring-white/10 in dark mode?
- Shadows are not allowed in dark mode
- Dark shadows barely show on dark backgrounds, so a subtle light ring defines the edge
- Rings are faster
- It is required by
@custom-variant