Dark mode is more than just a trend โ it's a user-friendly feature that improves readability and reduces eye strain. The best part? Implementing dark mode can be super simple using CSS variables and a pinch of JavaScript.
Letโs build a light/dark theme toggle in under 5 minutes!
๐ง Why Use :root and CSS Variables?
Using CSS variables (also called custom properties) allows you to:
Define your color scheme in one place
Easily switch themes dynamically
Avoid repeating values across your stylesheets
โ๏ธ Step 1: Setup Your HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dark Mode Demo</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h1>Dark Mode in 5 Minutes</h1>
<button id="toggle-theme">Toggle Theme</button>
</div>
<script src="script.js"></script>
</body>
</html>
๐จ Step 2: Create Light & Dark Themes Using :root
/* styles.css */
:root {
--bg-color: #ffffff;
--text-color: #1a1a1a;
--button-bg: #e2e8f0;
--button-text: #000000;
}
[data-theme="dark"] {
--bg-color: #1a1a1a;
--text-color: #f5f5f5;
--button-bg: #2d3748;
--button-text: #ffffff;
}
body {
background-color: var(--bg-color);
color: var(--text-color);
font-family: sans-serif;
transition: all 0.3s ease;
}
button {
padding: 10px 20px;
margin-top: 20px;
background-color: var(--button-bg);
color: var(--button-text);
border: none;
cursor: pointer;
border-radius: 6px;
}
๐ก Step 3: Add JavaScript to Toggle Themes
// script.js
const toggleBtn = document.getElementById("toggle-theme");
toggleBtn.addEventListener("click", () => {
const currentTheme = document.documentElement.getAttribute("data-theme");
if (currentTheme === "dark") {
document.documentElement.removeAttribute("data-theme");
localStorage.setItem("theme", "light");
} else {
document.documentElement.setAttribute("data-theme", "dark");
localStorage.setItem("theme", "dark");
}
});
// Optional: Remember userโs preference
window.addEventListener("DOMContentLoaded", () => {
const savedTheme = localStorage.getItem("theme");
if (savedTheme === "dark") {
document.documentElement.setAttribute("data-theme", "dark");
}
});
โ Done! Now Test It
Click the "Toggle Theme" button to switch between light and dark themes. Your site will:
Animate smoothly between modes
Remember the user's last preference (thanks to localStorage)
Stay maintainable by using variables ๐
๐งช Bonus: Add More Theming Flexibility
Want to go beyond dark mode? You can add multiple themes or responsive theme changes using the same concept!
๐ฌ Whatโs Your Favorite Dark Mode Trick?
Let me know in the comments if you've built a dark mode toggle differently โ or share a link to your own themed site!
Top comments (6)
This is great! Iโve used a similar approach in my portfolio โ just havenโt implemented localStorage yet. Sounds like a solid idea!
We could even improve it further by fetching the system theme as the default ๐. I was actually thinking of adding that too. Great article, as always! ๐
Thank you so much, Madhurima! ๐
Yes, adding localStorage really helps with preserving user preference โ definitely a small touch that improves UX.
And you're absolutely right โ using the system's preferred theme as the default would make it even smoother.
Let me know when you implement it โ would love to check it out!
pretty cool honestly, i always end up messing up the variable names when i try this - you think there's ever a time dark mode doesn't actually make things better
Thanks, Nevo! ๐
Haha, totally get the variable name chaos โ Iโve been there too! I started using a naming convention like --clr-bg, --clr-text, etc., and grouping them in comments โ really helps keep things tidy.
As for dark mode not always being better โ yes, absolutely! Some situations come to mind:
Daylight viewing: On bright screens or outdoors, light mode can actually improve readability.
Branding: For some brands (especially minimalist or clinical designs), light mode communicates the feel better.
Accessibility: High contrast themes can sometimes be more effective than a typical dark palette for visually impaired users.
Itโs all about giving users the option โ thatโs where the real magic is ๐ฅ
That's very practical.
Let's share our issues together.
Keep in touch. jaylee518.watcher@gmail.com
Thanks a lot, Riku! ๐
Really glad you found it practical.
Letโs definitely stay connected and share knowledge! You can find me on LinkedIn here: linkedin.com/in/preeti-yadav5443/
Looking forward to staying in touch! ๐