Sub topic 4
Configuring dark mode in Tailwind v3 and v4
This chapter assumes you know basic HTML and CSS, Tailwind utility classes and variants such as hover: and md:, and basic JavaScript. It also assumes a project with Tailwind already installed. By the end you will be able to switch the dark: variant from following the operating system to following a class or attribute, in both Tailwind v3 and v4.
What you are configuring
The dark: variant needs a rule for when "dark mode is active". Tailwind supports two rules:
- Media strategy.
dark:applies when the browser reportsprefers-color-scheme: dark. The user's operating system decides. This is the default in v3 and v4. - Manual strategy.
dark:applies when a marker, such as a.darkclass or adata-themeattribute, is on an ancestor element. Your code decides.
You need the manual strategy for a theme toggle. Without it, the buttons you build will not change anything, because the system preference overrides them.
Tailwind v3
In v3 you set the darkMode option in tailwind.config.js. Leaving it out gives you the media strategy.
// tailwind.config.js (Tailwind v3.4.1 and later)
module.exports = {
darkMode: 'selector', // use 'class' in earlier v3 versions
content: ['./src/**/*.{html,js}'],
};With darkMode: 'selector', dark: utilities apply when the .dark class is on an ancestor. The usual place for it is the <html> element:
<html class="dark">
<body>
<div class="bg-white text-gray-900 dark:bg-gray-900 dark:text-white">
<h1 class="text-2xl font-bold">Hello</h1>
<p class="text-gray-600 dark:text-gray-300">This card follows the active theme.</p>
</div>
</body>
</html>Remove class="dark" and the light colors return.
The 'selector' value was added in v3.4.1. Earlier v3 releases use 'class', which behaves the same way for this purpose. If you are unsure which version you have, run npm ls tailwindcss.
Tailwind v4
Tailwind v4 has no darkMode option and no JavaScript config by default. You configure the variant in your CSS with @custom-variant, which redefines what dark: means.
/* Tailwind v4 */
@import "tailwindcss";
@custom-variant dark (&:where(.dark, .dark *));The selector reads: the element itself is .dark, or it is inside a .dark element. :where() keeps the added specificity at zero, so your dark: utilities do not become harder to override than other utilities.
You are not limited to a class. The same technique works with a data attribute:
/* Tailwind v4, using a data attribute instead of a class */
@import "tailwindcss";
@custom-variant dark (&:where([data-theme=dark], [data-theme=dark] *));With this version, you mark the page with <html data-theme="dark">.
Choosing a strategy
| Question | Media strategy | Manual strategy |
|---|---|---|
| Who decides the theme? | The operating system | Your code and the user |
| Setup needed | None | Config option (v3) or `@custom-variant` (v4) |
| JavaScript needed | No | Yes, to set the marker |
| User can override the system | No | Yes |
Choose the media strategy if you want zero code and are happy to follow the system. Choose the manual strategy if users should be able to pick light or dark on your site. The next chapters build that picker.
Check that it works
After changing the configuration, test it in the browser:
- Rebuild your CSS or restart the dev server.
- Open DevTools and select the
<html>element. - Add the
darkclass (or your attribute) by hand. - Confirm that elements with
dark:utilities change. - Remove the marker and confirm they change back.
If nothing changes, the usual causes are a stale build, the wrong version's syntax, or a content path (v3) that does not include the file you are editing.
Exercise
Your project uses Tailwind v4, and you want dark: utilities to apply when <html data-theme="dark"> is set. Which line do you add to your CSS after @import "tailwindcss";, and why can you not use a darkMode option?
Show answer
Add @custom-variant dark (&:where([data-theme=dark], [data-theme=dark] *));. Tailwind v4 has no darkMode config option, so you redefine the dark variant in CSS. The selector matches the element with the attribute and everything inside it.
Exercise
In Tailwind v3.3, which value of darkMode makes dark: apply when a .dark class is on an ancestor?
Show answer
'class'. The 'selector' value only exists from v3.4.1 onward.