Sub topic 3
Taking control with a class or selector
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 you have a project with Tailwind installed. Here you will switch the dark: variant from the operating system setting to a setting your page controls.
Why the default is not enough
By default, dark: follows the prefers-color-scheme media query. The browser reports the operating system setting, and your page cannot override it. That means you cannot add a "Dark" button that works against the system setting.
A manual strategy changes the condition. The dark: utilities apply when an ancestor element carries a marker. That marker is usually a dark class on the <html> element. It can also be an attribute such as data-theme="dark". Your JavaScript adds or removes the marker, so your page decides the theme.
Configuring the class strategy
The configuration differs between major versions.
Tailwind v3
In v3 you set the darkMode option in tailwind.config.js. Use 'selector' from v3.4.1 onward. Earlier v3 releases use 'class'.
// tailwind.config.js (Tailwind v3.4.1 and later)
module.exports = {
darkMode: 'selector', // use 'class' in earlier v3 versions
content: ['./src/**/*.{html,js}'],
};With this setting, dark: utilities apply only when .dark is on an ancestor of the element.
Tailwind v4
Tailwind v4 has no darkMode option. You redefine the dark variant in your CSS with @custom-variant.
/* Tailwind v4 */
@import "tailwindcss";
@custom-variant dark (&:where(.dark, .dark *));The selector &:where(.dark, .dark *) matches the element if it has the dark class itself or sits inside an element that has it. :where() adds no specificity, so your dark utilities do not become harder to override.
Using a data attribute
The same technique works with any marker. If your markup already uses a data-theme attribute, key the variant to it.
/* Tailwind v4, using a data attribute instead of a class */
@import "tailwindcss";
@custom-variant dark (&:where([data-theme=dark], [data-theme=dark] *));You then set data-theme="dark" on <html> instead of adding a class. Your styling code must use the same marker that your variant definition checks.
Using it in markup
The markup does not change. Only the condition behind dark: changes.
<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>To test it, add class="dark" to the <html> element and reload. The card turns dark whatever the operating system says. Remove the class and it turns light again.
Choosing a strategy
| Question | Media (default) | Class or selector |
|---|---|---|
| Who decides the theme? | The operating system | Your page and its JavaScript |
| Can the user override the system setting? | No | Yes |
| Needs JavaScript? | No | Yes |
| Needs configuration? | No | Yes, in v3 config or v4 CSS |
Choose the manual strategy when users should pick a theme in your interface. Choose the default when following the system is enough.
Common mistakes
- Setting
darkMode: 'media'or leaving the option out, then expecting a class to work. - Putting a v3
darkModeoption in a v4 project. v4 ignores it, so use@custom-variant. - Using
'selector'on a v3 version older than 3.4.1. Use'class'there. - Defining the variant with a
data-themeattribute but toggling adarkclass in JavaScript, or the reverse.
Exercise
Your project uses Tailwind v4. You want dark: utilities to apply only when <html data-theme="dark"> is set, and not when the operating system is dark. What do you add to your CSS, and why is a darkMode option in a config file not the answer?
Show answer
Add @custom-variant dark (&:where([data-theme=dark], [data-theme=dark] *)); after @import "tailwindcss";. Tailwind v4 has no darkMode config option, so the dark variant must be redefined in CSS. The selector matches elements that have the attribute or sit inside an element that has it, so setting data-theme="dark" on <html> turns on every dark: utility on the page.