Stat Card with Trend Delta
KPI cards with value, change since last period and a sparkline, where a falling number can be good news.
AI prompt
Clean dashboard of three KPI cards in a row, Revenue 48.2k with a green up arrow plus 12 percent and an upward sparkline, Users 8,431 with a grey flat arrow 0 percent, and Churn 2.1 percent with a green down arrow minus 0.4 points labelled improved with a falling sparkline, light theme, rounded cards, thin dividers, small labels above values, flat modern style. Negative: gradients, 3D, clutter, text errors. Reduced motion: static design.About this pattern
Definition
A stat card shows one number with its change over time. The arrow shows direction, but colour and wording must show whether the change is good or bad: a rising churn rate is bad and a falling one is good. Mixing the two up is a common dashboard mistake.
Purpose & UX principles
- Show the value large, then the delta with an arrow, a sign and the period.
- Colour by meaning (good, bad, neutral), not by direction.
- Add a small sparkline to show the shape of the trend.
- Include text like improved or worse in the accessible name so colour is not the only cue.
Interaction states
- Switch the period between 7 days and 30 days.
Motion specification
- Values change instantly.
- Reduced motion: nothing animates.
Responsive behaviour
- Use consistent number formatting and units.
- Keep sparklines unlabelled but supply the overall change in text.
Code
<div class="bx"><div class="pre" id="pre" role="group" aria-label="Period"><button data-p="0" aria-pressed="true">7 days</button><button data-p="1" aria-pressed="false">30 days</button></div><div class="gd" id="gd"></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}
.gd{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:8px}.sc{border:1px solid var(--line);border-radius:12px;background:#fff;padding:8px;display:grid;gap:3px;font-size:11px}.sc small{color:var(--muted);font-size:10.5px}.sc strong{font-size:18px;line-height:1.1}.dl{font-weight:700;display:flex;align-items:center;gap:3px;font-size:10.5px}.dl.g{color:#1a7f45}.dl.b{color:#c0261a}.dl.n{color:#6b6b6b}.sc svg{width:100%;height:26px;display:block}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}const D=[[['Revenue','48.2k',12,1],['Users','8,431',0,0],['Churn','2.1%',-.4,-1]],[['Revenue','181k',-3,1],['Users','31.9k',9,1],['Churn','2.6%',.3,-1]]],gd=document.getElementById('gd'),bs=[...document.querySelectorAll('#pre button')];let p=0,s=3;const R=()=>(s=(s*1664525+1013904223)%4294967296)/4294967296;
function spark(d,dir){const n=10;let y=[],v=50;for(let i=0;i<n;i++){v+=(d>0?3:d<0?-3:0)+(R()-.5)*14;y.push(v);}const mn=Math.min(...y),mx=Math.max(...y)||1;return y.map((q,i)=>(i*(100/(n-1))).toFixed(1)+','+(24-(q-mn)/((mx-mn)||1)*22).toFixed(1)).join(' ');}
function up(){s=7;gd.innerHTML=D[p].map(c=>{const d=c[2],good=d===0?0:(d*c[3]>0?1:-1),cls=good>0?'g':good<0?'b':'n',arr=d>0?'\u25B2':d<0?'\u25BC':'\u25B6',txt=good>0?'better':good<0?'worse':'no change',col=good>0?'#1a7f45':good<0?'#c0261a':'#8a8a86';
return '<div class="sc" role="group" aria-label="'+c[0]+' '+c[1]+', '+(d===0?'unchanged':Math.abs(d)+(c[0]==='Churn'?' points ':' percent ')+(d>0?'up':'down'))+', '+txt+'"><small>'+c[0]+'</small><strong>'+c[1]+'</strong><span class="dl '+cls+'"><span aria-hidden="true">'+arr+'</span>'+(d>0?'+':d<0?'\u2212':'')+Math.abs(d)+(c[0]==='Churn'?' pt':'%')+' \u00b7 '+txt+'</span><svg viewBox="0 0 100 26" preserveAspectRatio="none" aria-hidden="true"><polyline fill="none" stroke="'+col+'" stroke-width="2" points="'+spark(d,c[3])+'"/></svg></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>Stat Card with Trend Delta — 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}
.gd{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:8px}.sc{border:1px solid var(--line);border-radius:12px;background:#fff;padding:8px;display:grid;gap:3px;font-size:11px}.sc small{color:var(--muted);font-size:10.5px}.sc strong{font-size:18px;line-height:1.1}.dl{font-weight:700;display:flex;align-items:center;gap:3px;font-size:10.5px}.dl.g{color:#1a7f45}.dl.b{color:#c0261a}.dl.n{color:#6b6b6b}.sc svg{width:100%;height:26px;display:block}
@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="Period"><button data-p="0" aria-pressed="true">7 days</button><button data-p="1" aria-pressed="false">30 days</button></div><div class="gd" id="gd"></div></div>
</div>
<script>
const D=[[['Revenue','48.2k',12,1],['Users','8,431',0,0],['Churn','2.1%',-.4,-1]],[['Revenue','181k',-3,1],['Users','31.9k',9,1],['Churn','2.6%',.3,-1]]],gd=document.getElementById('gd'),bs=[...document.querySelectorAll('#pre button')];let p=0,s=3;const R=()=>(s=(s*1664525+1013904223)%4294967296)/4294967296;
function spark(d,dir){const n=10;let y=[],v=50;for(let i=0;i<n;i++){v+=(d>0?3:d<0?-3:0)+(R()-.5)*14;y.push(v);}const mn=Math.min(...y),mx=Math.max(...y)||1;return y.map((q,i)=>(i*(100/(n-1))).toFixed(1)+','+(24-(q-mn)/((mx-mn)||1)*22).toFixed(1)).join(' ');}
function up(){s=7;gd.innerHTML=D[p].map(c=>{const d=c[2],good=d===0?0:(d*c[3]>0?1:-1),cls=good>0?'g':good<0?'b':'n',arr=d>0?'\u25B2':d<0?'\u25BC':'\u25B6',txt=good>0?'better':good<0?'worse':'no change',col=good>0?'#1a7f45':good<0?'#c0261a':'#8a8a86';
return '<div class="sc" role="group" aria-label="'+c[0]+' '+c[1]+', '+(d===0?'unchanged':Math.abs(d)+(c[0]==='Churn'?' points ':' percent ')+(d>0?'up':'down'))+', '+txt+'"><small>'+c[0]+'</small><strong>'+c[1]+'</strong><span class="dl '+cls+'"><span aria-hidden="true">'+arr+'</span>'+(d>0?'+':d<0?'\u2212':'')+Math.abs(d)+(c[0]==='Churn'?' pt':'%')+' \u00b7 '+txt+'</span><svg viewBox="0 0 100 26" preserveAspectRatio="none" aria-hidden="true"><polyline fill="none" stroke="'+col+'" stroke-width="2" points="'+spark(d,c[3])+'"/></svg></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>