Heatmap Calendar
A grid of days shaded by activity, with a tooltip and arrow-key navigation.
AI prompt
Build a heatmap of 12 weeks by 7 days (84 cells) laid out as a CSS grid with grid-auto-flow column and 7 rows, cell size about 18px with 3px gaps and 4px radius. Generate deterministic activity counts from 0 to 12 and map them to five shades from #ebebe7 to #171717 (levels 0 to 4). A legend row "Less" to "More" shows the five shades. Hovering a cell shows a tooltip "N contributions on Mon, 14 Apr" and a 2px dark outline. The grid is a single tab stop with role grid semantics: arrow keys move the active cell, Home and End go to the first and last cell of the row, and aria-activedescendant points at the active cell; each cell has an aria-label with the date and count. On load fade the cells in column by column with a 12ms stagger over 500ms. Under prefers-reduced-motion skip the fade.About this pattern
Definition
Each cell is one day, arranged in weekly columns. Darker cells mean more activity. Hovering a cell shows the date and count.
Purpose & UX principles
- Reveals rhythm and gaps over months at a glance.
- Intensity uses five lightness steps with a legend, and each cell has a text label, so it is not colour only.
- With 84 cells a tab stop each would be hostile: the grid is one tab stop with arrow-key navigation.
Interaction states
- Level 0 to 4, hovered cell (outline), keyboard-active cell (outline and tooltip).
Motion specification
- Cells fade in column by column over 500ms with a 12ms stagger.
- Hover outline appears in 100ms.
- Reduced motion: no fade-in.
Responsive behaviour
- Cells size to the container width; the grid never scrolls horizontally at phone widths.
- Touch: tapping a cell shows its tooltip.
Code
<div class="box">
<div class="hm" id="hm" role="grid" tabindex="0" aria-label="Activity over the last 12 weeks. Use arrow keys."></div>
<div class="foot"><span id="tip" aria-live="polite">Hover or use arrow keys</span><span class="lg">Less <i style="background:#ebebe7"></i><i style="background:#c8c8c3"></i><i style="background:#8c8c87"></i><i style="background:#4a4a47"></i><i style="background:#171717"></i> More</span></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}
.box{width:min(300px,100%)}
.hm{display:grid;grid-template-rows:repeat(7,1fr);grid-auto-flow:column;grid-auto-columns:1fr;gap:3px;border-radius:6px}
.hm:focus-visible{outline:2px solid var(--accent);outline-offset:4px}
.c{aspect-ratio:1;border-radius:4px;animation:in .5s ease-out backwards;animation-delay:calc(var(--c) * 12ms)}
.c:hover,.c.act{outline:2px solid #171717;outline-offset:1px}
@keyframes in{from{opacity:0}}
.foot{display:flex;justify-content:space-between;align-items:center;gap:8px;margin-top:10px;font-size:11px;color:var(--muted)}
.lg{display:inline-flex;align-items:center;gap:3px;white-space:nowrap}
.lg i{width:10px;height:10px;border-radius:2px;display:inline-block}
@media (prefers-reduced-motion:reduce){.c{animation:none}}const hm=document.getElementById('hm'),tip=document.getElementById('tip'),cols=12,cells=[],shade=['#ebebe7','#c8c8c3','#8c8c87','#4a4a47','#171717'];
const days=['Mon','Tue','Wed','Thu','Fri','Sat','Sun'],start=new Date(2025,0,6);
let seed=7;const rnd=()=>(seed=(seed*9301+49297)%233280)/233280;
for(let c=0;c<cols;c++)for(let r=0;r<7;r++){const n=Math.floor(Math.pow(rnd(),1.6)*13),lvl=n===0?0:n<4?1:n<7?2:n<10?3:4,dt=new Date(start.getTime()+(c*7+r)*864e5);
const el=document.createElement('div');el.className='c';el.id='c'+cells.length;el.setAttribute('role','gridcell');el.style.background=shade[lvl];el.style.setProperty('--c',c);
const txt=n+' contribution'+(n===1?'':'s')+' on '+days[r]+', '+dt.getDate()+' '+dt.toLocaleString('en',{month:'short'});el.setAttribute('aria-label',txt);el.dataset.t=txt;hm.append(el);cells.push(el);
el.addEventListener('pointerenter',()=>{tip.textContent=txt;});el.addEventListener('click',()=>{setAct(+el.id.slice(1));});}
let act=0;function setAct(i){cells[act].classList.remove('act');act=i;cells[i].classList.add('act');hm.setAttribute('aria-activedescendant',cells[i].id);tip.textContent=cells[i].dataset.t;}
hm.addEventListener('keydown',e=>{const r=act%7,m={ArrowDown:r<6?act+1:act,ArrowUp:r>0?act-1:act,ArrowRight:act+7<cells.length?act+7:act,ArrowLeft:act-7>=0?act-7:act,Home:act-r,End:act-r+6};if(e.key in m){e.preventDefault();setAct(m[e.key]);}});
hm.addEventListener('focus',()=>setAct(act));hm.addEventListener('blur',()=>cells[act].classList.remove('act'));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>Heatmap Calendar — 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}}
.box{width:min(300px,100%)}
.hm{display:grid;grid-template-rows:repeat(7,1fr);grid-auto-flow:column;grid-auto-columns:1fr;gap:3px;border-radius:6px}
.hm:focus-visible{outline:2px solid var(--accent);outline-offset:4px}
.c{aspect-ratio:1;border-radius:4px;animation:in .5s ease-out backwards;animation-delay:calc(var(--c) * 12ms)}
.c:hover,.c.act{outline:2px solid #171717;outline-offset:1px}
@keyframes in{from{opacity:0}}
.foot{display:flex;justify-content:space-between;align-items:center;gap:8px;margin-top:10px;font-size:11px;color:var(--muted)}
.lg{display:inline-flex;align-items:center;gap:3px;white-space:nowrap}
.lg i{width:10px;height:10px;border-radius:2px;display:inline-block}
@media (prefers-reduced-motion:reduce){.c{animation:none}}
</style></head>
<body><div class="stage">
<div class="box">
<div class="hm" id="hm" role="grid" tabindex="0" aria-label="Activity over the last 12 weeks. Use arrow keys."></div>
<div class="foot"><span id="tip" aria-live="polite">Hover or use arrow keys</span><span class="lg">Less <i style="background:#ebebe7"></i><i style="background:#c8c8c3"></i><i style="background:#8c8c87"></i><i style="background:#4a4a47"></i><i style="background:#171717"></i> More</span></div>
</div>
</div>
<script>
const hm=document.getElementById('hm'),tip=document.getElementById('tip'),cols=12,cells=[],shade=['#ebebe7','#c8c8c3','#8c8c87','#4a4a47','#171717'];
const days=['Mon','Tue','Wed','Thu','Fri','Sat','Sun'],start=new Date(2025,0,6);
let seed=7;const rnd=()=>(seed=(seed*9301+49297)%233280)/233280;
for(let c=0;c<cols;c++)for(let r=0;r<7;r++){const n=Math.floor(Math.pow(rnd(),1.6)*13),lvl=n===0?0:n<4?1:n<7?2:n<10?3:4,dt=new Date(start.getTime()+(c*7+r)*864e5);
const el=document.createElement('div');el.className='c';el.id='c'+cells.length;el.setAttribute('role','gridcell');el.style.background=shade[lvl];el.style.setProperty('--c',c);
const txt=n+' contribution'+(n===1?'':'s')+' on '+days[r]+', '+dt.getDate()+' '+dt.toLocaleString('en',{month:'short'});el.setAttribute('aria-label',txt);el.dataset.t=txt;hm.append(el);cells.push(el);
el.addEventListener('pointerenter',()=>{tip.textContent=txt;});el.addEventListener('click',()=>{setAct(+el.id.slice(1));});}
let act=0;function setAct(i){cells[act].classList.remove('act');act=i;cells[i].classList.add('act');hm.setAttribute('aria-activedescendant',cells[i].id);tip.textContent=cells[i].dataset.t;}
hm.addEventListener('keydown',e=>{const r=act%7,m={ArrowDown:r<6?act+1:act,ArrowUp:r>0?act-1:act,ArrowRight:act+7<cells.length?act+7:act,ArrowLeft:act-7>=0?act-7:act,Home:act-r,End:act-r+6};if(e.key in m){e.preventDefault();setAct(m[e.key]);}});
hm.addEventListener('focus',()=>setAct(act));hm.addEventListener('blur',()=>cells[act].classList.remove('act'));
</script>
</body></html>