← Back to HTML & CSS, page 5

html-css-0100 · Hard senior

What is forced colors mode, and how do you make a custom UI work in it?

Forced colors mode replaces author colours with a few system colours and removes shadows and background images. Keep states visible with borders and outlines, use currentColor in SVG, adjust with system colour keywords in @media (forced-colors: active), and avoid forced-color-adjust: none except for small, careful cases.

Full answer

Forced colors mode is an operating system feature, such as Windows contrast themes. When it is on, the browser replaces many of your colours with a small set chosen by the user, so that text is easy to read. The CSS media feature is forced-colors: active.

The browser changes properties such as color, background-color, border-color and outline-color, and removes box-shadow and background images in many cases. Text shadows are removed too. SVG fills and strokes are not changed unless they use currentColor or a system colour.

This causes bugs in custom UI:

  • A custom checkbox or switch drawn only with background colour can become invisible, because the background is replaced.
  • Information shown only by a background colour or shadow disappears. A focus ring made with box-shadow is not shown, but a real outline is kept.
  • Icons in SVG with a fixed fill can vanish on a forced dark background.

Fixes:

  1. Use real borders and outlines for states, not only background or shadow.
  2. Use currentColor in SVG so icons follow the text colour.
  3. Use system colour keywords such as ButtonText, Canvas, CanvasText, Highlight and LinkText inside @media (forced-colors: active) where a manual rule is needed.
  4. If you must keep your own colour for a part, set forced-color-adjust: none on that element, with care, and use system colours inside it. Do not set it on the whole page.
  5. Use transparent borders, such as border: 2px solid transparent, so there is something for forced mode to colour.

Test by turning on a contrast theme in the operating system, or with DevTools emulation of forced-colors.

In normal mode the chip is blue with white text and a blue shadow ring on focus. In forced colors mode, the shadow is removed, but the transparent outline becomes visible in a system colour, and the chip uses the system highlight colours, so it is still clear.

Note that outline: 3px solid transparent is a known technique, because transparent outlines become visible only in forced colors mode.

html
<style>
  .chip { display: inline-block; padding: 4px 12px; border-radius: 12px; background: #e6f0ff; border: 2px solid transparent; }
  .chip[aria-pressed="true"] { background: #0a6cff; color: #fff; }
  button.chip:focus-visible { box-shadow: 0 0 0 3px #0a6cff; outline: 3px solid transparent; }
  @media (forced-colors: active) {
    .chip[aria-pressed="true"] { border-color: Highlight; background: Highlight; color: HighlightText; }
  }
</style>
<button type="button" class="chip" id="chip" aria-pressed="true">Selected</button>

Checked with forced colors emulated in Chrome, not with a real operating system contrast theme. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • Forced colors mode is controlled by the user's operating system.
  • It changes colours and removes box-shadow in many cases.
  • Use borders and outlines for states and focus.
  • Use currentColor in SVG and system colours in a forced-colors media query.
  • Test with a contrast theme or DevTools emulation.

Common mistakes

  • Using box-shadow as the only focus indicator, which disappears in forced colors mode.
  • Setting forced-color-adjust: none on the whole page.
  • Drawing icons with a fixed fill, so they disappear on the forced background.

Follow-ups

  • Which CSS media feature detects forced colors mode?
  • When is forced-color-adjust: none acceptable?

Tips

  • Explain what the OS does: it replaces colours and removes shadows.
  • Name the transparent outline technique and currentColor for SVG.