Frontend development · Advanced · 22 min
Make components work in forced colours mode
By Padding · Updated 18 Nov 2026
Modern browsers · CSS Color Adjustment Module Level 1 and Media Queries Level 5 · checked in Chromium with Playwright 1.65.1
You will find the Larkfield Clinic components that lose their meaning when Windows forced colours replace the page palette, fix them with a transparent border, currentColor and system colour keywords inside a forced-colors media query, and keep them fixed with a Playwright test that runs under emulation.
On this page
Windows has a setting called contrast themes. When someone turns one on, Chromium browsers and Firefox on Windows throw away most of the colours your stylesheet sets and paint the page from a small palette the person chose: one colour for the background, one for text, one for links, one for buttons, one for selected things, one for disabled text. Backgrounds go flat, shadows disappear, and text gets the palette's text colour. This is forced colours mode. People use it for low vision, for migraines and light sensitivity, or to read a screen in bright sun.
Most developers never switch it on, so most components are never tested in it. The usual failures are quiet: a button that was only a coloured rectangle becomes a floating word, a selected item that was shown only by its fill looks like every other item, and an icon drawn in a fixed colour turns invisible against the new background.
You will work on a cut-down Pick a time screen from the Larkfield Clinic booking app, the fictional community clinic used across the design track. The patient has chosen a Nurse appointment (15 minutes) on Tuesday 9 December, and 10:30 is selected. The colours come from the Larkfield design: accent #0F5E5A, on-accent #FFFFFF, text #1B1F24, text-muted #5A6270, border #7D8592 and the canvas #F6F7F8 background. The page looks right in normal mode. Your job is to make it keep its meaning when the colours are taken away, without changing how it looks for everyone else.
Step 1 · Build the Pick a time page
Make a demo folder and write the page. It has three components that will cause trouble: a clock icon in the service line, the time slot chips (built from radio buttons, with a tick on the selected one) and the Continue button.
mkdir -p ~/forced-colours-demo
cd ~/forced-colours-demo
cat > index.html <<'HTML'
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Pick a time · Larkfield Clinic</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<main>
<h1>Pick a time</h1>
<p class="service">
<svg class="icon" width="16" height="16" viewBox="0 0 16 16" aria-hidden="true">
<circle cx="8" cy="8" r="6.5" fill="none" stroke="#0F5E5A" stroke-width="1.5"/>
<path d="M8 4.5V8l2.5 1.5" fill="none" stroke="#0F5E5A" stroke-width="1.5" stroke-linecap="round"/>
</svg>
Nurse appointment · 15 min
</p>
<form method="get">
<fieldset>
<legend>Tue 9 Dec, morning</legend>
<div class="slots">
<label class="slot"><input type="radio" name="slot" value="09:00"><svg class="tick" width="16" height="16" viewBox="0 0 16 16" aria-hidden="true"><path d="M3 8.5l3.2 3L13 4.5" fill="none" stroke="#FFFFFF" stroke-width="2"/></svg>09:00</label>
<label class="slot"><input type="radio" name="slot" value="09:20"><svg class="tick" width="16" height="16" viewBox="0 0 16 16" aria-hidden="true"><path d="M3 8.5l3.2 3L13 4.5" fill="none" stroke="#FFFFFF" stroke-width="2"/></svg>09:20</label>
<label class="slot"><input type="radio" name="slot" value="09:40" disabled><svg class="tick" width="16" height="16" viewBox="0 0 16 16" aria-hidden="true"><path d="M3 8.5l3.2 3L13 4.5" fill="none" stroke="#FFFFFF" stroke-width="2"/></svg>09:40 <span class="taken">Taken</span></label>
<label class="slot"><input type="radio" name="slot" value="10:30" checked><svg class="tick" width="16" height="16" viewBox="0 0 16 16" aria-hidden="true"><path d="M3 8.5l3.2 3L13 4.5" fill="none" stroke="#FFFFFF" stroke-width="2"/></svg>10:30</label>
<label class="slot"><input type="radio" name="slot" value="11:00"><svg class="tick" width="16" height="16" viewBox="0 0 16 16" aria-hidden="true"><path d="M3 8.5l3.2 3L13 4.5" fill="none" stroke="#FFFFFF" stroke-width="2"/></svg>11:00</label>
</div>
</fieldset>
<button class="primary" type="submit">Continue</button>
</form>
</main>
</body>
</html>
HTMLThe SVG colours are written straight into the markup as stroke="#0F5E5A" and stroke="#FFFFFF", which is how icons often arrive from a design file or an icon set. Now the styles, written the way a busy team writes them: the button is a filled rectangle with no border, and the selected slot is shown by its fill, white text and the white tick.
cd ~/forced-colours-demo
cat > style.css <<'CSS'
:root {
--text: #1b1f24;
--text-muted: #5a6270;
--surface: #ffffff;
--canvas: #f6f7f8;
--border: #7d8592;
--accent: #0f5e5a;
--on-accent: #ffffff;
}
* { box-sizing: border-box; }
body {
margin: 0;
background: var(--canvas);
color: var(--text);
font-family: system-ui, sans-serif;
line-height: 1.5;
}
main { max-width: 26rem; margin: 0 auto; padding: 1rem; }
h1 { font-size: 1.5rem; margin: 0 0 0.25rem; }
.service {
display: flex;
align-items: center;
gap: 0.375rem;
margin: 0 0 1.5rem;
color: var(--text-muted);
font-size: 0.875rem;
}
fieldset { border: 0; margin: 0 0 1.5rem; padding: 0; min-width: 0; }
legend { font-weight: 600; margin-bottom: 0.75rem; padding: 0; }
.slots { display: flex; flex-wrap: wrap; gap: 0.5rem; }
.slot {
display: inline-flex;
align-items: center;
gap: 0.25rem;
min-height: 2.75rem;
min-width: 5.5rem;
padding: 0.5rem 0.875rem;
border: 1px solid var(--border);
border-radius: 0.5rem;
background: var(--surface);
font-variant-numeric: tabular-nums;
cursor: pointer;
}
.slot input {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
}
.slot .tick { display: none; }
.slot:has(input:checked) {
background: var(--accent);
border-color: var(--accent);
color: var(--on-accent);
font-weight: 600;
}
.slot:has(input:checked) .tick { display: block; }
.slot:has(input:disabled) {
border-style: dashed;
color: var(--text-muted);
cursor: not-allowed;
}
.slot:has(input:focus-visible) {
outline: 3px solid var(--accent);
outline-offset: 2px;
}
.primary {
width: 100%;
min-height: 3rem;
border: 0;
border-radius: 0.5rem;
background: var(--accent);
color: var(--on-accent);
font: inherit;
font-weight: 600;
cursor: pointer;
}
.primary:focus-visible {
outline: 3px solid var(--accent);
outline-offset: 2px;
}
CSSThe radio inputs are clipped to nothing rather than hidden with display: none, so they keep keyboard focus and their state. Build a time slot picker with radio buttons explains that pattern in full; here it only needs to work.
Check it worked
Open index.html in Chrome or Edge (open index.html on macOS, xdg-open index.html on Linux, or drag the file into a window). You see the heading, a teal clock icon before "Nurse appointment · 15 min", five white chips with grey edges, a dashed 09:40 Taken chip, a teal 10:30 chip with a white tick and bold white text, and a full-width teal Continue button.
Step 2 · Switch on forced colours in DevTools
You do not need Windows to see the problem. Chromium's DevTools can emulate forced colours on any desktop system.
- Open DevTools (F12, or Cmd+Option+I on macOS).
- Open the Command Menu with Ctrl+Shift+P (Cmd+Shift+P on macOS), type
renderingand choose Show Rendering. - In the Rendering panel, find "Emulate CSS media feature forced-colors" and set it to
forced-colors: active. - To see a dark palette as well, set "Emulate CSS media feature prefers-color-scheme" to
prefers-color-scheme: dark.
With emulation on, the browser does more than match the forced-colors media query. It forces the colours too: any colour that is not a system colour keyword is replaced, so background colours become the palette's background, text and border colours become the palette's text colour, and box-shadow and text-shadow are dropped. Images, including inline SVG, are left alone. In the emulated light palette the background is white and text is black; in the dark one they swap. Each palette also has its own highlight colour and its own disabled-text colour, which you will use later.
Look at the page in both palettes and write down what has lost its meaning.
- The Continue button is a bold word with nothing around it. Its teal fill was the only thing that made it a button, and fills are forced to the page background. It now looks like a heading.
- The 10:30 chip has the same flat background and edge as every other chip. In the light palette its tick is white on white, so all that is left is slightly bolder text. In the dark palette the white tick survives, by luck.
- The clock icon keeps its hard-coded teal, which is faint on the dark palette's black background, well below the 3:1 contrast that WCAG 1.4.11 asks of a meaningful icon.
- The 09:40 chip keeps its dashed edge and the word
Taken, so it still reads as unavailable, but its text is the same colour as the free times.
On Windows 11 you can test the real thing: Settings, Accessibility, Contrast themes, pick a theme and apply it. Windows ships several themes, light and dark, and people can edit every colour in them, so never design for one particular palette.
Check it worked
With forced-colors: active and the light scheme, the Continue button has no visible edge and the 10:30 chip shows no tick. Switch to the dark scheme: the clock icon is a faint teal outline against black. Set forced colours back to "No emulation" and the teal design returns.
Step 3 · Write a test that runs under emulation
Checking by eye catches the problem once. A test keeps it fixed. Install Playwright, which drives a real browser from a script, into the demo folder. --with-deps also installs the system libraries Chromium needs on Linux, which can ask for your password:
cd ~/forced-colours-demo
npm init -y > /dev/null
npm install playwright@1.65.1
npx playwright install --with-deps chromiumPlaywright can open a page with forcedColors: 'active', the same emulation as DevTools. The test opens the page twice, once with each palette, presses Tab once so the selected slot has keyboard focus, and reads the colours the browser actually computed. It does not compare against fixed colour values, because every user's palette is different. It compares parts of the page with each other: is the button edge a different colour from its fill, does the selected chip differ from a free one, does the icon use the text colour. A last check with forced colours off makes sure the fixes have not changed the Larkfield design.
cd ~/forced-colours-demo
cat > check-forced-colours.mjs <<'JS'
import { chromium } from 'playwright';
const url = new URL('./index.html', import.meta.url).href;
const browser = await chromium.launch();
let failures = 0;
function report(ok, message) {
console.log(`${ok ? 'PASS' : 'FAIL'} ${message}`);
if (!ok) failures++;
}
for (const scheme of ['light', 'dark']) {
const page = await browser.newPage({
viewport: { width: 390, height: 600 },
forcedColors: 'active',
colorScheme: scheme,
});
await page.goto(url);
await page.keyboard.press('Tab');
await page.screenshot({ path: `forced-${scheme}.png` });
const r = await page.evaluate(() => {
const css = (selector) => getComputedStyle(document.querySelector(selector));
const button = css('.primary');
const chosen = css('.slot:has(input:checked)');
const other = css('.slot:has(input[value="11:00"])');
const taken = css('.slot:has(input:disabled)');
const service = css('.service');
return {
forced: matchMedia('(forced-colors: active)').matches,
buttonEdge: button.borderTopStyle !== 'none' && parseFloat(button.borderTopWidth) >= 1,
buttonEdgeColour: button.borderTopColor,
buttonFill: button.backgroundColor,
chosenFill: chosen.backgroundColor,
otherFill: other.backgroundColor,
chosenText: chosen.color,
chosenAdjust: chosen.forcedColorAdjust,
ring: chosen.outlineColor,
pageText: css('body').color,
focused: document.activeElement.value,
tickStroke: css('.slot:has(input:checked) .tick path').stroke,
takenText: taken.color,
otherText: other.color,
serviceText: service.color,
iconStroke: css('.service .icon circle').stroke,
};
});
const tag = `[${scheme} palette]`;
report(r.forced, `${tag} forced colours are on`);
report(r.buttonEdge && r.buttonEdgeColour !== r.buttonFill, `${tag} Continue button has a visible edge`);
report(r.chosenFill !== r.otherFill, `${tag} selected slot looks different from the others`);
report(r.chosenAdjust === 'none', `${tag} selected slot opts out of the text backplate`);
report(r.tickStroke === r.chosenText, `${tag} tick uses the selected slot's text colour`);
report(r.takenText !== r.otherText, `${tag} taken slot text differs from free slots`);
report(r.focused === '10:30' && r.ring === r.pageText, `${tag} focus ring on the selected slot uses the page text colour`);
report(r.iconStroke === r.serviceText, `${tag} clock icon uses the forced text colour`);
await page.close();
}
// Forced colours off: the Larkfield design is unchanged.
const normal = await browser.newPage({ viewport: { width: 390, height: 600 } });
await normal.goto(url);
const n = await normal.evaluate(() => {
const css = (selector) => getComputedStyle(document.querySelector(selector));
return {
buttonFill: css('.primary').backgroundColor,
chosenFill: css('.slot:has(input:checked)').backgroundColor,
iconStroke: css('.service .icon circle').stroke,
};
});
const accent = 'rgb(15, 94, 90)';
report(n.buttonFill === accent && n.chosenFill === accent && n.iconStroke === accent, '[no forced colours] accent colour on button, selected slot and icon');
await browser.close();
if (failures) {
console.log(`${failures} checks failed`);
process.exitCode = 1;
} else {
console.log('All forced colours checks passed');
}
JSRun it before fixing anything, so you know it can fail:
node check-forced-colours.mjsThe focus ring check passes already, because outline colours are forced like borders. It guards the selected-slot fix in a later step, which changes how the browser colours that chip and its ring. The backplate check fails until that fix too; the later step explains what a backplate is.
Check it worked
The output has 17 lines of results and ends with 11 checks failed. Both palettes fail the button, selected slot, backplate, taken slot and icon checks. The tick check fails only in the light palette, matching what you saw in DevTools. The [no forced colours] line passes. The folder now holds forced-light.png and forced-dark.png; open them to see what the test saw.
Step 4 · Give the button an edge that survives
Forced colours keep border styles and widths and only replace the colour. That makes a transparent border the standard fix for anything drawn with a fill alone. In normal mode a transparent border is invisible and the teal fill shows through it. In forced colours the border's colour is replaced with the palette's text colour, and the button gets an outline of its own.
cd ~/forced-colours-demo
cat >> style.css <<'CSS'
.primary { border: 2px solid transparent; }
CSSIn your own stylesheet, change border: 0 in the .primary rule rather than adding a second rule. Because of box-sizing: border-box and the min-height, the button stays 48 pixels tall. Do the same for anything else that is a shape made of fill: cards, tags, toasts, the Booked banner, segmented controls. If a component draws its edge or its focus ring with box-shadow, that disappears completely in forced colours; Style keyboard focus with :focus-visible covers a focus ring that survives.
Check it worked
Reload with forced-colors: active. Continue now sits in a rounded box drawn in the palette's text colour, black in the light palette and white in the dark one. Turn emulation off: the button looks exactly as before, teal with no visible border.
Step 5 · Make SVG icons follow the text colour
Inline SVG is treated as an image, and forced colours leave images alone. A colour written as stroke="#0F5E5A" stays teal whatever the palette. The fix has two parts.
First, draw icons in currentColor, the value of the element's color property, and set that colour in CSS. A CSS rule beats an SVG presentation attribute, so these rules override the hard-coded strokes in the demo markup. In your own icon files, change the attributes to stroke="currentColor" (or fill="currentColor" for filled icons) at the source.
Second, look at where the colour comes from. Chromium gives SVG elements forced-color-adjust: preserve-parent-color. An <svg> that inherits its color gets the parent's forced colour. An <svg> with a color of its own, like .icon { color: var(--accent) }, keeps it. So in forced colours, set that colour to a system colour keyword. CanvasText is the palette's text colour; Canvas is its background.
cd ~/forced-colours-demo
cat >> style.css <<'CSS'
.icon { color: var(--accent); }
.icon circle, .icon path, .tick path { stroke: currentColor; }
@media (forced-colors: active) {
.icon { color: CanvasText; }
}
CSSThe tick needs no media query: its <svg> sets no colour, so it inherits the chip's text colour. That colour is white on teal in normal mode and the palette's text colour in forced colours.
Use the media query for corrections like this and keep them small. Do not try to restyle the whole page inside it. The browser has already done that work, and its palette is the one the user picked.
Check it worked
Reload with forced colours and the dark scheme: the clock icon is now white, the same as the service text beside it. With the light scheme it is black. Turn emulation off and it is teal again. In the light palette, the 10:30 chip now shows a black tick.
Step 6 · Mark the selected and taken slots with system colours
The selected chip still has the same flat background as the others. The tick and bold text are real cues, but they are small. In forced colours, the platform's own way to say "selected" is the highlight pair: Highlight for the background and HighlightText for the text on it. Native list boxes and selected text use them, so a user knows what they mean in their palette.
System colour keywords are the one kind of colour forced colours leave alone, so background: Highlight and color: HighlightText inside the media query keep their values. Chromium adds one complication. To keep text readable over images, it paints a backplate in the palette's background colour behind every line of text. On the selected chip that backplate sits between the Highlight fill and the HighlightText label, so the label is drawn in, say, white on a white block and disappears. Try it: leave out the forced-color-adjust line below and reload. The fill is there but the time has gone.
forced-color-adjust: none on the chip removes the backplate. It also stops the browser forcing any colour on the chip, so every colour on it must now be a system colour you set. That includes the focus ring: without the outline-color line, the ring stays teal. Even without the opt-out you would need that line, because the browser then draws the ring in the chip's own text colour, HighlightText, which can match the page background. That is what the focus check in your test watches for.
The taken slot gets GrayText, the system colour for disabled text, so it reads as unavailable in the user's palette as well as through its dashed edge and the word Taken.
cd ~/forced-colours-demo
cat >> style.css <<'CSS'
@media (forced-colors: active) {
.slot:has(input:checked) {
forced-color-adjust: none;
background: Highlight;
border-color: Highlight;
color: HighlightText;
}
.slot:has(input:focus-visible) { outline-color: CanvasText; }
.slot:has(input:disabled) {
border-color: GrayText;
color: GrayText;
}
}
CSSUse forced-color-adjust: none only where you need it, as here, and on the smallest element that needs it, never on body or a wrapper. Everything inside the element inherits the opt-out, and every colour you forget to replace is a colour the user asked not to see.
Check it worked
Reload with forced colours. In the light palette, 10:30 is a solid chip in the palette's highlight colour with readable bold text and a tick in the highlight text colour, and the text and dashed edge of 09:40 change to the palette's disabled colour, which differs from the free times. In the dark palette, 10:30 is filled with that palette's highlight colour and its time is still readable. The exact colours depend on the palette. Press Tab once: the focus ring around 10:30 is in the palette's text colour, not teal.
Step 7 · Run the test again
cd ~/forced-colours-demo
node check-forced-colours.mjsAdd this command to your CI job beside your other browser tests. A future change that removes the transparent border, sets an icon colour on the <svg> or adds a background-only component then fails the build instead of reaching patients who use contrast themes. Emulation covers what Chromium does; before a release, spend five minutes with a real Windows contrast theme as well, since Firefox and real palettes can differ in detail.
Check it worked
Every line starts with PASS and the last line is All forced colours checks passed. Open forced-light.png and forced-dark.png: the Continue button has an edge, 10:30 is highlighted with a tick and a focus ring, and the clock icon matches the text.
Step 8 · Clean up
Remove the demo folder, which also removes the local Playwright install. The browser Playwright downloaded stays in its shared cache; npx playwright uninstall removes it if you no longer need it.
cd ~
rm -rf ~/forced-colours-demoCheck it worked
ls ~/forced-colours-demo reports that there is no such file or directory.
Next steps
- Build a focus ring that holds up on every background and in contrast themes with Style keyboard focus with :focus-visible.
- Forced colours remove colour as a cue; make sure the design never depended on it in Never rely on colour alone to carry meaning.
- Build the full slot picker with keyboard support and a test in Build a time slot picker with radio buttons.
- Give the button all its states in Build the Larkfield button in HTML and CSS.
Related tutorials
- Organise CSS with cascade layersYou will have a stylesheet for the Larkfield Clinic booking app split into reset, tokens, base, vendor, layout, components and utilities layers, a third-party widget that can no longer override your components, no !important left, and a Playwright script that checks the computed styles.
- Load web fonts without layout shiftYou will self-host the Larkfield Clinic typeface as one subset WOFF2 file that still covers Yoruba, Hausa, Igbo and Akan letters, preload it, pick a font-display value and tune a fallback font with size-adjust, then measure the page's layout shift falling to near zero.
- Build a living style guide page from CSS tokensYou will have a style guide page that reads every token from tokens.css when it loads and shows the palette with contrast ratios, the type scale, the spacing scale and the Larkfield components in their states, plus a Playwright test that fails when a token is missing.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials