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 theprefers-color-scheme media query to detect dark mode:
Selector Strategy (Class-based)
For manual dark mode toggling, use the selector strategy by adding adark class to your HTML:
.dark class on a parent element:
:where() pseudo-class for specificity control:
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
- Background & Text
- Borders
- Shadows
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:
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.