Emmanuel Akinfulubi

HTML and CSSPractical III

Dark mode with CSS custom properties

36 Two themes from one set of variables

hard

Theme a panel with custom properties, then swap every colour for a dark scheme by redeclaring the variables only. Let the browser style form controls to match.

Stuck? Three hints, smallest first
  1. 1. Declare the tokens on :root, then reference them everywhere. No component names a colour directly.
  2. 2. @media (prefers-color-scheme: dark) redeclares the same tokens with different values.
  3. 3. color-scheme: light dark tells the browser to theme scrollbars and native controls too.

index.html

<div class="panel">
  <h3>Fare summary</h3>
  <p class="muted">Lagos to Abuja, one adult</p>
  <p class="total">&#8358;18,000</p>
  <label for="promo">Promo code</label>
  <input id="promo" name="promo" type="text">
</div>

style.css

:root {
  color-scheme: light dark;   /* native controls and scrollbars follow suit */
  --paper: #ffffff;
  --ink: #17171a;
  --muted: #5c5c66;
  --line: #c9cad1;
  --brand: #3055d8;
}

@media (prefers-color-scheme: dark) {
  :root {
    --paper: #101013;
    --ink: #ececf1;
    --muted: #a1a1ad;
    --line: #35353e;
    --brand: #8ba4ff;
  }
}

.panel {
  max-width: 260px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--paper);
  color: var(--ink);
  font-size: 0.85rem;
}

.panel h3 { margin: 0 0 4px; font-size: 0.95rem; }

.muted { margin: 0 0 8px; color: var(--muted); }

.total { margin: 0 0 12px; font-size: 1.2rem; font-weight: 700; color: var(--brand); }

.panel label { display: block; margin-bottom: 4px; font-weight: 600; }

.panel input {
  width: 100%;
  box-sizing: border-box;
  padding: 7px 9px;
  border: 1px solid var(--line);
  border-radius: 6px;
  font: inherit;
  /* No colours named here: the browser applies the scheme. */
}

.panel input:focus-visible {
  outline: 2px solid var(--brand);
  outline-offset: 1px;
}
Rendered result. Shown, not machine-checked: there is no browser in the build.
8 checks that prove this answer

Assertions, run in the browser. The markup and styles above are handed to them as html and css, so press Run and each check prints ok. The build fails if a single one fails.

Checks for exercise 36

// Both themes must define the same tokens, or something falls back to nothing.
const block = (re) => (re.exec(css) || [, ''])[1];
const light = block(/^:root \{([\s\S]*?)\n\}/m);
const darkQuery = block(/@media \(prefers-color-scheme: dark\)\s*\{([\s\S]*?)\n\}/);
const names = (s) => [...s.matchAll(/(--[a-z-]+):/g)].map((m) => m[1]).sort();

test('the light theme declares five tokens', names(light).length, 5);
test('the dark theme redeclares exactly the same names', names(darkQuery), names(light));
test('and no more', names(darkQuery).length, names(light).length);
test('color-scheme is declared, so native controls follow', /color-scheme:\s*light dark/.test(css), true);
test('components reference tokens rather than naming colours', (css.match(/var\(--/g) || []).length, 7);
test('the dark override touches only :root', /^\s*:root \{/.test(darkQuery), true);
test('no component rule is repeated inside it', /\.panel/.test(darkQuery), false);
test('the input is labelled', /<label for="promo"/.test(html) && /id="promo"/.test(html), true);

Output


      

The discipline is that no component names a colour: every rule references a token, so the dark theme is nine lines that redeclare variables and nothing else. The seventh check enforces exactly that — a .panel rule inside the dark query would mean the theme had started to fork. Two easy wins alongside. color-scheme: light dark is what makes the browser theme scrollbars, the caret and unstyled controls, which is why the input needs no colours of its own. And a token missing from one theme does not error, it just inherits the wrong value, so comparing the two lists is worth doing.

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.