Full answer
A theme is a set of design tokens, such as text colour and background, kept in custom properties. Components use var(--bg) and var(--text) and never hard-code colours. Switching the theme means redefining those variables in one place.
Three pieces work together:
prefers-color-schemeis a media feature that tells you the operating system or browser preference (lightordark). Use it to define dark values by default.color-scheme: light darkon:roottells the browser the page supports both, so built-in parts (form controls, scrollbars, default page background) also switch.- A manual override. Users often want to pick a theme independent of the system. Store the choice (for example in
localStorage) and write it as an attribute such asdata-theme="dark"onhtml. Your CSS then follows the attribute first and the system preference second.
To avoid a flash of the wrong theme on load, set the attribute from a tiny inline script in the head, before the first paint.
With a light system setting, the page shows dark text on white. With a dark system setting it switches to light text on a dark background. If data-theme="light" is set, the page stays light even on a dark system, and data-theme="dark" forces dark on a light system. A theme toggle only needs to set the attribute and save the choice.
Check colour contrast in both themes, not only the default. Shadows, images with white backgrounds and borders often need dark variants. A newer CSS function, light-dark(), can write both values on one line, but check support before depending on it.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Theme demo</title>
<script>
const saved = localStorage.getItem("theme");
if (saved) document.documentElement.dataset.theme = saved;
</script>
<style>
:root { color-scheme: light dark; --bg: #ffffff; --text: #1a1a1a; --accent: #0a6cff; }
@media (prefers-color-scheme: dark) {
:root:not([data-theme="light"]) { --bg: #121212; --text: #eeeeee; --accent: #7fb2ff; }
}
:root[data-theme="dark"] { color-scheme: dark; --bg: #121212; --text: #eeeeee; --accent: #7fb2ff; }
:root[data-theme="light"] { color-scheme: light; }
body { background: var(--bg); color: var(--text); font-family: sans-serif; }
a { color: var(--accent); }
</style>
</head>
<body>
<p>Hello, <a href="#" id="link">theme</a>.</p>
</body>
</html>This is a complete page. It was checked with the system scheme set to light and to dark, and with data-theme set to light and to dark.
Key points
- Use role-named tokens such as --bg and --text.
- prefers-color-scheme provides the system default.
- color-scheme: light dark themes scrollbars and form controls.
- A data-theme attribute on html supports a manual toggle.
- Set the saved choice before first paint to avoid a flash.
Common mistakes
- forgetting color-scheme. The page itself is themed, but the scrollbar, form controls and default text boxes stay light.
- reading the saved theme after the page renders, which flashes the wrong colours. Set it from an inline script in the head.
Follow-ups
- how do you read the system preference from JavaScript?
- How do you read the system colour scheme in JavaScript?
- What does light-dark() do, and what should you check before using it?
Tips
- Name tokens by role (--bg, --text, --border) and never by colour (--white). A dark theme then does not have to lie about names.
- Describe the three layers: tokens, system preference, manual override.
- Mention checking contrast in both themes.