Sub topic 1
How Dark Mode Works on the Web
Dark mode on the web is a conversation between three parties. The user states a preference, the browser reports it, and your CSS decides what to do with it. This chapter lays out that model and shows a small working page. Later chapters cover each piece in detail.
Prerequisites
This chapter assumes you already know:
- Basic HTML and CSS, including selectors and how the cascade resolves conflicting rules.
- CSS color values such as hex (
#1a1a1a) andrgb(). - Enough JavaScript to read and set attributes on a DOM element.
You do not need to know custom properties or media queries in depth. Both are introduced as they appear.
What "dark mode" means on the web
A website has no dark mode of its own. Dark mode is a set of colors that you write and apply under certain conditions. The browser does not recolor your page. It gives you two things:
- A signal that says whether the user prefers a light or dark appearance.
- A way to tell it which color schemes your page supports, so it can style the parts you did not write, such as form controls and scrollbars.
Everything else is ordinary CSS. If you never write dark colors, the page stays light even when the user's whole system is dark.
Some browser extensions and a few mobile browsers force dark colors onto pages that do not support them. This is an automatic inversion or recoloring step. It is a fallback for sites that ignore dark mode, and its results vary. A site that handles dark mode itself controls its own result.
The three layers
It helps to separate a dark theme into three layers. Most bugs come from mixing them up.
| Layer | Question it answers | Main tools |
|---|---|---|
| Preference | What does the user want? | Operating system or browser setting, a saved choice on your site |
| Page colors | What colors does the page use? | `prefers-color-scheme`, custom properties, `light-dark()` |
| Browser-drawn UI | How do built-in elements look? | `color-scheme` property, `<meta name="color-scheme">`, `theme-color` |
The preference layer is outside your CSS. The other two layers are yours to configure.
Where the preference comes from
Operating systems such as Windows, macOS, iOS, and Android let users choose light or dark appearance. Many also offer an automatic setting that switches at sunset. Browsers read this setting and expose it to web pages through a media query named prefers-color-scheme. Some browsers also let users override the value for the browser alone.
The query accepts two values you can rely on:
light: the user prefers a light appearance, or the browser has no signal.dark: the user prefers a dark appearance.
Because "no signal" behaves like light, a sensible design makes light the default and uses the dark query only as an override. This also keeps your page working in older browsers that do not know the query. They ignore the block and show the light theme.
The branch to the right of the diagram is easy to forget. Your own colors and the browser's built-in colors are controlled by different mechanisms, and a page needs both.
A first working page
The following page follows the system setting. It uses custom properties, which are CSS variables whose names start with -- and which you read with var(). Colors are defined once as variables, then the dark query replaces their values.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="color-scheme" content="light dark">
<title>Dark mode basics</title>
<style>
:root {
color-scheme: light dark;
--bg: #ffffff;
--text: #1a1a1a;
--link: #0b5fff;
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #121212;
--text: #e6e6e6;
--link: #8ab4ff;
}
}
body {
background: var(--bg);
color: var(--text);
font-family: system-ui, sans-serif;
max-width: 40rem;
margin: 2rem auto;
padding: 0 1rem;
}
a {
color: var(--link);
}
</style>
</head>
<body>
<h1>Hello, both themes</h1>
<p>Change your system appearance and this page follows it.
Read the <a href="https://developer.mozilla.org/">MDN docs</a>.</p>
<label>Your name <input type="text"></label>
<button type="button">Save</button>
</body>
</html>Open the file in a browser and switch your operating system between light and dark. The background, text, and link colors change without a reload. The text input and button also change, but that is the work of color-scheme, not the variables. You will see why next.
What `color-scheme` adds
Browsers draw many things you never style: the page canvas, default text color, form controls, scrollbars, and the default colors of elements such as <hr> and <select>. These follow the page's declared color schemes.
A page that declares nothing is treated as light-only. Even if you set --bg to near-black, the text field, the scrollbar, and the area outside your body background can still render light. This produces a half-dark page that looks broken.
There are two ways to declare schemes, and the example uses both:
- The
color-schemeCSS property, herecolor-scheme: light dark, says the page supports both. The browser then picks the one matching the user's preference. - The
<meta name="color-scheme" content="light dark">tag says the same thing in HTML. The browser reads it before the stylesheet downloads, so the first paint can already use the right canvas color.
Keep in mind that color-scheme does not change colors you set yourself. It only affects colors the browser supplies.
Why custom properties carry the theme
You could write dark rules by repeating every selector inside the media query:
@media (prefers-color-scheme: dark) {
body { background: #121212; color: #e6e6e6; }
a { color: #8ab4ff; }
.card { background: #1e1e1e; border-color: #333; }
}This works for tiny pages. It gets harder as the site grows. Every new component needs a light rule and a dark rule, and the two can drift apart. With custom properties, components refer to names such as --bg and --text, and only the variable definitions change per theme. The dark block stays as short as the list of colors, not as long as the stylesheet.
Custom properties also inherit. Redefining them on :root (the <html> element) affects the whole document, and you can redefine them on a single element to theme one region differently. The chapters that follow build this idea into named, meaningful color tokens.
Common errors
- Dark colors without a light default. If the only definition of
--bglives inside the dark query,var(--bg)is empty in light mode and the property is dropped. Define the light values at:rootfirst, then override. - Forgetting
color-scheme. The result is dark page areas next to light scrollbars and inputs. Add the property or the meta tag. - Hard-coded colors left in components. One
color: #000in a component stays black on a dark background and becomes unreadable. Search the stylesheet for literal colors when you add a theme. - Pure black and pure white.
#000on#fffis the highest possible contrast and often tires the eyes in the dark. Chapter 9 covers palette choices. - Assuming the media query is the whole story.
prefers-color-schemereflects the system. A user may want your site dark while their system is light. That requires a manual toggle and a saved choice, which are covered later. - Testing in one theme. A layout that looks fine in light can hide missing borders or invisible icons in dark. Always check both.
Choosing an approach
You have several ways to deliver a dark theme. The tutorial builds up from the simplest.
| Approach | Follows system | User can override | Best when |
|---|---|---|---|
| Media query plus custom properties | Yes | No | A small site where following the system is enough |
| Media query plus `light-dark()` | Yes | Only through `color-scheme` | You want fewer duplicated rules |
| `data-theme` attribute plus saved choice | Yes, as the default | Yes | You offer a light, dark, and system toggle |
Most production sites end at the third row. Starting with the first row means each later step adds to the same variables instead of replacing them.
How the rest of the tutorial fits together
The chapters follow the three layers. First you will learn the prefers-color-scheme query and custom properties in more depth, then organize colors as semantic tokens. After that come color-scheme and light-dark(), then the user toggle and the way to save its value without a flash of the wrong colors. The final chapters cover palette design, contrast, images, testing in developer tools, and the theme-color meta tag for the browser's own chrome.
Exercise
A page sets --bg: #121212 and --text: #e6e6e6 inside @media (prefers-color-scheme: dark), and declares nothing else about color schemes. On a dark system, the body looks dark, but text inputs and the scrollbar are still light. Why does this happen, and what is the smallest fix?
Show answer
The custom properties only change colors the page applies itself. Inputs and scrollbars are drawn by the browser, and a page that does not declare color-scheme is treated as light-only, so the browser styles them light. The smallest fix is to add color-scheme: light dark; to :root, or <meta name="color-scheme" content="light dark"> in the head. The browser then uses dark built-in styling when the user prefers dark.
Exercise
Why should the light values be defined outside the media query, with the dark query acting only as an override?
Show answer
Browsers that do not support the query, or that report no dark preference, skip the dark block. If light values lived only inside a light query, those browsers could end up with no values at all. Defining light values at :root guarantees every browser has a complete theme, and the dark block replaces only what needs to change.