concept
contrast-ratio — CSS reference
WCAG contrast compares relative luminances of adjacent colors. For ordinary text, AA generally requires 4.5:1; qualifying large text generally requires 3:1. AAA text thresholds are 7:1 and 4.5:1 respectively. Large text is at least 18pt regular or 14pt bold (or equivalent); exceptions exist for incidental text and logotypes. Do not round a failing ratio up to the threshold. Resolve transparency, gradients and actual adjacent backgrounds before applying the calculation.
Formal Syntax & Structure
css syntaxcss
(Llighter + 0.05) / (Ldarker + 0.05)Working Example
Example Snippetcss
<!doctype html><html lang="en"><meta charset="utf-8"><title>CSS accessible theme lab</title><style>body {margin:0} .sample {height:12px;background:#006050}#a{color:#767676;background:#fff} #b{color:#777;background:#fff}</style><body><p id="a">Sample A: #767676</p><p id="b">Sample B: #777777</p><script>function luminance(rgb) { return rgb.map(n => {const s=n/255; return s<=0.04045?s/12.92:((s+0.055)/1.055)**2.4;}).reduce((sum,v,i)=>sum+v*[0.2126,0.7152,0.0722][i],0); }function contrast(a,b){const x=luminance(a),y=luminance(b);return (Math.max(x,y)+0.05)/(Math.min(x,y)+0.05);}function channels(s){return s.match(/\d+/g).map(Number);}function sampleContrast(id){const s=getComputedStyle(document.getElementById(id));return contrast(channels(s.color),channels(s.backgroundColor));}</script></body></html>