DEV Community

Sohana Akbar
Sohana Akbar

Posted on

How to Add Dark Mode to Any Website (Even If You’re Not a CSS Wizard)

Introduction
Dark mode isn’t just a trend anymore—it’s an expectation. From Windows and macOS to iOS and Android, users want the option to switch from a blinding white background to a sleek, eye-friendly dark interface.

But here’s the good news: You don’t need to redesign your entire website. With a few smart CSS rules and a sprinkle of JavaScript, you can add dark mode to any website in under 30 minutes.

In this article, I’ll show you three methods—from quick-and-dirty to fully professional.

Method 1: The “Browser Native” Approach (Easiest)
Modern browsers now support prefers-color-scheme, a CSS media query that detects if a user has dark mode enabled in their OS settings.

Step 1: Add Dark CSS Variables
css
/* Light mode (default) */
:root {
--bg: #ffffff;
--text: #111111;
--accent: #0077cc;
}

/* Dark mode automatic */
@media (prefers-color-scheme: dark) {
:root {
--bg: #121212;
--text: #eeeeee;
--accent: #66ccff;
}
}

body {
background: var(--bg);
color: var(--text);
}
Pros: Zero JavaScript, respects system preferences.
Cons: No manual toggle for users.

Method 2: Manual Toggle with JavaScript (Most Flexible)
This gives users a button to switch modes, overriding their system preference.

Step 1: HTML Toggle Button
html
🌓 Dark Mode
Step 2: CSS with a .dark-mode Class
css
:root {
--bg: #ffffff;
--text: #111111;
}

.dark-mode {
--bg: #121212;
--text: #eeeeee;
}

body {
background: var(--bg);
color: var(--text);
transition: background 0.3s, color 0.2s;
}
Step 3: JavaScript to Save Preference
javascript
const toggle = document.getElementById('darkModeToggle');
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;

// Check localStorage or system preference
if (localStorage.getItem('theme') === 'dark' || (!localStorage.getItem('theme') && prefersDark)) {
document.body.classList.add('dark-mode');
}

toggle.addEventListener('click', () => {
document.body.classList.toggle('dark-mode');
const theme = document.body.classList.contains('dark-mode') ? 'dark' : 'light';
localStorage.setItem('theme', theme);
});
Pros: Full user control, persistent choice.
Cons: Requires a bit of JS.

Method 3: For Legacy Websites (Overriding Inline Styles)
If you’re dealing with messy third-party code or inline styles, use an inverted filter trick (use sparingly).

css
.dark-mode {
filter: invert(1) hue-rotate(180deg);
}
How it works: Inverts all colors, then fixes hue shifts. This makes white backgrounds black and black text white.

Warning: Images and videos will also invert. You’ll need to un-invert them:

css
.dark-mode img,
.dark-mode video {
filter: invert(1) hue-rotate(180deg);
}
Only use this as a temporary fix or for simple sites.

Pro Tips for a High-Quality Dark Mode
Don’t use pure black (#000000) – It creates harsh contrast. Use #121212 or #1e1e1e.

Reduce shadows – Dark mode looks cleaner with less shadow depth.

Test all interactive elements – Buttons, links, and inputs must remain accessible.

Toggle system preference + manual override – Best of both worlds:

javascript
// Listen to system changes
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', (e) => {
if (!localStorage.getItem('theme')) {
document.body.classList.toggle('dark-mode', e.matches);
}
});
Real-World Example: Adding Dark Mode to a Blog in 5 Minutes
Let’s say you have a basic WordPress or static HTML site.

Open your CSS file.

Define CSS variables for colors.

Add the .dark-mode class overrides.

Insert the JavaScript above.

Add a floating button with position: fixed.

That’s it. Your blog now has dark mode.

Conclusion
Dark mode is no longer optional—it’s a mark of a modern, user-respecting website. You don’t need a full rewrite. Start with CSS variables, add a JavaScript toggle, and store the user’s preference in localStorage.

Whether you manage a personal blog, a corporate site, or a web app, these three methods work everywhere.

Your users will thank you—especially those coding at 2 AM.

Ready to go dark?
Try the code snippets above today. Then ask yourself: Why didn’t I do this sooner?

Let me know if you’d like a ready-to-copy HTML/CSS/JS template for this.

Top comments (0)