Semantic Colour Roles
Name colours by job (primary, success, warning, danger, surface, text) and see them swap for light and dark themes.
AI prompt
Design system documentation page showing semantic colour roles as a clean grid of rounded swatches with labels beneath: surface, text, primary blue, success green, warning amber, danger red, each with its hex value, shown twice side by side for light and dark themes, a small sample card with a primary button and a danger alert below, flat vector style, plenty of white space. Negative: gradients, shadows, unrelated hues, text errors, decorative frames. Reduced motion: static page.About this pattern
Definition
A palette is a list of colours; a set of roles is a list of jobs. Naming colours by role (surface, text, primary, danger) lets a whole theme switch by changing the values behind the names, and keeps meaning consistent across the product.
When to use it
- Keep a small fixed set: surface, text, muted text, primary, success, warning and danger.
- Each role needs a light and a dark value that both pass contrast against their surface.
- Never use the danger colour for decoration; its job is to mean error.
What the controls do
- Light and Dark theme buttons, a table of roles with their values, and a live sample using them.
Motion & timing
- Colours cross-fade over 200ms when the theme changes.
- Reduced motion: instant switch.
Tips & variations
- Test every role against its background in both themes.
- Do not carry meaning by colour alone; pair with an icon or word.
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></div><div class="th" id="th"><div class="sw" id="sw"></div><div class="sm"><b>Sample card</b><span>Roles keep meaning across themes.</span><div><button type="button" class="pb">Save</button><em class="al">Payment failed</em></div></div></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}
.th{--bg:#fff;--tx:#1b1b1f;--mu:#6a6a72;--pr:#2b59d6;--ok:#1f8a4c;--wa:#b86e00;--er:#c62f2f;border:1px solid var(--line);border-radius:12px;padding:10px;background:var(--bg);color:var(--tx);display:grid;gap:8px;transition:background .2s,color .2s}
.sw{display:grid;grid-template-columns:repeat(4,1fr);gap:5px}.sw div{display:grid;gap:2px;font-size:10px;color:var(--mu)}.sw i{height:24px;border-radius:6px;border:1px solid rgba(128,128,128,.3)}.sw b{color:var(--tx);font-size:10.5px}
.sm{border:1px solid rgba(128,128,128,.3);border-radius:10px;padding:8px 10px;display:grid;gap:3px;font-size:12px}.sm span{color:var(--mu)}.sm div{display:flex;gap:8px;align-items:center;margin-top:3px}
.pb{height:26px;padding:0 12px;border-radius:7px;background:var(--pr);color:#fff;font-weight:600;font-size:12px}.al{font-style:normal;font-size:11.5px;color:var(--er);background:color-mix(in srgb,var(--er) 14%,transparent);padding:3px 8px;border-radius:6px}
@media (prefers-reduced-motion:reduce){.th{transition:none}}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}const T=[{bg:'#ffffff',tx:'#1b1b1f',mu:'#6a6a72',pr:'#2b59d6',ok:'#1f8a4c',wa:'#b86e00',er:'#c62f2f'},{bg:'#16171b',tx:'#f2f2f5',mu:'#a1a1aa',pr:'#7c9cff',ok:'#4cc38a',wa:'#f2b04a',er:'#ff7b7b'}],N={bg:'Surface',tx:'Text',mu:'Muted',pr:'Primary',ok:'Success',wa:'Warning',er:'Danger'},th=document.getElementById('th'),sw=document.getElementById('sw'),bs=[...document.querySelectorAll('#pre button')];let p=0;
function up(){const t=T[p];Object.keys(t).forEach(k=>th.style.setProperty('--'+k,t[k]));sw.innerHTML=Object.keys(t).map(k=>'<div><i style="background:'+t[k]+'"></i><b>'+N[k]+'</b>'+t[k]+'</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>Semantic Colour Roles — 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}
.th{--bg:#fff;--tx:#1b1b1f;--mu:#6a6a72;--pr:#2b59d6;--ok:#1f8a4c;--wa:#b86e00;--er:#c62f2f;border:1px solid var(--line);border-radius:12px;padding:10px;background:var(--bg);color:var(--tx);display:grid;gap:8px;transition:background .2s,color .2s}
.sw{display:grid;grid-template-columns:repeat(4,1fr);gap:5px}.sw div{display:grid;gap:2px;font-size:10px;color:var(--mu)}.sw i{height:24px;border-radius:6px;border:1px solid rgba(128,128,128,.3)}.sw b{color:var(--tx);font-size:10.5px}
.sm{border:1px solid rgba(128,128,128,.3);border-radius:10px;padding:8px 10px;display:grid;gap:3px;font-size:12px}.sm span{color:var(--mu)}.sm div{display:flex;gap:8px;align-items:center;margin-top:3px}
.pb{height:26px;padding:0 12px;border-radius:7px;background:var(--pr);color:#fff;font-weight:600;font-size:12px}.al{font-style:normal;font-size:11.5px;color:var(--er);background:color-mix(in srgb,var(--er) 14%,transparent);padding:3px 8px;border-radius:6px}
@media (prefers-reduced-motion:reduce){.th{transition:none}}
@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></div><div class="th" id="th"><div class="sw" id="sw"></div><div class="sm"><b>Sample card</b><span>Roles keep meaning across themes.</span><div><button type="button" class="pb">Save</button><em class="al">Payment failed</em></div></div></div></div>
</div>
<script>
const T=[{bg:'#ffffff',tx:'#1b1b1f',mu:'#6a6a72',pr:'#2b59d6',ok:'#1f8a4c',wa:'#b86e00',er:'#c62f2f'},{bg:'#16171b',tx:'#f2f2f5',mu:'#a1a1aa',pr:'#7c9cff',ok:'#4cc38a',wa:'#f2b04a',er:'#ff7b7b'}],N={bg:'Surface',tx:'Text',mu:'Muted',pr:'Primary',ok:'Success',wa:'Warning',er:'Danger'},th=document.getElementById('th'),sw=document.getElementById('sw'),bs=[...document.querySelectorAll('#pre button')];let p=0;
function up(){const t=T[p];Object.keys(t).forEach(k=>th.style.setProperty('--'+k,t[k]));sw.innerHTML=Object.keys(t).map(k=>'<div><i style="background:'+t[k]+'"></i><b>'+N[k]+'</b>'+t[k]+'</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>