webdevpuneet.com

Color Contrast Checker — WCAG AA/AAA Ratio Tool

Color Contrast Checker · Tools · Plain HTML, CSS & JS · Live preview

Copy & paste

Source Code

<div class="cc-card">
  <div class="cc-preview" id="ccPreview">
    <span class="cc-big">Big Text</span>
    <span class="cc-small">The quick brown fox jumps over the lazy dog.</span>
  </div>

  <div class="cc-inputs">
    <label class="cc-field"><span>Text</span>
      <div class="cc-swatch"><input type="color" id="ccFg" value="#475569"><input type="text" id="ccFgHex" value="#475569" maxlength="7" spellcheck="false"></div>
    </label>
    <label class="cc-field"><span>Background</span>
      <div class="cc-swatch"><input type="color" id="ccBg" value="#ffffff"><input type="text" id="ccBgHex" value="#ffffff" maxlength="7" spellcheck="false"></div>
    </label>
  </div>

  <div class="cc-ratio"><strong id="ccRatio">0</strong><span>: 1 contrast ratio</span></div>

  <div class="cc-grid" id="ccGrid">
    <div class="cc-cell" data-k="aaNormal"><span>AA Normal</span><b></b></div>
    <div class="cc-cell" data-k="aaLarge"><span>AA Large</span><b></b></div>
    <div class="cc-cell" data-k="aaaNormal"><span>AAA Normal</span><b></b></div>
    <div class="cc-cell" data-k="aaaLarge"><span>AAA Large</span><b></b></div>
  </div>
</div>

Extend with AI

Take This Snippet Further With AI

The AI prompt for this one is for Patreon supporters ($3 a month, 7 days free), along with a site with no ads. Supporters enter the passcode once and it is remembered in this browser.

· Become a supporter

What's included

Features

Exact WCAG ratio
Relative-luminance math matching the WCAG 2.x definition.
AA & AAA checks
Pass/fail for 4.5:1, 3:1, 7:1 and 4.5:1 thresholds at a glance.
Normal & large text
Separate results for body copy and large/heading text.
Live preview
Real big and small text rendered in the chosen colors.
Swatch + hex sync
Native color picker and a hex field stay in sync both ways.
Hex normalisation
Accepts #fff or #ffffff, with or without the hash.
Pure functions
Reusable luminance and ratio helpers with no dependencies.
No library
Pure HTML/CSS/JS — no color or accessibility package.

About this UI Snippet

Color Contrast Checker — WCAG Contrast Ratio with AA/AAA Pass-Fail

Screenshot of the Color Contrast Checker snippet rendered live

A color contrast checker measures whether text is legible against its background using the WCAG contrast formula, then shows whether it passes AA and AAA at normal and large sizes. It's the tool every designer and developer needs to keep interfaces accessible. This snippet computes the real WCAG ratio in the browser and previews it live, in plain HTML, CSS, and vanilla JavaScript.

The real WCAG math

Contrast isn't a naive RGB difference — it's based on relative luminance. The script converts each colour to linearised sRGB (applying the c <= 0.03928 ? c/12.92 : ((c+0.055)/1.055)^2.4 transfer function per channel), weights the channels 0.2126 / 0.7152 / 0.0722, and computes the ratio as (L_lighter + 0.05) / (L_darker + 0.05). That's the exact WCAG 2.x definition, so the number you see matches what an audit tool or accessibility checker reports.

Pass/fail at a glance

The four thresholds are evaluated and shown as PASS/FAIL pills: AA Normal needs 4.5:1, AA Large needs 3:1, AAA Normal needs 7:1, and AAA Large needs 4.5:1. "Large" means roughly 18px bold or 24px regular and up. Seeing all four at once tells you not just whether a pairing works, but for which text sizes — so you can confidently use a borderline colour for headings while avoiding it for body copy.

Dual input, always in sync

Each colour has a native <input type="color"> swatch and a hex text field, kept in sync both ways: pick from the OS colour picker and the hex updates; type a hex (3- or 6-digit, with or without #) and the swatch updates. A small clampHex validator normalises shorthand like #fff to #ffffff and ignores invalid input, so the preview never breaks mid-typing.

Live preview

The preview panel renders real big and small text in the chosen colours, so you judge legibility with your eyes as well as the number — the two together catch problems a ratio alone can miss, like a technically-passing pair that still feels muddy. Every change recomputes instantly with no button to press.

Self-contained and portable

The luminance and ratio functions are pure and dependency-free — you can lift them straight into a design-system linter, a Figma plugin, or a build-time check. The whole checker is a compact, framework-agnostic reference for doing WCAG contrast correctly instead of eyeballing it.

Step by step

How to Use

  1. 1
    Paste HTML, CSS, and JSA contrast checker renders with a live text preview and pass/fail grid.
  2. 2
    Choose colorsUse the swatch picker or type a hex for text and background.
  3. 3
    Read the ratioThe big number is the exact WCAG contrast ratio, updated live.
  4. 4
    Check the levelsThe four pills show AA/AAA pass or fail for normal and large text.
  5. 5
    Judge the previewThe sample text renders in your colors so you can sanity-check legibility.
  6. 6
    Reuse the mathLift the luminance/ratio functions into a linter or design tool.

Real-world uses

Common Use Cases

Design token validation
Validate text-on-surface pairs against the exact WCAG 2.x relative-luminance formula before they become part of a design system.
Theme building
Test palette choices from a theme palette generator, with pass or fail for AA and AAA at normal and large sizes.
Accessibility audits
Spot failing combinations before launch, with a live preview rendering both big and small text in the chosen colours.
Dark mode tuning
Verify contrast in both themes alongside a colour mode toggle, where a pair that passes in light often fails in dark.
Brand and marketing legibility
Keep calls to action and headings readable over brand colours, using a colour swatch for the chosen pair.
Related: Cron Expression Builder
See the Cron Expression Builder for a related forms pattern worth pairing with this one.

Got questions?

Frequently Asked Questions

It uses the WCAG 2.x formula: each color is converted to relative luminance by linearising its sRGB channels and weighting them 0.2126 (R), 0.7152 (G), 0.0722 (B), then the ratio is (lighter + 0.05) / (darker + 0.05). This is the same math accessibility auditors use, so the number matches official checkers — it is not a simple RGB or brightness difference.

They are WCAG conformance levels. AA requires 4.5:1 for normal text and 3:1 for large text; AAA is stricter at 7:1 normal and 4.5:1 large. "Large" is about 18px bold or 24px regular and up. AA is the common legal/target baseline; AAA is an enhanced level. The grid shows all four so you know exactly which sizes a color pair is safe for.

The ratio is objective but does not capture everything — a pair can pass yet still feel muddy, or you may want to see how a borderline color reads at real sizes. Rendering actual big and small text in the chosen colors lets you confirm legibility with your eyes, catching issues a single number can hide.

Yes. The luminance(rgb) and ratio(fg, bg) functions are pure and have no dependencies, so you can copy them into a design-system linter, a CI check that fails on low-contrast tokens, a Figma plugin, or a Storybook addon. They take hex/RGB and return the WCAG ratio directly.

Keep the luminance and ratio helpers as plain functions and hold the two colors in state (useState/ref/component fields). Compute the ratio and pass/fail flags as derived values on each change, and bind them to the preview style and the pill classes. Two-way bind the color and hex inputs to the same state. In Tailwind, swap the classes for utilities; the math is unchanged.