Sub topic 1
How Dark Mode Works on the Web
Dark mode on the web is a set of colors that the browser or your CSS chooses when the user prefers a dark appearance. This chapter explains the pieces that make that work and how they connect. Later chapters build each piece in detail.
Prerequisites
This chapter assumes you know:
- Basic HTML and CSS, including selectors and the cascade.
- CSS color values such as hex and rgb.
- Enough JavaScript to read and set DOM attributes.
The three layers of a dark theme
A working dark mode has three layers. Each answers a different question.
| Layer | Question it answers | Main tool |
|---|---|---|
| Preference | Does the user want dark or light? | `prefers-color-scheme` media query, or a saved choice |
| Colors | Which colors should the page use? | CSS custom properties |
| Browser UI | How should built-in controls look? | `color-scheme` property |
Most dark mode bugs come from mixing these layers up. For example, changing your page colors does not change the look of a default <select> or the page scrollbar. That is the job of the third layer.
The system preference
Operating systems let users pick a light or dark appearance. Browsers expose that choice to CSS through the prefers-color-scheme media query. It matches dark when the user's system is set to dark and light otherwise.
A media query is a condition that decides whether a block of CSS applies. Here the block applies only for dark users. You can follow the system setting with no JavaScript at all.
Colors as custom properties
A custom property is a CSS variable whose name starts with --. You set it on an element and read it with var(). Because custom properties inherit, setting them on :root makes them available everywhere on the page.
The simplest dark mode defines the light colors first. It then overrides the same names inside the media query:
:root {
--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);
}
a {
color: var(--link);
}Notice that the rules for body and a never mention a theme. They only say which variable to use. Only the variable values change. This is the central idea of the whole tutorial: write your component styles once, and swap the values underneath them.
What the browser does with color-scheme
Without extra information, the browser assumes your page is light. It draws form controls, scrollbars, and the default page background in light colors, even if your CSS paints the page dark. The color-scheme property tells the browser which schemes your page supports, so it can adjust that built-in UI.
:root {
color-scheme: light dark;
}The value light dark means the page supports both, and the browser should follow the user's preference.
Where a manual toggle fits
The system setting is a good default, but some users want a different theme on your site than on their device. A manual toggle sets an attribute such as data-theme on the root element. Your CSS then lets that attribute win over the media query. The toggle also needs to save the choice, usually in localStorage, and apply it before the first paint. Otherwise, the page flashes the wrong colors on load. Later chapters cover both steps.
Common mistakes
- Hard-coding colors in many rules. Every color you do not put in a variable needs a separate dark override.
- Forgetting
color-scheme. The page looks dark, but form fields and scrollbars stay bright. - Using pure black and pure white. This often causes eye strain. Later chapters cover palette design.
Exercise
A page sets --bg and --text correctly inside a prefers-color-scheme: dark media query. In dark mode the page looks right, but a <textarea> still has a bright white background. Which layer is missing, and what is the fix?
Show answer
The browser UI layer is missing. Add color-scheme: light dark; to :root so the browser draws built-in controls in the user's preferred scheme.