Sub topic 2
Default behavior: following the operating system
This chapter covers what the dark: variant does before you configure anything. It assumes you can read basic HTML and CSS, know Tailwind utilities and variants such as hover: and md:, and have a project with Tailwind installed. Basic JavaScript (the DOM and localStorage) is only needed for the last section.
What `dark:` does out of the box
A variant in Tailwind is a prefix that wraps a utility in a condition. hover:bg-gray-200 applies only while the pointer is over the element. The dark: variant works the same way. Its condition is the CSS media feature prefers-color-scheme, which reports whether the user has asked for a light or a dark interface.
This is the default in both Tailwind v3 and v4. You do not add any configuration, plugin or JavaScript. You write the light style first and add a dark: utility for each property that should change in dark mode.
<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>On a device set to light, the card has a white background and near-black text. On a device set to dark, the same markup renders with a near-black background and white text. The page contains no logic. The browser evaluates the media query and applies the matching rules.
What Tailwind generates
Seeing the generated CSS explains most of the behavior later in this chapter. With the default strategy, a dark: utility compiles to the normal utility rule wrapped in a media query. In Tailwind v4 the output looks roughly like this:
.bg-white {
background-color: var(--color-white);
}
@media (prefers-color-scheme: dark) {
.dark\:bg-gray-900 {
background-color: var(--color-gray-900);
}
}Tailwind v3 produces the same structure, but its color declarations use the --tw-bg-opacity variable. Three consequences follow from this output:
- The
dark:rule is only active while the media query matches. Outside dark mode, the rule exists in the stylesheet but does nothing. - The selector is a single class, so a
dark:utility has the same specificity as its light counterpart. Tailwind places variant rules after plain utilities in the stylesheet, so the dark rule wins when both match. You do not need!important. - The condition is evaluated by the browser, not by Tailwind. Tailwind never knows which theme is active.
The third point matters when you debug. If a dark: class does not take effect, the cause is almost always in the environment (the browser's reported preference) or in the build (the class was never generated), not in Tailwind's runtime, because there is no runtime.
Where the preference comes from
prefers-color-scheme has two possible values in practice: light and dark. A third value, no-preference, appeared in the first version of the specification and has since been removed. Current browsers always report light or dark, so you should not write code that depends on a "no preference" state.
The browser picks the value from one of these sources:
- The operating system setting, such as Windows "Choose your mode", macOS "Appearance" or the Android and iOS display settings. Many systems can also switch automatically at sunset.
- A browser-level override. Some browsers let the user choose a theme that overrides the system, and a few do this per profile.
- A developer tools override. Chrome, Edge, Firefox and Safari can emulate either value without touching your system settings.
The result is that the user's choice lives outside your site. They set it once for every site and application on the device, and your page inherits it.
Because that fence must use the required info string, here is the diagram in the correct form:
Trying it yourself
The following page is complete and runnable. It loads the Tailwind v4 browser build from a CDN, so it needs no install step. That build is intended for experiments and prototypes, not for production. Save it as index.html and open it in a browser.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>System dark mode</title>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
</head>
<body class="bg-white text-gray-900 dark:bg-gray-900 dark:text-white">
<main class="mx-auto max-w-md p-8">
<h1 class="text-2xl font-bold">Hello</h1>
<p class="mt-2 text-gray-600 dark:text-gray-300">
This page follows your operating system theme.
</p>
</main>
</body>
</html>To switch themes without changing your system settings, use the emulation in your browser's developer tools. In Chrome and Edge, open DevTools, open the command menu with Ctrl+Shift+P (Cmd+Shift+P on macOS), run "Show Rendering", and set "Emulate CSS media feature prefers-color-scheme" to dark. Firefox and Safari have equivalent toggles in their inspector toolbars. The page restyles immediately and no reload is needed.
Choosing the default strategy explicitly
In Tailwind v3 the default is media, and you can state it in tailwind.config.js to make the choice visible to other developers:
// tailwind.config.js (Tailwind v3)
module.exports = {
darkMode: 'media', // this is the default value
content: ['./src/**/*.{html,js}'],
};In Tailwind v4 there is no darkMode option and nothing to write. The dark variant is defined as the prefers-color-scheme media query unless you override it in CSS. A stylesheet that only imports Tailwind already has system-following dark mode:
/* Tailwind v4: dark: follows prefers-color-scheme with no extra code */
@import "tailwindcss";The later chapters on manual control show how to replace this default. For now, treat the media strategy as what you get when you do nothing.
Reading the same preference in JavaScript
CSS is enough for styling, but sometimes JavaScript needs to know the theme too. Examples are choosing between a light and a dark map style, or setting a chart palette. The matchMedia API evaluates the same media query and can notify you when it changes.
const query = window.matchMedia("(prefers-color-scheme: dark)");
function report() {
console.log(query.matches ? "System prefers dark" : "System prefers light");
}
report();
query.addEventListener("change", report);query.matches is a boolean. The change event fires when the system setting flips, for example at sunset on a device with automatic appearance. Your CSS updates by itself in that case, but any value you copied into JavaScript state does not, so listen for the event if you keep such state.
Strengths and limits
The default strategy suits many sites, and its limits are the reason the manual strategy exists.
| Aspect | Default (media) strategy |
|---|---|
| Setup | None in v4, one optional line in v3 |
| JavaScript needed | None |
| Flash of the wrong theme | Does not occur, because the browser resolves the query before first paint |
| User control on your site | None. The user must change the device or browser setting |
| Persistence | Handled by the operating system |
| Per-site override | Not possible |
| Works with server-rendered or static pages | Yes, with no extra work |
The absence of a flash is a real advantage. Approaches that depend on a class set by a script need extra care to avoid showing the wrong theme for a moment. That problem is covered in a later chapter. The media strategy sidesteps it entirely.
The main limitation is that a visitor cannot choose a theme that differs from the system for your site alone. A person who runs a dark system but wants to read your documentation in light mode has no way to do so. If your product needs an in-page toggle, you need the class or selector strategy in the next chapter.
Edge cases and common mistakes
- Forgetting the light styles.
dark:bg-gray-900alone gives you a dark background in dark mode and the browser default (usually transparent over white) in light mode. Write the light utility first, then add thedark:override. - Only changing some properties. If you set
dark:bg-gray-900but leave the text color alone, the text keeps its light-mode color and may become unreadable on the dark background. Every element you recolor needs a matching text, border and, where used, ring and shadow color. - Expecting
dark:to apply because adarkclass is on<html>. With the default strategy the class is irrelevant. Tailwind only reads the media query. This is the most common reason that copied toggle code appears to do nothing. The fix is the strategy change in the next chapter. - Testing only one theme. Developers who work in dark mode often never see the light version. Use emulation to check both.
- Printing. Printers do not report a dark preference, so printed pages use light styles. Browsers generally render print with the
printmedia type, andprefers-color-schemethere resolves tolight. This is usually what you want. - Forced colors and high-contrast modes. Operating-system high-contrast modes can override your colors with a fixed palette, regardless of your
dark:utilities. Do not rely on color alone to convey state. - Images and embeds. The variant only changes CSS. A logo with a dark wordmark on a transparent background will vanish on a dark page. Provide a second asset, or use a technique such as showing and hiding two images with
dark:hiddenandhidden dark:block.
<img src="/logo-dark-text.svg" alt="Acme" class="block dark:hidden" />
<img src="/logo-light-text.svg" alt="Acme" class="hidden dark:block" />The pair above shows exactly one image at a time. Both files are still requested by the browser in most cases, so keep them small.
Summary of the working model
With no configuration, dark:foo means "apply foo when @media (prefers-color-scheme: dark) matches". The browser decides the value from the operating system, a browser override or a DevTools emulation. Tailwind generates ordinary CSS and has no runtime. You gain zero-setup behavior with no flash, and you give up any per-site choice. The next chapter shows how to take that choice back.
Exercise
A teammate adds a dark class to the <html> element with a button, and expects dark:bg-gray-900 to take effect. The project uses Tailwind v4 with only @import "tailwindcss"; in its stylesheet. Nothing changes when the button is clicked, but the page does turn dark when the operating system is set to dark. Explain why, and name the property of the environment that currently controls dark:.
Show answer
In the default configuration, dark: compiles to @media (prefers-color-scheme: dark), so the dark class on <html> is never consulted. The only thing that controls the variant is the browser's reported prefers-color-scheme value, which comes from the operating system, a browser override or DevTools emulation. That is why the page darkens when the OS is dark and ignores the button. To make the class work, the dark variant must be redefined in CSS, which a later chapter covers.
Exercise
Write the JavaScript that logs "dark" or "light" now and again every time the user changes the system appearance while your page is open.
Show answer
Create a media query list with window.matchMedia("(prefers-color-scheme: dark)"), define a function that logs query.matches ? "dark" : "light", call it once, and register it with query.addEventListener("change", fn). The change event fires when the system preference flips.