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 Basics to Advanced lesson | BraveProgrammer
  1. Courses
  2. /
  3. CSS Basics to Advanced

Lesson 46 of 50

·css

CSS for Dark Mode & Theming

9 mins

Course progress92%

What you'll learn

Keep these ideas in mind as you work through the lesson.

3 points
  • `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.

Dark mode isn’t just a colour swap – it requires a thoughtful palette, contrast checks, and a strategy for user‑initiated toggles.

System‑Preferred Dark Mode (prefers-color-scheme)

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

Manual Toggle – Data Attribute Approach

<button id="themeBtn">Toggle dark</button>
const btn = document.getElementById('themeBtn');
btn.addEventListener('click',()=>{
  document.documentElement.dataset.theme =
    document.documentElement.dataset.theme === 'dark' ? 'light' : 'dark';
});
[data-theme="dark"] {
  --bg:#111; --text:#eee; --primary:#4a90e2;
}

Changing the attribute swaps the entire colour palette instantly.


Designing a Robust Dark Palette

  1. Neutral backgrounds – avoid pure black (#000) – a dark gray (#111) reduces eye strain.
  2. Elevated surfaces – use slightly lighter shades for cards (#1e1e1e).
  3. Accent contrast – ensure accent colours still meet WCAG 2.1 AA against the dark background.
  4. Images – provide dark‑mode‑friendly versions (image-set() with a dark variant) or use CSS filters (filter:invert(0.9) hue-rotate(180deg)).

Example – Dark‑Mode Image Swap

.logo {
  background:url('logo-light.svg') center/contain no-repeat;
}
@media (prefers-color-scheme: dark) {
  .logo { background-image:url('logo-dark.svg'); }
}

Storing User Preference

Persist the choice in localStorage and apply it on page load:

const saved = localStorage.getItem('theme');
if (saved) document.documentElement.dataset.theme = saved;

btn.addEventListener('click',()=>{
  const newTheme = document.documentElement.dataset.theme === 'dark' ? 'light' : 'dark';
  document.documentElement.dataset.theme = newTheme;
  localStorage.setItem('theme', newTheme);
});

Accessibility Checklist

  • Verify contrast in both light & dark themes (≥ 4.5:1 for normal text).
  • Ensure focus rings remain visible (they should use outline that adapts to the theme).
  • Provide a non‑CSS toggle for users whose OS does not expose prefers-color-scheme.

Testing Tools

  • Chrome DevTools → Toggle dark mode under Rendering tab.
  • aXe and Lighthouse will flag low contrast in either theme.

Pro Tip – Use CSS Custom Properties for Every Colour

Manipulating a single variable (--primary) updates the whole UI without rewriting each rule.

Browser Support

prefers-color-scheme is supported in Chrome 76+, Safari 12.1+, Edge 79+, Firefox 67+. Older browsers fall back to the default light theme.

Make it stickPractice what you've learned1 quiz · 6 practical tests · Optional activities from CSS Basics to AdvancedBrowse activities
Previous lessonLogical CSS for InternationalizationContinue to lesson 47CSS Grid + Flexbox Hybrid LayoutsBack to CSS Basics to Advanced curriculum