Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

CSS for Dark Mode & Theming | CSS Course lesson | BraveProgrammer
  1. Courses
  2. /
  3. CSS Course

Lesson 46 of 50

·css

CSS for Dark Mode & Theming

9 mins

Course progress92%

What you'll learn

Keep this idea in mind as you work through the lesson.

1 point
  • `prefers-color-scheme` respects the user’s OS setting. * Using CSS variables makes **runtime switching** trivial. * Provide sufficient contrast in both light and dark themes.

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); }

Example: Manual toggle with JavaScript

document.getElementById('themeBtn').addEventListener('click',()=>{
  document.documentElement.dataset.theme =
    document.documentElement.dataset.theme === 'dark' ? 'light' : 'dark';
});
[data-theme="dark"] { --bg:#111; --text:#eee; }
Make it stickPractice what you've learned1 quiz · 6 practical tests · Optional activities from CSS CourseBrowse activities
Previous lessonLogical CSS for InternationalizationContinue to lesson 47CSS Grid + Flexbox Hybrid LayoutsBack to CSS Course curriculum