The short answer
- Name your colours by role (
--ink,--paper,--accent,--muted,--line), not by hue, and define them once on:root. - Add a dark block that changes only those values, for both the system setting and a manual switch:
@media (prefers-color-scheme: dark)plus[data-theme="dark"]. - Mark which theme is showing on
<html>(data-theme="dark"or adarkclass) and setcolor-scheme, so form controls, scrollbars and third-party components follow. - Check contrast in both themes: body text at least 4.5:1 against its background, large text and icons at least 3:1.
- Ask your agent to follow these rules in one message (the prompt card below), then look at every page in both themes.
The most common dark-mode bug in AI-built sites is not ugly colours. It is text you cannot see: a component that still draws dark text because it never learned the page went dark. It happens when dark mode is painted on as a background colour instead of being a state every component can read.
Colours by role, defined once
:root {
--paper: #fbfaf7; /* page background */
--surface: #ffffff; /* cards, inputs */
--ink: #1b1a17; /* text */
--muted: #5f5b53; /* secondary text */
--line: #e4e0d8; /* borders */
--accent: #b4410f; /* brand: buttons, links */
--on-accent: #ffffff;
color-scheme: light;
}Every rule in the site then uses only these names: color: var(--ink), background: var(--surface). A hard-coded #333 anywhere is a future invisible-text bug.
The dark values, two ways in
@media (prefers-color-scheme: dark) {
:root:not([data-theme="light"]) {
--paper: #141311; --surface: #1d1c19; --ink: #f1eee8; --muted: #a8a397;
--line: #34322d; --accent: #f08a4b; --on-accent: #1b1a17;
color-scheme: dark;
}
}
:root[data-theme="dark"] {
--paper: #141311; --surface: #1d1c19; --ink: #f1eee8; --muted: #a8a397;
--line: #34322d; --accent: #f08a4b; --on-accent: #1b1a17;
color-scheme: dark;
}The first block follows the visitor's system setting unless they chose light; the second applies when they chose dark on your switch. Notice the accent changes too: an orange that reads well on cream is too dark on near-black, and the text on it flips from white to dark.
The live demo loads here when scripts run. The text below says what it shows.
Pick a brand colour and a theme. The sample card re-themes through the role variables above, and each text pair shows its measured contrast (WCAG 2): body text, secondary text, a link and the button label. The dark accent keeps your hue and is lightened until links read at 4.5:1 on the dark card; the button label flips between white and near-black, whichever reads better. Tick background only to see the common bug: the page goes dark and the text does not. Open The CSS for these colours to copy the values.
Say which theme is showing
A component you install (a chart, a date picker, a toast) cannot see your background colour. It can see an attribute. Set one on <html> and keep it in step with the switch:
const root = document.documentElement;
function setTheme(choice) { // 'light' | 'dark' | 'system'
if (choice === 'system') root.removeAttribute('data-theme');
else root.setAttribute('data-theme', choice);
try { localStorage.setItem('theme', choice); } catch {}
}
const saved = (() => { try { return localStorage.getItem('theme'); } catch { return null; } })();
if (saved === 'light' || saved === 'dark') root.setAttribute('data-theme', saved);Load that before your CSS (a small classic <script> in <head>) so the page never flashes the wrong theme. Then check what your components expect: many follow [data-theme="dark"], Tailwind's dark: variant follows a dark class on <html> when you configure it that way, and some only follow the system setting. In our own customer tests, a site that went dark through its own CSS but set no attribute left an installed chart drawing dark titles on a dark card. Adding the attribute fixed it.
color-scheme: dark matters more than it looks: it is what turns native form fields, scrollbars and the default canvas dark.
Tailwind
With Tailwind, keep the same idea: the variables hold the values, Tailwind's theme points at them, and dark mode is the class on <html>. In Tailwind v4 the class-based variant is one line in your CSS:
@import "tailwindcss";
@custom-variant dark (&:where(.dark, .dark *));
@theme {
--color-ink: var(--ink);
--color-paper: var(--paper);
--color-accent: var(--accent);
}Now bg-paper text-ink works in both themes with no dark: classes at all, because the variables change underneath. Keep dark: for the rare element that needs a different treatment, not for every colour. If you use the dark class, set the class instead of data-theme in the switch above, or set both.
Check contrast in both themes
Body text needs a contrast ratio of at least 4.5:1 against its background, large text (24 px, or 19 px bold) and icons at least 3:1 (WCAG 2.2, level AA). Muted text is where dark themes usually fail. Chrome DevTools shows the ratio when you inspect a text element; check --muted on --paper and on --surface in both themes, and the button text on --accent.
The prompt
Add dark mode to this site. Rules:
1. Define colours once as CSS variables named by role on :root (--paper, --surface, --ink, --muted, --line, --accent, --on-accent), using our current colours for light. Replace every hard-coded colour in the CSS with one of these.
2. Add dark values in @media (prefers-color-scheme: dark) on :root:not([data-theme="light"]), and the same values on :root[data-theme="dark"]. Set color-scheme: light / dark with them.
3. Add a theme switch (System, Light, Dark) that sets or removes data-theme on <html> and remembers the choice in localStorage; apply the saved choice in a small script in <head> before the CSS so nothing flashes.
4. Any component library or installed component: check which signal it reads for dark mode (data-theme, a .dark class or the system setting) and make sure it gets it.
5. Check contrast: body and muted text at least 4.5:1 on paper and surface, button text at least 4.5:1 on the accent, in both themes. List each pair with its ratio.
Do not change layout, spacing or fonts. Tell me every file you changed.It works because it names the roles, both ways into dark mode, the attribute components need, and a contrast check the agent must report rather than assume.
Something here did not work?
Every command on this page was run before it was published, on the systems named in "How we tested this". If a step fails for you, write to [email protected] with what you ran and what you saw, and we will fix the page.