Lesson 46 of 50
cssCSS for Dark Mode & Theming
9 mins
Course progress92%
What you'll learn
Keep this idea in mind as you work through the lesson.
Lesson 46 of 50
css9 mins
Keep this idea in mind as you work through the lesson.
Leverage prefers-color-scheme and CSS variables to create automatic dark‑mode themes.
:root {
--bg:#fff; --text:#111; --primary:#0066cc;
}
@media (prefers-color-scheme: dark) {
:root { --bg:#111; --text:#eee; --primary:#4a90e2; }
}
body { background:var(--bg); color:var(--text); }
document.getElementById('themeBtn').addEventListener('click',()=>{
document.documentElement.dataset.theme =
document.documentElement.dataset.theme === 'dark' ? 'light' : 'dark';
});
[data-theme="dark"] { --bg:#111; --text:#eee; }