Dev.to WebDev 🛠 Dev 👁 0 📖 5 min read

How to Check Color Contrast for Accessibility (WCAG AA)

You pick a nice grey for body text, it looks fine on your monitor, and an audit later flags every paragraph on the site. If you want to know how to check color contrast for accessibility before that happens, the good new

How to Check Color Contrast for Accessibility (WCAG AA)

You pick a nice grey for body text, it looks fine on your monitor, and an audit later flags every paragraph on the site. If you want to know how to check color contrast for accessibility before that happens, the good news is that it is one formula and a few rules. The bad news is that most mistakes happen in the rules, not the formula.

This guide shows the WCAG numbers, how to calculate a ratio yourself, how to check it in Chrome DevTools, and the traps that make a "passing" pair fail.

Why color contrast matters

Low contrast text is hard to read for people with low vision or colour vision deficiencies, and for everyone reading a phone in sunlight. It is also the most common accessibility failure on the web. Chrome's own DevTools documentation calls low contrast text "the top accessibility issue on the web", citing WebAIM's Million report.

WCAG covers it in Success Criterion 1.4.3 (Contrast Minimum), which is the level AA requirement most laws and policies point to.

The WCAG contrast ratios you need to know

What you are checking AA AAA
Normal text 4.5:1 7:1
Large text (18pt / 24px, or 14pt / about 18.66px bold) 3:1 4.5:1
UI components and meaningful graphics (SC 1.4.11) 3:1 none

Two exceptions are easy to miss. Text in a disabled control, pure decoration, or text that is part of a logo has no contrast requirement. Everything else does, including placeholder text, captions and the small print under a form.

How to check color contrast by hand

The ratio compares the relative luminance of the two colours: (L1 + 0.05) / (L2 + 0.05), where L1 is the lighter colour. Luminance comes from the sRGB channels after you convert them to linear light, as defined in the W3C relative luminance definition.

Here is the whole thing in Python, no libraries:

def lin(c):
    c = c / 255
    return c / 12.92 if c <= 0.04045 else ((c + 0.055) / 1.055) ** 2.4

def luminance(hex_color):
    h = hex_color.lstrip("#")
    r, g, b = (int(h[i:i+2], 16) for i in (0, 2, 4))
    return 0.2126 * lin(r) + 0.7152 * lin(g) + 0.0722 * lin(b)

def ratio(fg, bg):
    l1, l2 = sorted((luminance(fg), luminance(bg)), reverse=True)
    return (l1 + 0.05) / (l2 + 0.05)

for fg, bg in [("#767676", "#ffffff"), ("#777777", "#ffffff"),
               ("#ffffff", "#3b82f6"), ("#ffffff", "#2563eb")]:
    print(f"{fg} on {bg}: {ratio(fg, bg):.4f}:1")

We ran it, and this is the output:

#767676 on #ffffff: 4.5422:1
#777777 on #ffffff: 4.4781:1
#ffffff on #3b82f6: 3.6779:1
#ffffff on #2563eb: 5.1686:1

Look at the first two lines. #767676 and #777777 are one step apart, and they land on opposite sides of 4.5:1. The W3C Understanding document is explicit that the ratios are thresholds and must not be rounded: 4.499:1 does not meet 4.5:1. So a tool that shows "4.5" for #777777 and a green tick is giving you the wrong answer.

A ruler from 2.5:1 to 7.5:1 with the 3:1, 4.5:1 and 7:1 thresholds marked, and four example pairs plotted: 3.68, 3.95, 4.48 and 5.17

The third line is a very common case: white text on a mid blue button (#3b82f6, the Tailwind blue-500). At 3.68:1 it fails for normal text but passes for large text and for the button's outline as a UI component. Moving one shade darker to #2563eb gets you to 5.17:1 and the problem is gone.

How to check color contrast in Chrome DevTools

You do not need an extension for a quick check:

  1. Right-click the text and choose Inspect.
  2. Hover the element with the inspect tool. The tooltip shows a Contrast value, with a warning sign if it is too low.
  3. In the Styles pane, click the colour swatch next to color. The colour picker has a Contrast ratio section that shows the AA and AAA lines and can suggest a passing colour.

For a whole page, the Lighthouse accessibility audit and the CSS Overview panel list every low contrast element. The Chrome DevTools contrast guide walks through each of these.

Check it with a free contrast checker

When you are choosing colours rather than auditing a live page, a dedicated checker is faster. We built MyToolStudio, and its free WCAG color contrast checker runs in your browser with no sign up.

The MyToolStudio color contrast checker showing white text on #3b82f6 at 3.68:1, failing AA for normal text and passing for large text and UI components

We put the failing button pair from above into it: text #ffffff, background #3b82f6. It reports 3.68:1, the same number as the script, with AA normal text marked as a fail and large text and UI components as a pass. A few things that save time:

  • Font size and bold controls. Set the real size of your text and the tool tells you whether it counts as normal or large, so you know which threshold applies.
  • Opacity. Semi-transparent text is measured as it actually appears on the background (more on that below).
  • Suggested fixes. For our pair it offered #0d70e1 as a background that keeps white text and reaches 4.75:1.
  • Contrast grid. Paste up to 12 colours and every text and background pair is checked at once, which is handy for a whole palette.

If a palette fails everywhere, the accessible color palette generator starts from passing pairs, and the color blindness simulator shows how the result looks with different types of colour vision.

Common mistakes when checking contrast

White text on #3b82f6 at three sizes: 16px regular fails, 18.66px bold and 24px regular pass because they count as large text

Rounding up. Covered above, and worth repeating: 4.48:1 is a fail.

Ignoring opacity. color: rgba(0, 0, 0, 0.5) on white looks like a dark grey in your CSS, but on screen it blends to roughly #808080. Our script gives 3.95:1 after rounding the blended channel, and the checker reports 3.98:1 because it does not round it first. Either way it fails for normal text. Always measure the blended colour, not the CSS value.

Testing one state only. Hover, focus, visited and disabled styles all have their own colours. A link can pass at rest and fail on hover. Placeholder text is not exempt either.

Text over images and gradients. One check against the "average" colour proves nothing. W3C lists this as a known failure (F83). Check the lightest part of the image behind the text, or add a solid or semi-opaque layer under it.

Treating large text as "anything that looks big". Large means at least 24px regular or about 18.66px bold. A 20px regular heading is still normal text and needs 4.5:1.

FAQ

What is a good color contrast ratio?
For WCAG AA, at least 4.5:1 for normal text and 3:1 for large text and UI parts. If you can reach 7:1 for body text, you also meet AAA.

Does WCAG 2.2 change the contrast rules?
No. WCAG 2.2 keeps 1.4.3, 1.4.6 and 1.4.11 the same as 2.1. APCA, the new contrast method for WCAG 3, is still a draft, so treat its Lc score as extra information and keep using the 2.x ratios for compliance.

Do logos and disabled buttons need to meet contrast?
No. Text that is part of a logo, pure decoration, and inactive controls are exempt from 1.4.3. Everything a user needs to read or operate is not.

Wrapping up

Checking contrast is quick once you know the rules: use the real blended colours, match the threshold to the text size, check every state, and never round up. If you are working through other web checks, our earlier guides on testing a robots.txt file and decoding a JWT safely follow the same hands-on approach.

You can find the rest of our free developer, SEO and accessibility utilities on the MyToolStudio tools page. For the official wording of the criterion, read the W3C Understanding page for SC 1.4.3.

More from this series

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.