Reading history

Dark Mode with CSS: prefers-color-scheme and a Theme Toggle

6 min read 0 views

Add dark mode to a website with CSS variables, prefers-color-scheme and color-scheme, build a manual light and dark toggle, and design a comfortable dark theme.

Most phones and computers now offer a dark theme, and many people use it at night or to save battery on OLED screens. A website that stays bright white can be uncomfortable for them. This lesson shows how to support dark mode with CSS: following the user’s system setting with prefers-color-scheme, organising colours with custom properties, and adding a manual theme switch.

In this lesson you will learn

  • Reading the system theme with prefers-color-scheme
  • Storing theme colours in CSS variables
  • Letting built-in controls adapt with color-scheme
  • A manual light/dark toggle with a data-theme attribute
  • Design tips for comfortable dark themes

You should know CSS variables and media queries.

Following the System Setting

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Dark mode with variables</title>
  <style>
    :root {
      color-scheme: light dark;             /* form controls and scrollbars adapt too */
      --bg: #ffffff; --text: #1f2933; --card: #f1f5f9; --accent: #0b6bcb;
    }
    @media (prefers-color-scheme: dark) {
      :root { --bg: #111827; --text: #e5e7eb; --card: #1f2937; --accent: #60a5fa; }
    }
    body { background: var(--bg); color: var(--text); font-family: sans-serif; margin: 12px; font-size: 14px; }
    .card { background: var(--card); padding: 10px; border-radius: 8px; }
    a { color: var(--accent); }
  </style>
</head>
<body>
  <div class="card">
    <h3>Follows your system setting</h3>
    <p>Switch your phone or computer to dark mode and this card changes. <a href="#">A link</a></p>
    <input placeholder="Inputs adapt too">
  </div>
</body>
</html>

Result

  • All colours live in variables on :root. The rest of the stylesheet uses only var(--bg), var(--text) and so on, never fixed colours.
  • @media (prefers-color-scheme: dark) matches when the device is in dark mode. Inside it, only the variable values change, so one small block re-themes the whole page.
  • color-scheme: light dark tells the browser the page supports both, so it also darkens built-in parts it draws itself: text inputs, scrollbars and checkboxes.
  • Switch your device to dark mode and reload this page: the demo follows it. You can also test in Chrome DevTools under Rendering → Emulate CSS media feature prefers-color-scheme.

A Manual Toggle

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Theme toggle</title>
  <style>
    :root { --bg: #fffdf7; --text: #222; --card: #fff3cd; }
    [data-theme="dark"] { --bg: #0f172a; --text: #f1f5f9; --card: #1e293b; }
    body { background: var(--bg); color: var(--text); font-family: sans-serif; margin: 12px;
           font-size: 14px; transition: background 0.3s, color 0.3s; }
    .card { background: var(--card); padding: 10px; border-radius: 8px; }
    button { padding: 6px 12px; cursor: pointer; }
  </style>
</head>
<body>
  <div class="card">
    <p>Choose a theme yourself:</p>
    <button id="toggle" aria-pressed="false">Dark mode</button>
  </div>
  <script>
    var root = document.documentElement;
    var btn = document.getElementById("toggle");
    btn.addEventListener("click", function () {
      var dark = root.getAttribute("data-theme") !== "dark";
      root.setAttribute("data-theme", dark ? "dark" : "light");
      btn.textContent = dark ? "Light mode" : "Dark mode";
      btn.setAttribute("aria-pressed", String(dark));
      // A real site would also save the choice: localStorage.setItem("theme", ...)
    });
  </script>
</body>
</html>

Result

  • The dark colours are attached to [data-theme="dark"] instead of a media query. JavaScript sets that attribute on the <html> element when the button is clicked.
  • aria-pressed tells screen reader users whether the toggle is currently on.
  • The transition on background and color makes the switch gentle.
  • On a real site, save the choice with localStorage and apply it in a small script in the <head>, before the page is drawn, to avoid a white flash on every visit.
  • A good pattern combines both: follow the system theme by default and let the user override it.

Designing a Good Dark Theme

DoAvoid
Dark grey or navy backgrounds such as #111827Pure black with pure white text, which glares and smears on some screens
Slightly lighter surfaces for raised cardsRelying on shadows, which barely show on dark backgrounds
Lighter, less saturated accent coloursBright, saturated colours that vibrate against dark grey
Checking contrast for both themesAssuming contrast that passes in light mode also passes in dark

Images may also need care: a logo with dark text on a transparent background disappears on a dark page. Use <picture> with media="(prefers-color-scheme: dark)" to swap in a light version, or give the logo a light background.

The light-dark() Function

Newer browsers support a shortcut: color: light-dark(#1f2933, #e5e7eb); uses the first colour in light mode and the second in dark mode. It requires color-scheme: light dark on the element or an ancestor. It has worked in all major browsers since 2024, but the variable approach above remains the most flexible.

Common Mistakes

  1. Hard-coding colours throughout the stylesheet, so the dark theme must override hundreds of rules.
  2. Forgetting color-scheme, leaving bright white inputs on a dark page.
  3. Not saving the user’s choice, so it resets on every page.
  4. Testing only one theme and missing unreadable text in the other.

Practice Exercises

Exercise 1. Add a --border variable that is #d0d7de in light mode and #30363d in dark mode, and use it on .card.

Show answer
:root { --border: #d0d7de; }
@media (prefers-color-scheme: dark) { :root { --border: #30363d; } }
.card { border: 1px solid var(--border); }

Exercise 2. Write the JavaScript line that saves the current theme and the line that reads it back.

Show answer
localStorage.setItem("theme", dark ? "dark" : "light");
var saved = localStorage.getItem("theme");   // "dark", "light" or null

Frequently Asked Questions

Does dark mode really save battery?

On OLED and AMOLED screens, used in many phones, dark pixels use less power, so dark themes can save a noticeable amount. On ordinary LCD screens the backlight stays on, so the saving is very small.

Can I make my site dark only?

You can, but some people find light text on dark backgrounds harder to read for long periods. Offering both themes suits the most visitors.

Summary

  • Keep all colours in CSS variables and change only the variables per theme.
  • prefers-color-scheme: dark follows the device setting.
  • color-scheme: light dark adapts built-in controls.
  • A data-theme attribute plus localStorage gives users a choice that persists.

Next: Project: Build a School Admin Dashboard.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *