Sub topic 2
Default behavior: following the operating system
This chapter assumes you have a project with Tailwind CSS installed. It also assumes you know basic HTML and CSS, and that you have used variants such as hover: and md:. By the end, you will know what dark: does when you configure nothing, and where that default stops being enough.
What `dark:` does out of the box
Tailwind's dark: variant applies a utility only when dark mode is active. With no configuration, "active" means the user's operating system or browser is set to a dark appearance. Both Tailwind v3 and v4 behave this way.
<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>Each dark: utility overrides its light counterpart only in dark mode. Without any dark: classes, the element keeps its light styles in both modes. You write the light design first and add dark: overrides only where the colors need to change.
The mechanism: a media query
The default is called the media strategy. Tailwind wraps every dark: utility in the CSS prefers-color-scheme media query. This is a simplified version of what is generated for dark:bg-gray-900:
@media (prefers-color-scheme: dark) {
.dark\:bg-gray-900 {
background-color: #111827;
}
}The browser reads the appearance setting from the operating system (for example, macOS Appearance or Windows "Choose your mode"). It then decides whether the media query matches. Your page does no work. The rule applies when the query matches and does nothing otherwise.
The theme also changes live. If the user switches their OS from light to dark while your page is open, the browser re-evaluates the query and the page restyles without a reload. Many systems also switch automatically at sunset, and your page follows that too.
Reading the preference in JavaScript
You rarely need JavaScript with this strategy. Still, the same query is available through matchMedia, and later chapters rely on it. This snippet reports the current preference and logs changes:
const query = window.matchMedia("(prefers-color-scheme: dark)");
console.log("Dark mode active:", query.matches);
query.addEventListener("change", (event) => {
console.log("Dark mode active:", event.matches);
});query.matches is true when the system prefers dark. The change event fires when the user changes the system setting.
Testing without changing your OS
You don't have to switch your whole computer to dark mode to test.
Trade-offs of the default
The media strategy is the simplest option. It needs no configuration, no JavaScript, and no stored state. It also respects what the user already told their device, which is a good default for content sites and documentation.
It has one limit. The page cannot override the system. A visitor whose OS is set to light cannot choose dark for your site, and the reverse is also true. A theme button in your interface would have nothing to change, because the media query reflects only the system setting.
| Question | Media strategy |
|---|---|
| Needs config in v3 or v4? | No |
| Needs JavaScript? | No |
| Follows OS changes live? | Yes |
| Lets the user override per site? | No |
| Flash of wrong theme on load? | No, CSS decides before first paint |
The last row matters for later chapters. Because the browser evaluates the media query as part of CSS, the first paint already uses the correct theme. The flash-of-wrong-theme problem appears only once you add JavaScript-driven toggling.
Common mistakes with the default
- Expecting a toggle to work. Adding a
.darkclass to<html>does nothing under the media strategy. The generated CSS never looks for that class. - Forgetting the light styles.
dark:text-whiteon its own gives white text on a dark background, but the light mode still needs a readable color. Set both. - Testing in only one mode. A page can look right in your OS theme and be unreadable in the other one. Check both.
The next chapter shows how to switch to a manual strategy that keys dark: to a class or selector. Use it when you need a user-controlled theme.
Exercise
Your site uses bg-white dark:bg-gray-900 with no Tailwind dark mode configuration. A visitor's laptop is set to light mode, and they click a button on your page that adds the dark class to <html>. What happens to the background, and why?
Show answer
The background stays white. With the default media strategy, dark: utilities are wrapped in @media (prefers-color-scheme: dark), so they depend only on the system setting. The dark class is never checked. To make the class work, you must configure the manual (class or selector) strategy.
Exercise
How can you check how your page looks in dark mode without changing your operating system's appearance?
Show answer
Use the browser developer tools to emulate the prefers-color-scheme media feature, for example the "Emulate CSS media feature prefers-color-scheme" option in the Rendering panel in Chrome or Edge.