concept

relative-luminance — CSS reference

Relative luminance quantifies the light output of a color relative to reference white. For an opaque sRGB triplet, normalize each channel to 0–1 and decode the transfer curve before applying the channel weights. HSL lightness and OKLCH lightness are not substitutes. This example deliberately accepts only opaque sRGB values.

Formal Syntax & Structure

css syntaxcss
L = 0.2126 R + 0.7152 G + 0.0722 B (linearized sRGB channels)

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:#000;background:#fff} #b{color:#777;background:#fff}</style><body><p id="a">Black on white</p><p id="b">Gray on white</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>
CSS Reference