# Dark Mode Strategy

> Implement responsive theme switching in Tailwind CSS v4 using the dark: variant modifier combined with class-based root triggers and system preference synchronization.

Subject: Tailwind CSS | Lesson 16 of 16 | 7 min read
Interactive lesson: https://codewithanimation.in/tailwind/dark-mode-strategy
Previous: [Dynamic Classes & tailwind-merge](https://codewithanimation.in/tailwind/dynamic-classes-and-merge.md)

## What Dark Mode Strategy in Tailwind CSS v4 Is

Dark mode strategy in Tailwind CSS v4 provides seamless theme adaptivity using the `dark:` variant modifier prefix (for example, `dark:bg-slate-900 dark:text-slate-100`).

Tailwind v4 supports both system preference strategy (`prefers-color-scheme`) and class-based selector strategy (`.dark` on `<html>` or `<body>`).

Using the class-based selector strategy enables user-controlled theme toggling (Light, Dark, System) while preserving user choices in `localStorage`.

## Why Class-Based Dark Mode Combined with Utilities Powers Great UX

Combining Tailwind's `dark:` modifier with class-based root toggling offers distinct architectural advantages:

1. Declarative Co-location: Light and dark color tokens reside directly side-by-side in markup (`bg-white dark:bg-slate-900`), avoiding split stylesheet maintenance.

2. User Preference Control: Relying solely on `prefers-color-scheme` media queries prevents users from toggling dark mode manually within the application. Class-based strategy supports explicit UI theme switchers.

3. Zero FOUT (Flash of Unstyled Text): Synchronizing the root `.dark` class inline before page render ensures instant theme application without color flashes during initial load.

## How to Configure and Implement Theme Switching in React

Follow these step-by-step instructions to set up dark mode in Tailwind v4 and React:

1. Configure CSS Dark Variant: In your main CSS file, configure the dark selector strategy if using manual toggling: `@variant dark (&:where(.dark, .dark *));`.

2. Pair Utility Tokens: Apply paired light and dark utilities on UI containers: `bg-slate-50 text-slate-900 dark:bg-slate-900 dark:text-slate-100 border-slate-200 dark:border-slate-800 transition-colors`.

3. Toggle Root Class in React: In your theme context or state hook, toggle `.dark` on `document.documentElement.classList`: `root.classList.toggle('dark', isDark)`.

## Common mistake: Forgetting Border and Sub-label Dark Variants

A frequent design flaw when implementing dark mode is adding `dark:bg-slate-900` and `dark:text-white` to main containers, but forgetting border and secondary text elements.

Light borders (`border-slate-200`) and sub-labels (`text-slate-600`) become glaring or unreadable on dark backgrounds if not explicitly paired with `dark:border-slate-800` and `dark:text-slate-300`.

Always audit cards, dividers, inputs, and captions across both theme modes.

## Tip: Smooth Theme Transitions with CSS transition-colors

Add `transition-colors duration-200` to top-level containers when toggling dark mode.

This applies a smooth, polished color crossfade across backgrounds, borders, and text colors when switching themes.

## Try it: Interactive Dark Mode Strategy Showcase

Experience live theme switching across light, dark, and system modes while observing how dark: variant utilities respond instantaneously.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/tailwind/dark-mode-strategy

## Check yourself

### Question 1

Why is the class-based dark mode selector strategy preferred over relying exclusively on prefers-color-scheme media queries?

- A. Class-based strategy allows users to explicitly toggle themes in application UI regardless of OS settings
- B. Media queries are unsupported in modern desktop web browsers
- C. Class-based strategy reduces compiled CSS file size by half

Correct answer: A

Class-based dark mode strategy enables application UI theme toggles (Light/Dark/System) that persist user choices independently of system OS settings.

- Why not B: prefers-color-scheme media queries are fully supported across all modern browsers.
- Why not C: Selector strategy modifies rule triggers, not stylesheet compilation size.

### Question 2

Which utility combination correctly styles a container card for both light and dark mode accessibility?

- A. bg-white text-slate-900 border-slate-200 dark:bg-slate-900 dark:text-slate-100 dark:border-slate-800
- B. bg-white text-slate-900 dark:bg-slate-900
- C. dark:bg-slate-900 dark:text-slate-100 without light mode utility defaults

Correct answer: A

Complete theme utility pairing explicitly defines light defaults alongside dark: variant overrides for background, text, and border colors.

- Why not B: Missing border and text dark variants leaves light borders and poor text contrast on dark backgrounds.
- Why not C: Omitting default light utilities causes fallback rendering to browser default transparent backgrounds.

### Question 3

How do production web applications prevent Flash of Unstyled Text (FOUT) or theme flicker on initial page render?

- A. Execute an inline blocking script in HTML <head> to read localStorage and add .dark to <html> before render
- B. Delay rendering the React application for 2 seconds using setTimeout
- C. Use CSS @keyframes animations on page load

Correct answer: A

An inline script in the document <head> evaluates theme preferences and toggles .dark on documentElement synchronously prior to DOM rendering, eliminating theme flash.

- Why not B: Delaying React rendering causes poor performance and layout jumps.
- Why not C: CSS animations do not check localStorage theme preference during initial document parse.

## Remember this

- Use dark: variant modifiers to declare dark theme overrides directly in markup alongside light defaults.
- Configure class-based selector strategy (@variant dark) to enable explicit UI theme switches.
- Always pair background, text, and border utilities for balanced contrast across both modes.
- Add transition-colors duration-200 to containers for smooth theme mode crossfades.
- Synchronize theme preference in document <head> inline script to prevent initial page theme flash.
