Tab Navigation
Accessible tabs with a sliding underline that switch between related panels.
AI prompt
Build an accessible tab component with three tabs (Overview, Specs, Reviews) following the WAI-ARIA tabs pattern: a container with role="tablist", buttons with role="tab", aria-selected, aria-controls and a roving tabindex (selected tab tabindex 0, others -1), and panels with role="tabpanel", aria-labelledby and hidden when inactive. Left and Right arrows move focus and selection, Home and End jump to the ends. An absolutely positioned underline (2px, accent colour) slides to the selected tab by animating transform and width over 280ms cubic-bezier(0.4,0,0.2,1); recompute its position on resize. Panels fade in with a 6px upward slide in 160ms. Make the tab list horizontally scrollable if it overflows and scroll the selected tab into view. Disable animations for prefers-reduced-motion.About this pattern
Definition
A row of labels where exactly one is selected and shows its panel below. An underline slides from tab to tab to show the change. It implements the WAI-ARIA tabs pattern.
Purpose & UX principles
- Lets people switch between peer views of the same context without leaving the page.
- Do not use tabs for steps in a process or for navigation between unrelated pages.
- Keep labels short, one or two words, and avoid more than six tabs.
Interaction states
- Default, hover and selected, with
aria-selectedreflecting the active tab. - Only the selected tab is in the tab order; arrows move between tabs (roving tabindex).
- Panels not shown have the
hiddenattribute.
Motion specification
- Underline: width and translateX, 280ms cubic-bezier(0.4, 0, 0.2, 1).
- Panel: 160ms fade and 6px upward slide.
- Reduced motion: underline jumps, panel appears without animation.
Responsive behaviour
- If tabs overflow, make the list horizontally scrollable with the active tab scrolled into view.
- Never wrap the labels onto two lines.
Code
<div class="tabs">
<div class="list" role="tablist" aria-label="Product" id="list">
<button role="tab" id="t1" aria-controls="p1" aria-selected="true">Overview</button>
<button role="tab" id="t2" aria-controls="p2" aria-selected="false" tabindex="-1">Specs</button>
<button role="tab" id="t3" aria-controls="p3" aria-selected="false" tabindex="-1">Reviews</button>
<span class="bar" id="bar" aria-hidden="true"></span>
</div>
<div role="tabpanel" id="p1" aria-labelledby="t1" class="panel"><h3>Overview</h3><p>A short description of what the product does and who it is for.</p></div>
<div role="tabpanel" id="p2" aria-labelledby="t2" class="panel" hidden><h3>Specs</h3><p>Weight 240 g · Battery 36 h · Water resistance IP67.</p></div>
<div role="tabpanel" id="p3" aria-labelledby="t3" class="panel" hidden><h3>Reviews</h3><p>4.7 out of 5 from 1,284 reviews.</p></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}
.tabs{width:min(460px,100%);border:1px solid var(--line);border-radius:18px;background:var(--surface);padding:6px 20px 24px}
.list{position:relative;display:flex;gap:6px;border-bottom:1px solid var(--line);overflow-x:auto;scrollbar-width:none}
[role=tab]{flex:none;background:none;border:0;color:var(--muted);font:inherit;font-size:14px;font-weight:600;padding:14px 14px;cursor:pointer;white-space:nowrap}
[role=tab]:hover{color:var(--text)}
[role=tab][aria-selected=true]{color:var(--text)}
[role=tab]:focus-visible{outline:2px solid var(--accent);outline-offset:-3px;border-radius:8px}
.bar{position:absolute;left:0;bottom:-1px;height:2px;width:10px;background:var(--accent);border-radius:2px;transition:transform .28s cubic-bezier(.4,0,.2,1),width .28s cubic-bezier(.4,0,.2,1)}
.panel{padding-top:20px;animation:in .16s ease-out}
.panel p{color:var(--muted)}
@keyframes in{from{opacity:0;transform:translateY(6px)}}
@media (prefers-reduced-motion:reduce){.bar{transition:none}.panel{animation:none}}const list=document.getElementById('list'),tabs=[...list.querySelectorAll('[role=tab]')],bar=document.getElementById('bar');
function place(t){bar.style.width=t.offsetWidth+'px';bar.style.transform='translateX('+t.offsetLeft+'px)';}
function select(t,focus){
tabs.forEach(x=>{const on=x===t;x.setAttribute('aria-selected',on);x.tabIndex=on?0:-1;document.getElementById(x.getAttribute('aria-controls')).hidden=!on;});
place(t);t.scrollIntoView({block:'nearest',inline:'nearest'});if(focus)t.focus();
}
tabs.forEach(t=>t.addEventListener('click',()=>select(t,false)));
list.addEventListener('keydown',e=>{const i=tabs.indexOf(document.activeElement);if(i<0)return;
const map={ArrowRight:(i+1)%tabs.length,ArrowLeft:(i-1+tabs.length)%tabs.length,Home:0,End:tabs.length-1};
if(e.key in map){e.preventDefault();select(tabs[map[e.key]],true);}});
addEventListener('resize',()=>place(tabs.find(t=>t.getAttribute('aria-selected')==='true')));
place(tabs[0]);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>Tab Navigation — 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}}
.tabs{width:min(460px,100%);border:1px solid var(--line);border-radius:18px;background:var(--surface);padding:6px 20px 24px}
.list{position:relative;display:flex;gap:6px;border-bottom:1px solid var(--line);overflow-x:auto;scrollbar-width:none}
[role=tab]{flex:none;background:none;border:0;color:var(--muted);font:inherit;font-size:14px;font-weight:600;padding:14px 14px;cursor:pointer;white-space:nowrap}
[role=tab]:hover{color:var(--text)}
[role=tab][aria-selected=true]{color:var(--text)}
[role=tab]:focus-visible{outline:2px solid var(--accent);outline-offset:-3px;border-radius:8px}
.bar{position:absolute;left:0;bottom:-1px;height:2px;width:10px;background:var(--accent);border-radius:2px;transition:transform .28s cubic-bezier(.4,0,.2,1),width .28s cubic-bezier(.4,0,.2,1)}
.panel{padding-top:20px;animation:in .16s ease-out}
.panel p{color:var(--muted)}
@keyframes in{from{opacity:0;transform:translateY(6px)}}
@media (prefers-reduced-motion:reduce){.bar{transition:none}.panel{animation:none}}
</style></head>
<body><div class="stage">
<div class="tabs">
<div class="list" role="tablist" aria-label="Product" id="list">
<button role="tab" id="t1" aria-controls="p1" aria-selected="true">Overview</button>
<button role="tab" id="t2" aria-controls="p2" aria-selected="false" tabindex="-1">Specs</button>
<button role="tab" id="t3" aria-controls="p3" aria-selected="false" tabindex="-1">Reviews</button>
<span class="bar" id="bar" aria-hidden="true"></span>
</div>
<div role="tabpanel" id="p1" aria-labelledby="t1" class="panel"><h3>Overview</h3><p>A short description of what the product does and who it is for.</p></div>
<div role="tabpanel" id="p2" aria-labelledby="t2" class="panel" hidden><h3>Specs</h3><p>Weight 240 g · Battery 36 h · Water resistance IP67.</p></div>
<div role="tabpanel" id="p3" aria-labelledby="t3" class="panel" hidden><h3>Reviews</h3><p>4.7 out of 5 from 1,284 reviews.</p></div>
</div>
</div>
<script>
const list=document.getElementById('list'),tabs=[...list.querySelectorAll('[role=tab]')],bar=document.getElementById('bar');
function place(t){bar.style.width=t.offsetWidth+'px';bar.style.transform='translateX('+t.offsetLeft+'px)';}
function select(t,focus){
tabs.forEach(x=>{const on=x===t;x.setAttribute('aria-selected',on);x.tabIndex=on?0:-1;document.getElementById(x.getAttribute('aria-controls')).hidden=!on;});
place(t);t.scrollIntoView({block:'nearest',inline:'nearest'});if(focus)t.focus();
}
tabs.forEach(t=>t.addEventListener('click',()=>select(t,false)));
list.addEventListener('keydown',e=>{const i=tabs.indexOf(document.activeElement);if(i<0)return;
const map={ArrowRight:(i+1)%tabs.length,ArrowLeft:(i-1+tabs.length)%tabs.length,Home:0,End:tabs.length-1};
if(e.key in map){e.preventDefault();select(tabs[map[e.key]],true);}});
addEventListener('resize',()=>place(tabs.find(t=>t.getAttribute('aria-selected')==='true')));
place(tabs[0]);
</script>
</body></html>