Loading video player…

Check Text Contrast in DevTools

In this lesson, you gave the paragraphs of the work day notice you built in HTML a light gray that looked readable on the pale green page, then measured its contrast against that background in DevTools’ color picker. It failed, so you dragged it darker until AA turned green.

Key Points

  • Watch out: readable to you isn’t the test. Someone with low vision, someone reading a phone in bright sunlight, or someone on a dim screen sees less contrast than you do. So measure contrast instead of judging it by eye.
  • Open the color picker from the Styles pane. In the Elements panel, select the paragraph under Turn the Compost. The Styles pane shows your p rule from events.css with a small square of its color in front of the value. Click that square, and the color picker opens.
  • The picker’s big square holds the shades of one color, from white at the top left to black along the bottom, and its left edge runs through the grays. A small circle marks your color, and the value field shows its hex code. You can ignore the rest of the picker for now.
  • The contrast ratio compares how light the text is with how light the background behind it is. On the notice, that’s the honeydew from your body rule. The scale runs from 1:1, text the same color as its background, up to 21:1, black on white. Your light gray, #999999, showed Contrast ratio 2.74 on honeydew.
  • No contrast information available isn’t a pass. DevTools couldn’t find the background, as happens on the notice’s date paragraph. Click the eyedropper beside Pick background color, then click the background on the page, and the ratio appears.
  • WCAG AA is the level of the Web Content Accessibility Guidelines that most sites aim for. For body text, it asks for a ratio of at least 4.5:1. AAA is a stricter level, at 7:1. Click the Contrast ratio row to see both. At 2.74, both had a red mark, so the gray failed.
  • Large text has lower bars, and the picker chooses them by itself from the text’s size and weight. For the notice’s heading, it showed AA: 3.0 and AAA: 4.5. That’s all you need about large text for now.
  • Two white curves appear on the square. Every shade below the upper one passes AA, and every shade below the lower one passes AAA. On a light background like the notice’s, darker text means more contrast, so drag the circle down the left edge to keep it gray. The ratio climbs as the paragraphs on the page darken with it. At #757575 the ratio was 4.46, still a fail. One step further, at #737373, it was 4.59, and AA got a green check.
  • DevTools changes the page, not your file. The asterisk before events.css in the Styles pane means the change lives only in DevTools, and a reload brings back the old gray. Copy the new value into your file and save.
  • The rule: even a color that looks fine to you can fail, so check each text color against the background it sits on, and move it away from that background’s lightness (darker on a light background) until AA turns green at 4.5:1 for body text. Check the colors you chose in the last lesson the same way.

Final Code

events/events.css, the file from Choose Colors with the new p rule at the end and its gray darkened until it passes AA on honeydew:

Language: CSS Filename: events/events.css
/* Practice styles for the garden's event pages */

body {
  background-color: honeydew;
  color: oklch(0.247 0.05 152.7);
  font-family: system-ui, sans-serif;
}

h1 {
  color: oklch(0.447 0.116 152.7);
}

h2 {
  color: oklch(0.347 0.08 152.7);
}

a {
  color: oklch(0.447 0.116 152.7);
  text-decoration-color: color-mix(
    in oklch, currentColor 40%, transparent
  );
}

p {
  color: #737373;
}

Next, in Size Text With rem and em, you’ll decide how big that text should be, and in which unit.

00:00 Before the garden’s colors go on every page, you’ll measure whether everyone can read the text.

00:06 Say you’d like the notice’s paragraphs a little softer than its headings. At the end of events.css, you give every paragraph a light grey. Save, and the paragraphs turn grey on the pale green page.

00:19 On your screen, they look perfectly readable. Watch out: readable to you isn’t the test. Someone with low vision, someone reading a phone in bright sunlight, or someone on a dim screen sees less contrast than you do.

00:34 So measure contrast instead of judging it by eye.

00:39 Open DevTools, and in the Elements panel, select the paragraph under Turn the Compost. The Styles pane lists the rules that match it. Your rule from events.css is at the top, with a small square of its color in front of the value.

00:54 Click that square, and the color picker opens. The big square holds the shades of one color, from white at the top left to black along the bottom. Its left edge runs through the greys.

01:06 The small circle marks your grey, and the value field shows its hex code. You can ignore the rest of the picker for now.

01:15 Under the value is the contrast ratio, 2.74. It compares how light the text is with how light the background behind it is. Here, that’s the honeydew from your body rule.

01:26 The scale runs from 1 to 1, text the same color as its background, up to 21 to 1, black on white. On some elements, the picker says No contrast information available instead.

01:40 That isn’t a pass. DevTools couldn’t find the background. Click the eyedropper beside Pick background color, then click the background on the page.

01:50 Click the row to see what the ratio needs to reach. AA is a level of WCAG, the Web Content Accessibility Guidelines, and it’s the level most sites aim for.

02:01 For body text, AA asks for at least 4.5 to 1. AAA is a stricter level, at 7. Both have a red mark. At 2.74, your light grey fails.

02:16 Large text, like your heading, has lower bars, and the picker chooses them from the text’s size and weight by itself. That’s all you need about large text for now.

02:27 The two white curves on the square show where those levels lie. Every shade below the upper curve passes AA. Every shade below the lower one passes AAA. Your grey sits above both.

02:41 On a light background like this one, darker text means more contrast. So drag the circle down the left edge to keep it grey. As the grey darkens, the ratio climbs, and the paragraphs on the page darken with it.

02:55 Right above the curve, the ratio reads 4.46, and AA still fails. One step further, the circle crosses the curve. The ratio reads 4.59, and AA gets a green check.

03:11 The picker changed the page, not your file. The asterisk before events.css means the change lives only in DevTools, and a reload brings back the old grey. So copy the new value into events.css, and save.

03:28 Even a color that looks fine to you can fail, so check each text color against the background it sits on, in the color picker. Move it away from that background’s lightness, darker here, until AA turns green at 4.5 to 1 for body text.

03:44 Your paragraphs now pass. Check the colors you chose in the last lesson the same way. Next, in Size Text With rem and em, you’ll decide how big that text should be, and in which unit.

Become a Member to join the conversation.