Contrast Ratio Checker
Pick a text and background colour and see the WCAG contrast ratio with AA and AAA verdicts.
AI prompt
Clean infographic for designers titled Contrast Ratio: two large colour swatches, dark text on a pale background and mid-grey text on white, each with the word Aa in a large size and a ratio such as 12.6:1 and 2.8:1 beneath, green pass badges for AA and AAA on the first and a red fail badge on the second, flat vector style, plenty of white space, no gradients or shadows. Negative: decorative clutter, tiny text, wrong numbers, photos. Reduced motion: static graphic.About this pattern
Definition
Contrast ratio compares the brightness of two colours on a scale from 1 to 21. WCAG asks for at least 4.5 for normal text and 3 for large text at level AA, and 7 and 4.5 at AAA. It is computed from relative luminance, not from how different the hues look.
When to use it
- Use it before choosing grey text on white; light greys often fail.
- Large text (24px or 19px bold) only needs 3.
- Colour alone should never be the only signal; pair it with shape or text.
What the controls do
- Two colour pickers, four sample pairs and four pass or fail badges.
Motion & timing
- Results update on every change; there is no animation.
- Reduced motion: nothing to change.
Tips & variations
- Test both light and dark themes separately.
- Test the hover and disabled states as well as the default one.
Code of this live preview
<div class="bx"><div class="rw" style="grid-auto-flow:column;justify-content:start;gap:14px"><label class="rw">Text<input id="fg" type="color" value="#767676"></label><label class="rw">Background<input id="bg" type="color" value="#ffffff"></label></div><div class="pr" id="pr"><b>Aa</b> <span>The quick brown fox jumps over the lazy dog.</span></div><div class="bd" id="bd"></div><div class="pre" id="pre" role="group" aria-label="Pairs"><button data-p="0" aria-pressed="true">Dark on pale</button><button data-p="1" aria-pressed="false">Grey on white</button><button data-p="2" aria-pressed="false">White on blue</button><button data-p="3" aria-pressed="false">Black on yellow</button></div></div>:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
.pre{display:flex;gap:5px;flex-wrap:wrap}.pre button{height:28px;padding:0 11px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.bx{width:min(330px,100%);display:grid;gap:9px}.rw{display:grid;gap:3px;font-size:11.5px;color:var(--muted)}.rw b{color:var(--text);font-weight:600}input[type=range]{width:100%;accent-color:#171717}
.out{font-size:12px;color:var(--muted);line-height:1.4}.out b{color:var(--text)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
input[type=color]{width:54px;height:30px;border:1px solid var(--line);border-radius:8px;padding:2px;background:var(--surface);cursor:pointer}
.pr{display:flex;gap:10px;align-items:center;padding:12px 14px;border-radius:12px;border:1px solid var(--line);font-size:14px}.pr b{font-size:30px;line-height:1}
.bd{display:flex;gap:6px;flex-wrap:wrap;align-items:center;font-size:12px}.bd span{padding:3px 9px;border-radius:99px;font-weight:600}.bd .ok{background:#dcf5e3;color:#14502a}.bd .no{background:#fbe0dd;color:#7a1d14}.bd strong{font-size:16px;margin-right:4px}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}const Pr=[['#1f2430','#f4f5f8'],['#8c8c8c','#ffffff'],['#ffffff','#2b59d6'],['#111111','#ffe14d']],fg=document.getElementById('fg'),bg=document.getElementById('bg'),pr=document.getElementById('pr'),bd=document.getElementById('bd'),bs=[...document.querySelectorAll('#pre button')];let p=0;
const lum=h=>{const [r,g,b]=[1,3,5].map(i=>parseInt(h.slice(i,i+2),16)/255).map(v=>v<=.03928?v/12.92:Math.pow((v+.055)/1.055,2.4));return .2126*r+.7152*g+.0722*b};
function up(){const a=lum(fg.value),b=lum(bg.value),r=(Math.max(a,b)+.05)/(Math.min(a,b)+.05);pr.style.color=fg.value;pr.style.background=bg.value;
const t=(n,lab)=>'<span class="'+(r>=n?'ok':'no')+'">'+lab+' '+(r>=n?'pass':'fail')+'</span>';bd.innerHTML='<strong>'+r.toFixed(2)+':1</strong>'+t(4.5,'AA text')+t(3,'AA large')+t(7,'AAA text')+t(4.5,'AAA large');bs.forEach(b=>b.setAttribute('aria-pressed',String(+b.dataset.p===p)));}
bs.forEach(b=>b.addEventListener('click',()=>{p=+b.dataset.p;fg.value=Pr[p][0];bg.value=Pr[p][1];up();}));[fg,bg].forEach(i=>i.addEventListener('input',()=>{bs.forEach(k=>k.setAttribute('aria-pressed','false'));up();}));fg.value=Pr[0][0];bg.value=Pr[0][1];up();One complete HTML file. Save it and open it in a browser.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Contrast Ratio Checker — demo</title>
<style>
:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
html,body{height:100%}body{background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Inter,Roboto,sans-serif;-webkit-font-smoothing:antialiased}.stage{min-height:100%;display:grid;place-items:center;padding:24px}button,input,select,textarea{font-family:inherit}@media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}}
.pre{display:flex;gap:5px;flex-wrap:wrap}.pre button{height:28px;padding:0 11px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.bx{width:min(330px,100%);display:grid;gap:9px}.rw{display:grid;gap:3px;font-size:11.5px;color:var(--muted)}.rw b{color:var(--text);font-weight:600}input[type=range]{width:100%;accent-color:#171717}
.out{font-size:12px;color:var(--muted);line-height:1.4}.out b{color:var(--text)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
input[type=color]{width:54px;height:30px;border:1px solid var(--line);border-radius:8px;padding:2px;background:var(--surface);cursor:pointer}
.pr{display:flex;gap:10px;align-items:center;padding:12px 14px;border-radius:12px;border:1px solid var(--line);font-size:14px}.pr b{font-size:30px;line-height:1}
.bd{display:flex;gap:6px;flex-wrap:wrap;align-items:center;font-size:12px}.bd span{padding:3px 9px;border-radius:99px;font-weight:600}.bd .ok{background:#dcf5e3;color:#14502a}.bd .no{background:#fbe0dd;color:#7a1d14}.bd strong{font-size:16px;margin-right:4px}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}
</style></head>
<body><div class="stage">
<div class="bx"><div class="rw" style="grid-auto-flow:column;justify-content:start;gap:14px"><label class="rw">Text<input id="fg" type="color" value="#767676"></label><label class="rw">Background<input id="bg" type="color" value="#ffffff"></label></div><div class="pr" id="pr"><b>Aa</b> <span>The quick brown fox jumps over the lazy dog.</span></div><div class="bd" id="bd"></div><div class="pre" id="pre" role="group" aria-label="Pairs"><button data-p="0" aria-pressed="true">Dark on pale</button><button data-p="1" aria-pressed="false">Grey on white</button><button data-p="2" aria-pressed="false">White on blue</button><button data-p="3" aria-pressed="false">Black on yellow</button></div></div>
</div>
<script>
const Pr=[['#1f2430','#f4f5f8'],['#8c8c8c','#ffffff'],['#ffffff','#2b59d6'],['#111111','#ffe14d']],fg=document.getElementById('fg'),bg=document.getElementById('bg'),pr=document.getElementById('pr'),bd=document.getElementById('bd'),bs=[...document.querySelectorAll('#pre button')];let p=0;
const lum=h=>{const [r,g,b]=[1,3,5].map(i=>parseInt(h.slice(i,i+2),16)/255).map(v=>v<=.03928?v/12.92:Math.pow((v+.055)/1.055,2.4));return .2126*r+.7152*g+.0722*b};
function up(){const a=lum(fg.value),b=lum(bg.value),r=(Math.max(a,b)+.05)/(Math.min(a,b)+.05);pr.style.color=fg.value;pr.style.background=bg.value;
const t=(n,lab)=>'<span class="'+(r>=n?'ok':'no')+'">'+lab+' '+(r>=n?'pass':'fail')+'</span>';bd.innerHTML='<strong>'+r.toFixed(2)+':1</strong>'+t(4.5,'AA text')+t(3,'AA large')+t(7,'AAA text')+t(4.5,'AAA large');bs.forEach(b=>b.setAttribute('aria-pressed',String(+b.dataset.p===p)));}
bs.forEach(b=>b.addEventListener('click',()=>{p=+b.dataset.p;fg.value=Pr[p][0];bg.value=Pr[p][1];up();}));[fg,bg].forEach(i=>i.addEventListener('input',()=>{bs.forEach(k=>k.setAttribute('aria-pressed','false'));up();}));fg.value=Pr[0][0];bg.value=Pr[0][1];up();
</script>
</body></html>