Sub topic 1
Styling for dark mode with the dark: variant
Prerequisites
This chapter assumes you know basic HTML and CSS, and that you have used Tailwind utility classes and variants such as hover: and md:. You need a project with Tailwind CSS already installed. Later chapters use JavaScript (the DOM and localStorage), but this one does not.
The working model
Tailwind's dark: variant is a prefix. It tells Tailwind to apply a utility only when dark mode is active. Without the prefix, a utility applies in every mode.
A common pattern is to write the light style first, then add a dark: utility for each property that 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>In light mode, the card has a white background and dark text. In dark mode, dark:bg-gray-900 and dark:text-white override the light values. The heading has no dark: classes, so it keeps its size and weight in both modes. It inherits its color from the parent, which changes with the theme.
Only write a dark: class for a property that needs a different value in dark mode. If a style is the same in both modes, leave it unprefixed.
What "active" means
The dark: variant needs a rule for deciding when dark mode is on. Tailwind supports two approaches.
| Strategy | Dark mode is active when | Who decides |
|---|---|---|
| Media (the default) | The operating system or browser reports `prefers-color-scheme: dark` | The user's system setting |
| Class or selector | A `.dark` class (or another selector you choose) is on an ancestor element | Your code, for example a toggle button |
prefers-color-scheme is a CSS media feature. It reports whether the user has asked their device for a light or dark appearance.
The media strategy needs no setup. If you add dark: classes and switch your system to dark, the page changes. The cost is that users cannot override their system setting on your site.
The class or selector strategy gives you control. Your code decides when dark mode is on, so you can offer a toggle and remember the choice. It requires a small configuration change, which differs between Tailwind v3 and v4. The next chapters cover that setup.
Trying it out
To see the default behavior, follow these steps.
- Add the card markup above to a page that Tailwind builds.
- Open the page in a browser.
- Switch your operating system to dark appearance. Most browsers also let you emulate this in developer tools, under the rendering options for
prefers-color-scheme. - Confirm that the background and text colors change.
If nothing changes, the next steps are in the troubleshooting chapter. For now, check that the classes are spelled exactly, including the colon, and that Tailwind is scanning the file that contains them.
Common mistakes
- Forgetting the light value. If you write only
dark:bg-gray-900, the element has no background in light mode. Set the base style first. - Setting too many
dark:classes. Every color on the page may need a dark counterpart. Repetition grows fast, and a later chapter shows how CSS variables reduce it. - Expecting a toggle to work with the default setup. With the media strategy, adding a
.darkclass does nothing. The toggle only works after you configure the class or selector strategy. - Relying on color names alone. Swapping
whiteforgray-900does not guarantee readable text. Contrast is covered in a later chapter.
Exercise
A page has <p class="text-gray-600 dark:text-gray-300">. Tailwind uses the default media strategy. A user whose system is set to dark adds a dark class to the <html> element by hand. Which text color does the paragraph have, and why?
Show answer
It has text-gray-300 only if the system is set to dark, because the default media strategy checks prefers-color-scheme and ignores the .dark class. Since this user's system is dark, the paragraph is gray-300. For a user whose system is light, adding the class would change nothing. To make the class control the theme, you must configure the class or selector strategy.