×

css · level 3 · unit 1 · lesson 4

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

A is a set of token values you swap all at once.

Write your components so they never name a colour, only tokens like var(--surface) and var(--text). A theme is a full set of values for those tokens. Swap the set and the whole page changes, without touching one component rule.

One way is a class: .theme-dark sets dark values, and everything inside follows. Another is the visitor's own setting: applies when their device is set to dark mode.

Many sites use both: the device setting by default, plus a class the visitor can switch with a button.

<div class="card">Light card</div>
<div class="theme-dark">
  <div class="card">Dark card</div>
</div>
:root {
  --surface: white;
  --text: #111827;
}
.theme-dark {
  --surface: #111827;
  --text: #f9fafb;
}
.card {
  padding: 16px;
  color: var(--text);
  background-color: var(--surface);
}

Press show my page to see it here.

To follow the device instead, put the dark values in @media (prefers-color-scheme: dark) around a :root rule.