Eleven at night, you open an app in a dark room. The white screen glares, so you switch on dark mode. In a good app, all the text can still be read. In another app, the grey text disappears and the cards melt into the background.
Dark UI (dark mode) is a version of a page with a dark background and light text. A good dark UI isn't a light page with its colours flipped. It is designed again. Picture the terrace of Mrs. Sri's homestay at nine at night, with one lamp overhead. The floor is dark grey, not pitch black. A table is higher than the floor, so it is closer to the lamp and looks lighter. The table's shadow is barely visible, because the floor is already dark. These three things on the terrace match the three rules of dark UI:
- The dark grey floor: the page background is dark grey, not black.
- The lighter table: an element on top of another element (a card on the background, a menu on a card) gets a lighter colour.
- The missing table shadow: a shadow (the dark edge around a card, made with
box-shadow) stops working on a dark background.
Not flipped, but designed again
This table lists the role of each colour on the Mist Homestay booking page, in the light and the dark version. The number in brackets is the contrast ratio, computed with the WCAG formula against the surface under that colour: text is measured against the card, and the button text against its green.
| Role | Light | Dark |
|---|---|---|
| Page background | #ffffff |
#111827 |
Card (.mist) |
#ffffff + border + shadow |
#1f2937, no shadow |
| Border | #e2e8f0 |
#374151 |
| Main text | #1f2937 (14.68:1) |
#f1f5f9 (13.40:1) |
| Secondary text | #475569 (7.58:1) |
#94a3b8 (5.72:1) |
| Green (button, chosen night) | #15803d, white text (5.02:1) |
#22c55e, #111827 text (7.79:1) |
Secondary text is the paler supporting text, such as the note under the button. If the light colours are used as they are on the dark card, the secondary text #475569 is only 1.94:1 and can't be read. So every colour has to be picked and measured again.
Background: dark grey, not #000
White text on #000000 (pitch black) has a contrast of 21:1, the highest possible. That sounds good, but it is too much contrast. Material Design 2 (Google's design guidelines) recommends #121212 as the surface colour of a dark theme (another name for dark mode). The reason: dark grey makes the eyes tire less quickly, because light text on dark grey has less contrast than on black. Dark grey also isn't the darkest colour, so there is still room to tell surfaces apart: the ones on top of it are made lighter.
Mrs. Sri's page uses #111827. #f1f5f9 text on it is 16.19:1, still far above the 4.5:1 requirement. The one argument for pure black: on an OLED screen (a screen where each pixel makes its own light), black pixels are switched off and save battery. That is why some apps offer it as an extra option, not the default.
Elevation: lighter surfaces, not shadows
Elevation means how high one surface sits above another. In the light version, the card stands apart from the background because its shadow darkens the white background around the card a little. A rgba(0, 0, 0, 0.1) shadow (black at 10% strength) turns white into #e6e6e6 at its darkest point. The difference from white is 1.25:1, enough to see.
Now you make a dark version with a #111827 background and leave the same shadow, 0 1px 3px rgba(0, 0, 0, 0.1), as it is. Is the shadow still visible? Guess before you go on.
The shadow is barely visible. In the dark version, the same shadow turns #111827 into #0f1623. The difference is only 1.02:1. On #000 the difference is 1.00:1: no difference at all. Darkening a colour that is already dark makes no difference.
Instead, higher elements get a lighter surface. The background is #111827, and the card on it is #1f2937 (1.21:1 against the background). A menu or dialog (a window that appears over the page) above the card can be lighter still, such as #374151. Material Design 2 calls this an elevation overlay: the higher the surface, the lighter its colour. Material 3 (2023) replaced it with surface container roles (names for surface colours), from Surface Container Lowest to Highest, which get lighter in a dark theme. The principle is the same.
Green on dark: measure again
Green #15803d is used for the booking button and for the night button that is chosen. In the light version, what marks "2 nights chosen" is its green fill. WCAG 1.4.11 (the contrast rule for things that aren't text) asks for a state indicator like this fill to have at least 3:1 contrast against the colour next to it. On the white card, this green is 5.02:1, so it passes.
On the dark #1f2937 card, the same green is only 2.93:1, so it fails. The chosen night button and the unchosen ones become hard to tell apart. A lighter green, #22c55e, has 6.44:1 against the card. But white text on #22c55e is only 2.28:1. So the button's text changes to #111827 too (7.79:1). In dark mode, an accent colour is usually lighter and a little less intense than in the light version, and the text on it turns dark.
prefers-color-scheme: follow the OS setting
Users choose light or dark mode in their phone or laptop settings. CSS reads that choice through the prefers-color-scheme media query (a media query is a block of CSS that applies only in certain conditions). Keep every colour in a CSS variable, then change the values in one block. In this code, .mist is the booking card, .note the note under the button, and .book the book button:
:root {
color-scheme: light dark;
--bg: #ffffff; --surface: #ffffff; --line: #e2e8f0;
--text: #1f2937; --muted: #475569;
--accent: #15803d; --on-accent: #ffffff;
--shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #111827; --surface: #1f2937; --line: #374151;
--text: #f1f5f9; --muted: #94a3b8;
--accent: #22c55e; --on-accent: #111827;
--shadow: none; /* elevation through a lighter --surface */
}
}
body { background: var(--bg); color: var(--text); }
.mist { background: var(--surface); border: 1px solid var(--line); box-shadow: var(--shadow); }
.note { color: var(--muted); }
.book { background: var(--accent); color: var(--on-accent); }
@media (prefers-color-scheme: dark)applies when the user's OS is in dark mode. It holds only the new variable values. The.mistand.bookrules don't have to be written twice.color-scheme: light darktells the browser this page is ready for both modes. So date inputs, scrollbars and other built-in controls (form parts the browser draws itself) turn dark too. Without this line, a date input stays glaring white on a dark page.--shadow: nonein dark mode: the shadow can't be seen, so it is dropped.
Other options. This lesson uses the media query above, because it follows the OS setting, needs no JavaScript and works in old browsers. There are two other ways:
| Way | When to pick it |
|---|---|
light-dark(), for example --text: light-dark(#1f2937, #f1f5f9); |
two values in one line: the first for light mode, the second for dark mode (Chrome 123, Firefox 120, Safari 17.5). Without color-scheme: light dark, the first value is always used |
A theme button that saves the choice in localStorage |
visitors may choose a mode different from their OS. Their choice is set as data-theme="dark" on <html>. If the colours use light-dark(), writing :root[data-theme="dark"] { color-scheme: dark; } is enough |
localStorage is where the browser keeps small pieces of data that stay after the page is closed. We checked light-dark() and this data-theme in Chromium, Firefox and WebKit. There is one trap with a theme button. If localStorage is read after the page has loaded, the page shows light for a moment before it turns dark. So read it in a small <script> (JavaScript code) inside <head> (the part of the HTML the browser reads before the page content).
⚠️ Why does your dark mode feel wrong?
- Pitch black and pure white. 21:1 is too harsh. Use dark grey (
#111827,#121212) and off-white text (#f1f5f9). - Carrying the shadow into dark mode. The difference is only 1.02:1. Use a lighter surface for elevation.
- Not measuring the accent again.
#15803dpasses on the white card (5.02:1) but fails on the dark card (2.93:1). - Flipping colours with
filter: invert(1). Every colour flips, the photo too. Green#15803dbecomes pink#ea7fc2, and the order of light and dark between surfaces falls apart.
TL;DR: Dark UI (a version of a page with a dark background and light text) is designed again, not made by flipping colours. Don't use black #000000 as the background. White text on it is 21:1, too much contrast, and Material Design 2 (Google's design guidelines) recommends dark grey (#121212), because light text on dark grey is easier on the eyes. Mrs. Sri's page uses #111827. A shadow (the dark edge around a card) can't be seen on a dark background, because the difference is only 1.02:1. So elevation (a surface sitting on top of another) is shown with lighter surfaces: background #111827, card #1f2937. Measure every colour again. Secondary text (supporting text) becomes #94a3b8 (5.72:1). The button green becomes #22c55e (6.44:1 against the card, at least 3:1 under WCAG 1.4.11), with #111827 text on it. Keep colours in CSS variables, then change their values in @media (prefers-color-scheme: dark), a block that applies when the OS is in dark mode. Add color-scheme: light dark so the browser's built-in controls, such as date inputs, turn dark too.
⚡ Dark grey, not black
Guess first: why shouldn't #000000 (pitch black) be the background of dark mode?
Too much contrast. White text on it is 21:1, the highest possible. Material Design 2 (Google's design guidelines) recommends a dark grey, #121212, because it's easier on the eyes. Mrs. Sri's page uses #111827.
Meaning: Dark UI (a version of a page with a dark background and light text) is designed again, not made by flipping colours. Every colour is picked and measured again. For example, the secondary text (supporting text) #475569 is only 1.94:1 on the dark card, so it becomes #94a3b8.
⚡ Higher means lighter
Guess first: the card's rgba(0, 0, 0, 0.1) shadow from the light version stays on a dark #111827 background. Is it still visible?
Barely. The difference is only 1.02:1. Darkening a colour that is already dark makes no difference.
Meaning: In dark UI, elevation (a surface sitting on top of another) is shown with lighter surfaces: background #111827, card #1f2937, and a menu above the card lighter still.
⚡ Follow the OS setting
Write:
:root {
color-scheme: light dark;
--bg: #ffffff; --surface: #ffffff; --line: #e2e8f0;
--accent: #15803d; --on-accent: #ffffff;
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #111827; --surface: #1f2937; --line: #374151;
--accent: #22c55e; --on-accent: #111827;
}
}
Meaning: Keep colours in CSS variables, then change their values in one block. @media (prefers-color-scheme: dark) applies when the user's OS is in dark mode. --bg is the background colour, --surface the card's colour, --line the border, and --on-accent the colour of text on the green. Green #15803d on the dark card is only 2.93:1, so --accent (the accent colour) becomes #22c55e with dark text. color-scheme: light dark makes the browser's built-in controls, such as date inputs, turn dark too.