Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 11.3 — Theming and Dark Mode
CSS · Example 3 of 3

Follow the system setting

Change your operating system's appearance setting (or use DevTools: Rendering, then "Emulate CSS media feature prefers-color-scheme") and the box and message update without a reload.

A theme is a set of custom properties (colour tokens) that every component reads; dark mode swaps those values, color-scheme tells the browser to draw its own parts dark too, light-dark() picks a value per scheme, and prefers-color-scheme follows the user's system setting.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write tokens for a light and a dark theme with light-dark() and use them on body and on .card (which should be a slightly different surface colour).

02

Write CSS so a single <aside class="promo"> is always dark, even on a light page, including its form controls.

03

Write the JavaScript for a toggle button #theme that switches data-theme on <html> between light and dark and saves it.

Explain it without notes

01

How would you structure the CSS for a site that supports light and dark themes?

02

What does the color-scheme property do, and why is it needed even if you write a dark theme yourself?

03

What does light-dark() do, and what does it depend on?

04

How do you build a theme toggle that remembers the user's choice without a flash of the wrong theme?

Follow the system setting

Preview

Press Run to see the page.

Console

Nothing logged yet.