Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Adapt to More Contrast and Forced Colors

In this lesson, you checked the Hawthorn Seed Library catalogue under forced colors and found that its buttons lost their edges and its focus ring disappeared. You gave them edges drawn with a transparent border and outline, which forced colors keeps, and you made the seed cards’ borders stronger for visitors who ask for more contrast.

Key Points

  • The catalogue’s focus ring comes from outline: none and a box-shadow with no blur. Its color already passes 3:1 in both themes.
  • Some people can’t read pages in their designed colors. Windows gives them contrast themes. A person picks a few colors, such as light text on black, and the browser repaints every page with them. CSS calls this forced colors.
  • To try it, open DevTools’ Rendering panel from the Command Menu (Ctrl+Shift+P, or Cmd+Shift+P on macOS, then Show Rendering) and set the forced-colors emulation, Emulate CSS media feature forced-colors, to forced-colors: active. DevTools uses its own set of colors here. A person’s theme would use theirs.
  • In forced colors, the browser swaps the colors of text, backgrounds and borders for the person’s colors, and it sets every box-shadow to none. The focus ring was a box shadow, and the rule had already removed the outline, so a focused button showed nothing. Someone using the keyboard couldn’t see where they were.
  • Without its terracotta background, the button is plain blue text, like any link, so only an edge would show it’s a button.
  • Borders and outlines survive forced colors. Only their color changes. The form’s Send request button was as bare as the Borrow seeds buttons.
  • Each button gets border: 2px solid transparent. Transparent is a color too, so forced colors replaces it, and in normal colors the button’s background shows through it. The focus rule replaces outline: none with outline: 2px solid transparent and outline-offset: 2px. The offset keeps the ring apart from the button’s new edge. The box-shadow stays, so the ring looks as it did in normal colors.
  • The form’s Send request button had a more specific rule that set its border to none. Deleting that line lets the new border reach it too.
  • Other people keep a page’s own colors but turn on a system setting for more contrast, which CSS reads through prefers-contrast. The seed cards’ see-through borders only group content, so they don’t need 3:1, but under prefers-contrast: more they now use the same green as the fields’ borders. The media query comes after the :root rule that first declares --color-card-border, so it wins. Check it with Emulate CSS media feature prefers-contrast set to prefers-contrast: more, then set it back to No emulation.
  • The rule: draw edges and focus rings with borders and outlines, because forced colors removes every box shadow.

Colors That Follow the Person’s Theme

Forced colors repaints text, backgrounds and borders for you. When one of your own parts needs a particular color from the person’s palette, you can name it yourself with a system color keyword, usually inside a @media (forced-colors: active) query. These five cover most needs:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Bea’s catalogue now tones its motion down for visitors who ask. Some visitors also change how colors look. In this video, you’ll keep the catalogue’s buttons and focus ring visible when they do.

00:13 Click inside the first seed card, and press Tab. The focus ring around its Borrow seeds button comes from this rule. It removes the browser’s own outline, and a box-shadow with no blur draws the ring instead.

00:25 You already made its color pass 3 to 1.

00:29 Some people can’t read pages in their designed colors. Windows gives them contrast themes. A person picks a few colors, such as light text on black, and the browser repaints every page with them.

00:41 CSS calls this forced colors.

00:45 Open DevTools, then the Rendering panel from the Command Menu, as you did for reduced motion. Find the forced-colors emulation, and set it to active. Text turns black on white, and links turn blue.

00:58 The terracotta behind each button is gone. DevTools uses its own set of colors here. A person’s theme would use theirs.

01:07 Here’s the focus rule again. Click inside the first card, and press Tab to reach its Borrow seeds button. Your turn: pause the video and predict what shows that the button has focus.

01:24 Nothing does. It has no ring and no edge. In forced colors, the browser swaps text, background and border colors for the person’s. And it sets every box-shadow to none.

01:37 The ring was a box-shadow, and the rule had already removed the outline. Someone using the keyboard can’t see where they are. Now it’s plain blue text, like any link, so only an edge would show it’s a button.

01:51 The banner’s stripe, the cards’ borders and the fields’ borders all survived, in black. Below the fields, Send request is as bare as Borrow seeds. Forced colors keeps borders and outlines.

02:04 It only changes their color.

02:07 So give the buttons edges that only show up here. In the borrow button’s rule, add a 2px border that’s solid and transparent.

02:17 Transparent is a color too, so forced colors replaces it. In normal colors, the button’s background shows through it. In the focus rule, replace outline: none with a 2px outline that’s solid and transparent, and an outline-offset of 2px.

02:36 The offset keeps the ring apart from the button’s new edge. The box-shadow stays, so in normal colors the ring looks as it did. Send request has a more specific rule that sets its border to none.

02:48 Delete that line, so the new border reaches it.

02:53 The preview reloads with forced colors still on. Click inside the first card, and press Tab. Now every Borrow seeds button has an edge, and the focused one has a ring outside it.

03:05 Down in the form, Send request has its edge too. Set the emulation back, and tab to the button again. Bea’s terracotta buttons and focus ring are back.

03:18 Other people keep a page’s own colors but turn on a system setting for more contrast. CSS reads it as the prefers-contrast media feature. The seed cards’ borders are a see-through green.

03:31 They only group content, so they don’t need 3 to 1. Stronger ones still help someone who asked for more contrast. In the base layer, after the theme switch’s rules, add a media query for prefers-contrast set to more.

03:44 Inside it, on the root, point the card border role at the field border role.

03:51 It comes after the root rule that first sets the role, so it wins. In the Rendering panel, set the prefers-contrast emulation to more. The cards’ borders turn the same strong green as the fields.

04:05 Then set it back.

04:08 Here’s the rule: draw edges and focus rings with borders and outlines, because forced colors sets every box-shadow to none. Next comes Exercise: Respect the Visitor’s Settings.

04:22 There, you’ll adapt more of Bea’s catalogue to people’s settings on your own.

Become a Member to join the conversation.