Dark Mode Configuration
You'll learn how to set up and control dark mode in Tailwind CSS using built-in configuration strategies.
What Is Dark Mode Configuration?
Dark mode isn't just slapping dark variants on every element—it starts with configuring your project to use one of Tailwind's built-in detection strategies. This tells Tailwind how to know when dark mode should activate, and unlocks all those dark: variants you've been using.
The Two Strategies
Tailwind offers two approaches, set in your tailwind.config.js file under darkMode:
1. Media Query Strategy ('media')
module.exports = {
darkMode: 'media',
// ... rest of config
}
This respects the user's system preference (OS-level dark mode setting). The browser automatically applies dark styles when the user's device is in dark mode. No JavaScript needed—it works through the prefers-color-scheme media query behind the scenes.
Think of it like automatic headlights: the system decides when it's dark.
2. Class Strategy ('class')
module.exports = {
darkMode: 'class',
// ... rest of config
}
This gives you manual control. Dark variants only activate when a dark class exists on the <html> or <body> element. You toggle this class with JavaScript, letting users switch modes with a button regardless of system preference.
Think of it like manual headlights: you control the switch.
Which Should You Use?
- Use
'media'for automatic, system-respecting behavior - Use
'class'when you want user-controlled toggles or need to persist preferences in localStorage
Key Takeaway: Dark mode configuration in Tailwind's config file determines whether your dark: variants respond to system preferences ('media') or a manually toggled class ('class')—choose based on how much control you want.