Treemap Proportional Blocks
Rectangles sized by value pack into one frame; hover, focus or tap a block for its share. Two datasets to compare.
AI prompt
Clean treemap infographic of a household budget in one rectangle split into seven blocks sized by amount, the largest block Housing in deep blue, then Food, Transport, Savings and smaller blocks for Health, Leisure and Other, using lightness steps of one blue hue from dark to light, thin white gaps between blocks, labels with percentages inside the larger blocks, flat vector style. Negative: rainbow colours, 3D, shadows, text errors, pie charts. Reduced motion: static chart.About this pattern
Definition
A treemap shows parts of a whole as rectangles whose area equals the value. It uses space very efficiently, so it handles many categories where a pie chart would fail, and the biggest items are obvious at a glance. It is poor for precise comparisons of similar sizes.
Purpose & UX principles
- Area, not width or height, encodes the value.
- Sort largest to smallest and split the frame alternately by columns and rows.
- Label the big blocks; show the rest on hover or focus.
- Use one hue family with lightness steps, not a rainbow.
Interaction states
- Two dataset buttons; blocks respond to hover, focus and tap.
Motion specification
- Highlight changes instantly.
- Reduced motion: nothing animates.
Responsive behaviour
- Make every block keyboard focusable and announce its name and share.
- Provide the numbers in a table for people who need exact values.
Code
<div class="bx"><div class="pre" id="pre" role="group" aria-label="Data"><button data-p="0" aria-pressed="true">Budget</button><button data-p="1" aria-pressed="false">Storage</button></div><div class="tm" id="tm"></div><p class="out" id="out" role="status"></p></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}
.tm{position:relative;height:150px;border-radius:10px;overflow:hidden;background:#fff}.tm button{position:absolute;border:2px solid #fff;color:#fff;font-size:11px;font-weight:600;text-align:left;padding:5px 6px;overflow:hidden;line-height:1.25;border-radius:0}.tm button span{display:block;font-weight:400;opacity:.85}.tm button.on{outline:2px solid #171717;outline-offset:-2px;z-index:1}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}const D=[[['Housing',1400],['Food',620],['Transport',410],['Savings',380],['Health',190],['Leisure',160],['Other',90]],[['Photos',212],['Video',148],['Apps',96],['Music',52],['Documents',30],['Mail',18],['Other',9]]],tm=document.getElementById('tm'),bs=[...document.querySelectorAll('#pre button')];let p=0;
function lay(items,x,y,w,h,out){if(items.length===1){out.push([items[0],x,y,w,h]);return;}const tot=items.reduce((s,i)=>s+i[1],0);let acc=0,k=0;while(k<items.length-1&&acc+items[k][1]<tot/2)acc+=items[k++][1];if(k===0){acc=items[0][1];k=1;}const a=items.slice(0,k),b=items.slice(k),f=acc/tot;if(w>=h){lay(a,x,y,w*f,h,out);lay(b,x+w*f,y,w*(1-f),h,out);}else{lay(a,x,y,w,h*f,out);lay(b,x,y+h*f,w,h*(1-f),out);}}
function up(){const it=D[p],tot=it.reduce((s,i)=>s+i[1],0),o=[];lay(it,0,0,100,100,o);tm.innerHTML=o.map((r,i)=>{const pc=Math.round(r[0][1]/tot*100),L=26+i*7;return '<button type="button" data-i="'+i+'" style="left:'+r[1]+'%;top:'+r[2]+'%;width:'+r[3]+'%;height:'+r[4]+'%;background:hsl(215,70%,'+L+'%)" aria-label="'+r[0][0]+', '+pc+' percent">'+(r[3]*r[4]>520?r[0][0]+'<span>'+pc+'%</span>':'')+'</button>';}).join('');
document.getElementById('out').textContent='Hover, focus or tap a block.';tm.querySelectorAll('button').forEach((b,i)=>{const show=()=>{tm.querySelectorAll('button').forEach(k=>k.classList.toggle('on',k===b));document.getElementById('out').textContent=o[i][0][0]+': '+o[i][0][1]+' ('+Math.round(o[i][0][1]/tot*100)+'% of '+tot+').';};b.addEventListener('mouseenter',show);b.addEventListener('focus',show);b.addEventListener('click',show);});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>Treemap Proportional Blocks — 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}
.tm{position:relative;height:150px;border-radius:10px;overflow:hidden;background:#fff}.tm button{position:absolute;border:2px solid #fff;color:#fff;font-size:11px;font-weight:600;text-align:left;padding:5px 6px;overflow:hidden;line-height:1.25;border-radius:0}.tm button span{display:block;font-weight:400;opacity:.85}.tm button.on{outline:2px solid #171717;outline-offset:-2px;z-index:1}
@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="Data"><button data-p="0" aria-pressed="true">Budget</button><button data-p="1" aria-pressed="false">Storage</button></div><div class="tm" id="tm"></div><p class="out" id="out" role="status"></p></div>
</div>
<script>
const D=[[['Housing',1400],['Food',620],['Transport',410],['Savings',380],['Health',190],['Leisure',160],['Other',90]],[['Photos',212],['Video',148],['Apps',96],['Music',52],['Documents',30],['Mail',18],['Other',9]]],tm=document.getElementById('tm'),bs=[...document.querySelectorAll('#pre button')];let p=0;
function lay(items,x,y,w,h,out){if(items.length===1){out.push([items[0],x,y,w,h]);return;}const tot=items.reduce((s,i)=>s+i[1],0);let acc=0,k=0;while(k<items.length-1&&acc+items[k][1]<tot/2)acc+=items[k++][1];if(k===0){acc=items[0][1];k=1;}const a=items.slice(0,k),b=items.slice(k),f=acc/tot;if(w>=h){lay(a,x,y,w*f,h,out);lay(b,x+w*f,y,w*(1-f),h,out);}else{lay(a,x,y,w,h*f,out);lay(b,x,y+h*f,w,h*(1-f),out);}}
function up(){const it=D[p],tot=it.reduce((s,i)=>s+i[1],0),o=[];lay(it,0,0,100,100,o);tm.innerHTML=o.map((r,i)=>{const pc=Math.round(r[0][1]/tot*100),L=26+i*7;return '<button type="button" data-i="'+i+'" style="left:'+r[1]+'%;top:'+r[2]+'%;width:'+r[3]+'%;height:'+r[4]+'%;background:hsl(215,70%,'+L+'%)" aria-label="'+r[0][0]+', '+pc+' percent">'+(r[3]*r[4]>520?r[0][0]+'<span>'+pc+'%</span>':'')+'</button>';}).join('');
document.getElementById('out').textContent='Hover, focus or tap a block.';tm.querySelectorAll('button').forEach((b,i)=>{const show=()=>{tm.querySelectorAll('button').forEach(k=>k.classList.toggle('on',k===b));document.getElementById('out').textContent=o[i][0][0]+': '+o[i][0][1]+' ('+Math.round(o[i][0][1]/tot*100)+'% of '+tot+').';};b.addEventListener('mouseenter',show);b.addEventListener('focus',show);b.addEventListener('click',show);});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>