Dark Mode Token Pairs
Light, a tuned dark theme and a naive invert side by side, with live contrast ratios for each token pair.
AI prompt
UI design sheet showing the same settings card in three themes side by side: a light theme, a carefully tuned dark theme with dark grey background, slightly lighter card surface, off-white text and a soft light-blue accent, and a naive inverted version with pure black, pure white text and a harsh saturated blue accent that visibly vibrates, small contrast ratio labels under each, flat modern style, clean spacing. Negative: gradients, shadows, text errors, device frames. Reduced motion: static sheet.About this pattern
Definition
A good dark theme is not an inverted light theme. It maps each semantic role, such as background, surface, text and accent, to a new value: dark grey instead of pure black, off-white instead of pure white, and a lighter, slightly desaturated accent.
When to use it
- Background near #121214 rather than #000, with surfaces one step lighter to show elevation.
- Text near #ECECE8 so it does not glare; muted text still above 4.5:1.
- Raise the accent's lightness and lower its saturation so it does not vibrate on dark.
- Define tokens once and switch the values, never the components.
What the controls do
- Three theme buttons; a sample card and a live contrast table.
Motion & timing
- The theme swaps instantly.
- Reduced motion: nothing animates.
Tips & variations
- Test every text and surface pair in both themes, including muted text.
- Respect prefers-color-scheme as the default and let people override it.
Code of this live preview
<div class="bx"><div class="pre" id="pre" role="group" aria-label="Theme"><button data-p="0" aria-pressed="true">Light</button><button data-p="1" aria-pressed="false">Dark</button><button data-p="2" aria-pressed="false">Naive invert</button></div><div class="th" id="th"><div class="cd"><b>Notifications</b><span>Get a summary every morning.</span><u>Turn on</u></div></div><div class="tb" id="tb"></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}
button{font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}a{color:inherit;text-decoration:none}ul,ol,p,h4{margin:0;padding:0}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
.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}
.th{background:var(--bg);border-radius:12px;padding:12px;border:1px solid var(--line)}.cd{background:var(--sf);border-radius:10px;padding:10px 12px;display:grid;gap:3px;font-size:12px}.cd b{color:var(--tx);font-size:13.5px}.cd span{color:var(--mu)}.cd u{text-decoration:none;justify-self:start;margin-top:4px;padding:4px 12px;border-radius:7px;background:var(--ac);color:var(--ai);font-weight:600}
.tb{display:grid;gap:3px;font-size:11.5px}.tb div{display:flex;justify-content:space-between;border-bottom:1px solid var(--line);padding:2px 0}.tb b{font-weight:600}.tb .f{color:#c62f2f}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}const TH=[['#ffffff','#f4f4f1','#171717','#6b6b6b','#2b59d6','#ffffff'],['#121214','#1c1c20','#ecece8','#9a9aa2','#8aa9ff','#10131c'],['#000000','#111111','#ffffff','#777777','#0044ff','#ffffff']],th=document.getElementById('th'),tb=document.getElementById('tb'),bs=[...document.querySelectorAll('#pre button')];let p=0;
const lum=h=>{const c=[1,3,5].map(i=>parseInt(h.substr(i,2),16)/255).map(v=>v<=.03928?v/12.92:Math.pow((v+.055)/1.055,2.4));return .2126*c[0]+.7152*c[1]+.0722*c[2];},cr=(a,b)=>{const x=lum(a),y=lum(b),l=Math.max(x,y),d=Math.min(x,y);return (l+.05)/(d+.05);};
function up(){const t=TH[p];['--bg','--sf','--tx','--mu','--ac','--ai'].forEach((k,i)=>th.style.setProperty(k,t[i]));
const R=[['Text on surface',t[2],t[1],4.5],['Muted on surface',t[3],t[1],4.5],['Button label on accent',t[5],t[4],4.5],['Accent on background',t[4],t[0],3]];
tb.innerHTML=R.map(r=>{const c=cr(r[1],r[2]);return '<div><span>'+r[0]+'</span><b class="'+(c>=r[3]?'':'f')+'">'+c.toFixed(1)+':1 '+(c>=r[3]?'pass':'low')+'</b></div>';}).join('');bs.forEach(b=>b.setAttribute('aria-pressed',String(+b.dataset.p===p)));}
bs.forEach(b=>b.addEventListener('click',()=>{p=+b.dataset.p;up();}));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>Dark Mode Token Pairs — 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}}
button{font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}a{color:inherit;text-decoration:none}ul,ol,p,h4{margin:0;padding:0}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
.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}
.th{background:var(--bg);border-radius:12px;padding:12px;border:1px solid var(--line)}.cd{background:var(--sf);border-radius:10px;padding:10px 12px;display:grid;gap:3px;font-size:12px}.cd b{color:var(--tx);font-size:13.5px}.cd span{color:var(--mu)}.cd u{text-decoration:none;justify-self:start;margin-top:4px;padding:4px 12px;border-radius:7px;background:var(--ac);color:var(--ai);font-weight:600}
.tb{display:grid;gap:3px;font-size:11.5px}.tb div{display:flex;justify-content:space-between;border-bottom:1px solid var(--line);padding:2px 0}.tb b{font-weight:600}.tb .f{color:#c62f2f}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}
</style></head>
<body><div class="stage">
<div class="bx"><div class="pre" id="pre" role="group" aria-label="Theme"><button data-p="0" aria-pressed="true">Light</button><button data-p="1" aria-pressed="false">Dark</button><button data-p="2" aria-pressed="false">Naive invert</button></div><div class="th" id="th"><div class="cd"><b>Notifications</b><span>Get a summary every morning.</span><u>Turn on</u></div></div><div class="tb" id="tb"></div></div>
</div>
<script>
const TH=[['#ffffff','#f4f4f1','#171717','#6b6b6b','#2b59d6','#ffffff'],['#121214','#1c1c20','#ecece8','#9a9aa2','#8aa9ff','#10131c'],['#000000','#111111','#ffffff','#777777','#0044ff','#ffffff']],th=document.getElementById('th'),tb=document.getElementById('tb'),bs=[...document.querySelectorAll('#pre button')];let p=0;
const lum=h=>{const c=[1,3,5].map(i=>parseInt(h.substr(i,2),16)/255).map(v=>v<=.03928?v/12.92:Math.pow((v+.055)/1.055,2.4));return .2126*c[0]+.7152*c[1]+.0722*c[2];},cr=(a,b)=>{const x=lum(a),y=lum(b),l=Math.max(x,y),d=Math.min(x,y);return (l+.05)/(d+.05);};
function up(){const t=TH[p];['--bg','--sf','--tx','--mu','--ac','--ai'].forEach((k,i)=>th.style.setProperty(k,t[i]));
const R=[['Text on surface',t[2],t[1],4.5],['Muted on surface',t[3],t[1],4.5],['Button label on accent',t[5],t[4],4.5],['Accent on background',t[4],t[0],3]];
tb.innerHTML=R.map(r=>{const c=cr(r[1],r[2]);return '<div><span>'+r[0]+'</span><b class="'+(c>=r[3]?'':'f')+'">'+c.toFixed(1)+':1 '+(c>=r[3]?'pass':'low')+'</b></div>';}).join('');bs.forEach(b=>b.setAttribute('aria-pressed',String(+b.dataset.p===p)));}
bs.forEach(b=>b.addEventListener('click',()=>{p=+b.dataset.p;up();}));up();
</script>
</body></html>