🌙 What is Dark Mode 2.0 and Why Should You Care?
Dark mode isn’t just a trend anymore, it’s a user expectation. Today’s users want websites that can adapt to their device’s system theme preferences instantly, while also allowing manual theme toggles for personal control.
Why I think it’s awesome:
- Improves readability in low-light environments
- Saves battery on OLED/AMOLED screens
- Respects user’s OS or browser preferences
- Makes your site feel more modern and user-friendly
Prerequisites
Before you start, make sure you have:
- Basic knowledge of HTML and CSS
- A modern browser (Chrome, Firefox, Edge, Safari)
- A code editor like
VS Code
Step-by-Step Instructions
1. Setup CSS Variables for Theming
Instead of hardcoding colors, define them as CSS variables:
:root {
--bg-color: #ffffff;
--text-color: #000000;
--accent-color: #0077ff;
}
[data-theme="dark"] {
--bg-color: #121212;
--text-color: #ffffff;
--accent-color: #66ccff;
}
Why this is useful:
- Easy to maintain and update
- You can add more themes later without touching every CSS rule
2. Detect System Theme Preferences
Use the prefers-color-scheme
media query to apply dark mode automatically:
@media (prefers-color-scheme: dark) {
:root {
--bg-color: #121212;
--text-color: #ffffff;
--accent-color: #66ccff;
}
}
This means:
- If the user’s OS is in dark mode, your site will match it — no JavaScript needed.
3. Add a Manual Theme Toggle
HTML:
<button id="theme-toggle">Toggle Theme</button>
JavaScript:
const toggleBtn = document.getElementById('theme-toggle');
const currentTheme = localStorage.getItem('theme');
if (currentTheme) {
document.documentElement.setAttribute('data-theme', currentTheme);
}
toggleBtn.addEventListener('click', () => {
let theme = document.documentElement.getAttribute('data-theme') === 'dark' ? 'light' : 'dark';
document.documentElement.setAttribute('data-theme', theme);
localStorage.setItem('theme', theme);
});
Why this rocks:
- Users can override system settings if they prefer
- Preferences are saved between visits
4. Add Smooth Transitions
Prevent the theme change from being jarring:
html {
transition: background-color 0.3s ease, color 0.3s ease;
}
body {
background-color: var(--bg-color);
color: var(--text-color);
}
5. Support More Than Two Themes
You can easily add high-contrast or brand-specific themes:
[data-theme="high-contrast"] {
--bg-color: #000000;
--text-color: #ffff00;
--accent-color: #ff0000;
}
6. Best Practices
- Test your themes on multiple devices
- Use accessible color contrasts (check with Contrast Checker)
- Avoid pure black (#000) for better readability
- Always respect user preferences
Useful Steps Recap
- Create CSS variables for all colors
- Detect
prefers-color-scheme
for automatic theming - Add a JavaScript toggle for manual control
- Use smooth transitions for a better experience
- Extend with multiple theme options if needed
Thanks for reading!