slop audit
Blog post 79 / Accessibility and visual design

Forced colors: test CSS in Windows contrast themes

Test forced colors before launch. Keep button borders, keyboard focus, and selected states visible in Windows contrast themes, then add a Playwright check.

By Slop Audit · 6 min read

Forced colors can make a button lose its edge or a keyboard focus ring disappear, even when your site's normal colors pass a contrast check. A browser in this mode applies a user-selected palette and removes some visual effects. To test before launch, turn on forced colors, complete a real task with the keyboard, and repair any state that loses its visual cue.

What forced colors changes in CSS

Windows contrast themes, also known as Windows High Contrast mode, are one way to activate forced colors in a supporting browser. CSS exposes the active mode through @media (forced-colors: active). The browser can replace text, background, border, and outline colors with system colors. It also suppresses box shadows, text shadows, and gradient-only background images. A button whose boundary consists of a shadow can blend into its surroundings, and a selected card distinguished only by a tinted background can look like every other card. W3C CSS Color Adjustment specification.

Use forced-colors: active for repairs that apply when the browser is enforcing a palette. Use prefers-contrast to respond to a contrast preference, and prefers-color-scheme for light or dark styling. Check the site's authored colors separately with the color contrast accessibility guide.

Reproduce the missing state before editing CSS

Open the deployed route with the actual component, such as a listing with filter buttons or a form with custom fields. In Chrome DevTools, open Rendering and set Emulate CSS media feature forced-colors to forced-colors: active. You can confirm the mode in the console with matchMedia("(forced-colors: active)").matches. Chrome DevTools media emulation instructions.

With emulation on, check the same controls at rest, focused, and after activation.

  • Tab through the page. Look for a visible focus indicator even when the control already has a border. Check Shift+Tab as well.
  • Activate a toggle, select a tab, or choose a filter. Check whether you can identify the active option after moving focus elsewhere.
  • Trigger a validation error. Look for a visible message and a cue that identifies the affected field.
  • Open a menu or dialog. Check whether its boundary, close control, and links remain distinguishable from the surrounding content.

For a failure, save the route, browser, theme, control, state, and a screenshot. In the Elements panel, inspect the focused element or the element with the selected state. Look for a removed shadow, a suppressed gradient, a transparent border, or a state distinguished solely by its background. The keyboard accessibility checklist gives you a repeatable route through interactive states.

Keep borders and focus visible in forced colors

This filter button has three independent cues. A border marks the control, an outline marks keyboard focus, and a check mark shows whether the filter is active. The visible label stays stable while the component updates aria-pressed. The decorative mark is hidden from assistive technology because the button already exposes its pressed state.

<button class="filter-toggle" type="button" aria-pressed="false">
  <span class="filter-toggle__mark" aria-hidden="true">✓</span>
  In stock only
</button>

Wire the button to the filter in your application and update aria-pressed from the same state that controls the results. A native button supplies keyboard activation with Space or Enter. W3C button pattern.

.filter-toggle {
  padding: 0.5rem 0.75rem;
  border: 1px solid #42546a;
  border-radius: 0.375rem;
  background: #fff;
  color: #172033;
}

.filter-toggle__mark {
  display: inline-block;
  inline-size: 1em;
  visibility: hidden;
}

.filter-toggle[aria-pressed="true"] {
  background: #dbeafe;
}

.filter-toggle[aria-pressed="true"] .filter-toggle__mark {
  visibility: visible;
}

.filter-toggle:focus-visible {
  outline: 2px solid #1453a8;
  outline-offset: 3px;
}

@media (forced-colors: active) {
  .filter-toggle {
    border-color: ButtonText;
  }

  .filter-toggle:focus-visible {
    outline-color: Highlight;
  }
}

ButtonText and Highlight come from the active palette. Inspect the outline against the surrounding surface in the themes you support. If your existing design uses only a shadow for focus, add an outline with a width and style as well as a color. If the cue belongs to a tab, keep its selection marker independent of focus so it stays visible when the visitor moves into the panel. See the accessible tabs implementation guide.

Keep forced-color-adjust: auto, the default, for ordinary controls and text. A small color swatch whose hue is the content may need forced-color-adjust: none on the swatch itself. Keep the swatch's name and selected cue visible outside that override so the rest of the control uses the system palette. A page-wide opt-out can also suppress the browser's text backplates. W3C forced-color-adjust definition.

Add a forced colors Playwright regression test

Set the media mode before navigation. This example assumes the filter is the first tabbable control on a fixture or preview route and starts off. On a full page, follow the route's actual Tab order. Replace the example URL with your test route and use the Playwright media emulation options.

import { expect, test } from "@playwright/test";

test.use({ forcedColors: "active" });

test("stock filter keeps focus and a visible pressed cue", async ({ page }) => {
  await page.goto("https://example.com/filter-preview");

  const forcedColors = await page.evaluate(
    () => matchMedia("(forced-colors: active)").matches,
  );
  expect(forcedColors).toBe(true);

  const toggle = page.getByRole("button", { name: "In stock only" });
  const mark = toggle.locator(".filter-toggle__mark");

  await expect(toggle).toHaveAttribute("aria-pressed", "false");
  await expect(mark).toBeHidden();

  await page.keyboard.press("Tab");
  await expect(toggle).toBeFocused();
  await expect(toggle).toHaveCSS("outline-style", "solid");
  await expect(toggle).toHaveCSS("outline-width", "2px");

  await page.keyboard.press("Space");
  await expect(toggle).toHaveAttribute("aria-pressed", "true");
  await expect(mark).toBeVisible();
});

The assertions catch a missing outline style or a hidden pressed cue. Use a visual check for contrast and clipping because an outline can have the expected width and still be hard to see or get cut off by a parent. Before release, repeat the task in a browser on Windows with a light and a dark Contrast theme applied under Settings → Accessibility. If you're replacing legacy -ms-high-contrast rules, verify the shipped CSS uses the standard query. Microsoft Edge guidance on moving to forced colors.

Audit the deployed page before launch

Run Slop Audit on your public preview to check for related accessibility regressions after updating the component. When Browser Run is available, the report includes sampled keyboard focus evidence and axe text contrast findings with selectors where available. Use those findings to choose what to review and rescan the same URL after fixes. Keep the forced colors checks in your own browser test suite and Windows release pass. The website regression testing checklist shows how to record the route and evidence for the next release.