Sticky Table Header & Column
A wide table scrolls both ways while its header row and first column stay pinned, with edge shadows.
AI prompt
Clean UI mockup of a wide data table in a rounded scroll area, the first column of product names and the top header row both pinned with a soft shadow along their inner edges, pale grey header background, thin row dividers, numeric columns right aligned, the table partially scrolled so that columns in the middle are visible while the labels stay in place, light theme, flat modern style. Negative: heavy shadows, gradients, clutter, text errors, device frames. Reduced motion: static design.About this pattern
Definition
Large tables are hard to read when you lose the column titles or row labels as you scroll. position: sticky pins the header row to the top and the first column to the left, like frozen panes in a spreadsheet, so each cell stays linked to its labels.
Purpose & UX principles
- Give th in the header top: 0 and the first-column cells left: 0, with an opaque background and a higher z-index at the corner.
- Put the table in a scroll container with a fixed height.
- Add a shadow or border to the pinned edges only while content scrolls beneath them.
- Keep the pinned column narrow so there is room for data.
Interaction states
- A table with 8 columns and 12 rows; scroll or use the keyboard.
Motion specification
- Shadows fade in over 150ms when you scroll.
- Reduced motion: no fade.
Responsive behaviour
- Make the scroll container focusable so keyboard users can scroll it.
- On small screens consider turning rows into cards instead.
Code
<div class="bx"><div class="sc" id="sc" tabindex="0" aria-label="Sales table, scrollable"><table id="tb"></table></div><p class="out" id="out"></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}
.sc{height:170px;overflow:auto;border:1px solid var(--line);border-radius:12px;background:#fff;position:relative}table{border-collapse:separate;border-spacing:0;font-size:12px;min-width:620px}th,td{padding:7px 12px;border-bottom:1px solid var(--line);white-space:nowrap;text-align:right;background:#fff}thead th{position:sticky;top:0;background:#f4f4f1;z-index:2;font-weight:600}th:first-child,td:first-child{position:sticky;left:0;text-align:left;z-index:1;font-weight:600}thead th:first-child{z-index:3}.sc.sx td:first-child,.sc.sx th:first-child{box-shadow:6px 0 8px -6px rgba(0,0,0,.25)}.sc.sy thead th{box-shadow:0 6px 8px -6px rgba(0,0,0,.25)}.out{font-size:12px;color:var(--muted)}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}const sc=document.getElementById('sc'),tb=document.getElementById('tb'),C=['Product','Jan','Feb','Mar','Apr','May','Jun','Jul'],R=['Lamp','Chair','Desk','Shelf','Mug','Rug','Clock','Vase','Frame','Stool','Tray','Plant'];let s=7;const r=()=>(s=(s*1664525+1013904223)%4294967296)/4294967296;
tb.innerHTML='<thead><tr>'+C.map(c=>'<th scope="col">'+c+'</th>').join('')+'</tr></thead><tbody>'+R.map(n=>'<tr><td>'+n+'</td>'+C.slice(1).map(()=>'<td>'+(100+Math.floor(r()*900))+'</td>').join('')+'</tr>').join('')+'</tbody>';
function up(){sc.classList.toggle('sx',sc.scrollLeft>2);sc.classList.toggle('sy',sc.scrollTop>2);document.getElementById('out').textContent=sc.scrollLeft>2||sc.scrollTop>2?'Header row and first column stay pinned while you scroll.':'Scroll the table sideways or down.';}
sc.addEventListener('scroll',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>Sticky Table Header & Column — 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}
.sc{height:170px;overflow:auto;border:1px solid var(--line);border-radius:12px;background:#fff;position:relative}table{border-collapse:separate;border-spacing:0;font-size:12px;min-width:620px}th,td{padding:7px 12px;border-bottom:1px solid var(--line);white-space:nowrap;text-align:right;background:#fff}thead th{position:sticky;top:0;background:#f4f4f1;z-index:2;font-weight:600}th:first-child,td:first-child{position:sticky;left:0;text-align:left;z-index:1;font-weight:600}thead th:first-child{z-index:3}.sc.sx td:first-child,.sc.sx th:first-child{box-shadow:6px 0 8px -6px rgba(0,0,0,.25)}.sc.sy thead th{box-shadow:0 6px 8px -6px rgba(0,0,0,.25)}.out{font-size:12px;color:var(--muted)}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}
</style></head>
<body><div class="stage">
<div class="bx"><div class="sc" id="sc" tabindex="0" aria-label="Sales table, scrollable"><table id="tb"></table></div><p class="out" id="out"></p></div>
</div>
<script>
const sc=document.getElementById('sc'),tb=document.getElementById('tb'),C=['Product','Jan','Feb','Mar','Apr','May','Jun','Jul'],R=['Lamp','Chair','Desk','Shelf','Mug','Rug','Clock','Vase','Frame','Stool','Tray','Plant'];let s=7;const r=()=>(s=(s*1664525+1013904223)%4294967296)/4294967296;
tb.innerHTML='<thead><tr>'+C.map(c=>'<th scope="col">'+c+'</th>').join('')+'</tr></thead><tbody>'+R.map(n=>'<tr><td>'+n+'</td>'+C.slice(1).map(()=>'<td>'+(100+Math.floor(r()*900))+'</td>').join('')+'</tr>').join('')+'</tbody>';
function up(){sc.classList.toggle('sx',sc.scrollLeft>2);sc.classList.toggle('sy',sc.scrollTop>2);document.getElementById('out').textContent=sc.scrollLeft>2||sc.scrollTop>2?'Header row and first column stay pinned while you scroll.':'Scroll the table sideways or down.';}
sc.addEventListener('scroll',up);up();
</script>
</body></html>