Course contentsShow
← Web Development
Lesson 640 of 2,98211. Utility-First CSS with TailwindFree lesson

Dark Mode Variants

Implementing dark mode with the dark: prefix and configuring class-based or media-query-based dark mode strategies.

Dark Mode Variants

You'll learn how to make your Tailwind utilities respond to dark mode using the dark: prefix.

The Core Idea

Dark mode variants in Tailwind CSS let you apply different styles when a user's system (or your site) is in dark mode. Just like you've used hover: or focus: to change styles on interaction, the dark: prefix lets you change styles based on color scheme preference.

Think of it like having two complete wardrobes for your website—one for daytime, one for nighttime. The dark: prefix lets you define the nighttime outfit right alongside the daytime one.

How It Works

By default, you write regular utilities for light mode, then prefix dark mode alternatives with dark::

<div class="bg-white text-gray-900 dark:bg-gray-900 dark:text-white">
  This adjusts automatically in dark mode!
</div>

Two Configuration Strategies

In your tailwind.config.js, you choose how dark mode is triggered:

Media Query Strategy (default):

darkMode: 'media'

Responds automatically to the user's system preference (prefers-color-scheme: dark).

Class-Based Strategy:

darkMode: 'class'

Only activates when a dark class exists on your <html> or <body> element. This gives you manual control—perfect for toggle buttons.

With class-based mode, you'd add JavaScript to toggle the class based on user preference or a switch they control.

When to Use Which

Use media for automatic system respect. Use class when you want users to override their system setting with an in-app toggle.

Key Takeaway: The dark: variant lets you define dark mode styles inline with light mode styles, and your config file determines whether it responds to system settings or a manual class toggle.