Dark Mode Best Practices
How to implement dark mode that users will love, with accessibility in mind.
Dark mode has become a must-have feature for modern websites. Here’s how to implement it properly.
Why Dark Mode?
Users love dark mode for several reasons:
- Reduced eye strain in low-light conditions
- Battery savings on OLED screens
- Personal preference - some just prefer it
- Accessibility for light-sensitive users
Implementation Strategies
1. CSS Custom Properties
Use CSS variables for easy theme switching:
:root {
--bg-color: #ffffff;
--text-color: #1a1a1a;
}
.dark {
--bg-color: #0f172a;
--text-color: #f1f5f9;
}
body {
background: var(--bg-color);
color: var(--text-color);
}
2. Tailwind Dark Mode
Tailwind makes it easy with the dark: prefix:
<div class="bg-white dark:bg-slate-900">
<p class="text-gray-900 dark:text-gray-100">
Content
</p>
</div>
Respecting User Preferences
Always check the system preference first:
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
document.documentElement.classList.add('dark');
}
Persistence
Save the user’s choice:
// Save preference
localStorage.setItem('theme', 'dark');
// Load preference
const theme = localStorage.getItem('theme');
Accessibility Considerations
Contrast Ratios
| Element | Light Mode | Dark Mode |
|---|---|---|
| Body text | 12.6:1 | 13.1:1 |
| Links | 8.2:1 | 7.9:1 |
| Muted text | 4.6:1 | 4.8:1 |
Common Mistakes
Don’t Just Invert Colors
Simply inverting colors creates poor results:
- Images look wrong
- Brand colors get distorted
- Contrast may become too harsh
Do Create a Proper Palette
Design intentional dark mode colors:
/* Light mode */
--primary: #4f46e5;
/* Dark mode - slightly brighter for visibility */
--primary: #818cf8;
Smooth Transitions
Add transitions for a polished feel:
body {
transition: background-color 0.3s ease, color 0.3s ease;
}
Testing
Test your dark mode thoroughly:
- Check all pages in both modes
- Verify image visibility
- Test form elements
- Validate contrast ratios
- Test the toggle mechanism
Conclusion
Dark mode is more than an aesthetic choice—it’s about user comfort and accessibility. Take the time to implement it properly, and your users will thank you.