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

© 2026 BraveProgrammer. All rights reserved.

  1. Courses
  2. /
  3. CSS Basics to Advanced

Lesson 46 of 50 · css

CSS for Dark Mode & Theming

Duration: 9 mins

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; }
Previous: Logical CSS for InternationalizationNext: CSS Grid + Flexbox Hybrid Layouts