Skip to main content
Tailwind includes a dark variant that lets you style your site differently when dark mode is enabled.

Dark Mode Strategies

Tailwind supports multiple strategies for implementing dark mode. Choose the one that best fits your project’s needs.

Media Query Strategy (Default)

By default, Tailwind uses the prefers-color-scheme media query to detect dark mode:
This generates CSS like:
From the source code, the media strategy is implemented as:

Selector Strategy (Class-based)

For manual dark mode toggling, use the selector strategy by adding a dark class to your HTML:
Now dark mode is controlled by a .dark class on a parent element:
The selector strategy uses the :where() pseudo-class for specificity control:
This generates CSS like:

Custom Selector

Customize the dark mode selector:

Variant Strategy

For complete control, use the variant strategy with a custom selector pattern:
When using the variant strategy, you must include & in your selector to indicate where the utility’s selector should be inserted.

Common Dark Mode Patterns

Color Schemes

Form Elements

Images & Icons

Gradients

Combining with Other Variants

Dark mode works seamlessly with all other Tailwind variants:

Hover States

Focus States

Responsive Design

Group Hover

Toggling Dark Mode

When using the selector strategy, implement dark mode toggling with JavaScript:

Avoiding Flash of Unstyled Content

When using the selector strategy, add this script to your <head> before any other content to prevent a flash of the wrong theme:
This script must run synchronously in the <head> before your content renders. Don’t defer or async it.

Best Practices

Design System Consistency

Define semantic color variables that work in both modes:

Accessibility

Ensure sufficient contrast in both modes:

Test Both Modes

Always test your UI in both light and dark modes to ensure:
  • Sufficient color contrast
  • Readable text
  • Visible borders and dividers
  • Proper image handling
  • Correct icon colors
Use browser DevTools to toggle between light and dark mode preferences for quick testing.