Tabs with Sliding Indicator
Tabs with an indicator that slides and resizes to the active tab, in underline or pill style, with arrow-key navigation.
AI prompt
Clean UI mockup of a tab bar with four tabs Overview, Activity, Billing and Team, the active tab Activity with dark text and a thin dark underline indicator, a faint trail showing the indicator sliding from Overview, below it a content panel with a heading and two lines of text, a second variant with a dark rounded pill behind the active tab with white text, light theme, flat modern style. Negative: heavy shadows, clutter, text errors. Reduced motion: static design.About this pattern
Definition
A moving indicator connects the old and new tab, which helps people see what changed. It is a single element positioned with transform and width taken from the active tab's measurements. The tablist must stay accessible: roving tabindex, arrow keys, Home and End, and panels tied to tabs with aria-controls.
Purpose & UX principles
- Measure the active tab with offsetLeft and offsetWidth, then set the indicator's translateX and width.
- Use role tablist, tab and tabpanel with aria-selected and aria-controls.
- Left and Right arrows move between tabs; automatic activation is fine for light panels.
- Update the indicator again on resize.
Interaction states
- Click or use the arrow keys; choose the underline or pill style.
Motion specification
- The indicator slides in 250ms.
- Reduced motion: it jumps.
Responsive behaviour
- Do not hide inactive panels from the document with only opacity; use hidden.
- Keep the animation under 250 ms.
Code
<div class="bx"><div class="pre" id="pre" role="group" aria-label="Style"><button data-p="0" aria-pressed="true">Underline</button><button data-p="1" aria-pressed="false">Pill</button></div><div class="tl" id="tl" role="tablist" aria-label="Account"><i class="ind" id="ind"></i><button role="tab" id="t0" aria-selected="true" aria-controls="p0" tabindex="0">Overview</button><button role="tab" id="t1" aria-selected="false" aria-controls="p1" tabindex="-1">Activity</button><button role="tab" id="t2" aria-selected="false" aria-controls="p2" tabindex="-1">Billing</button><button role="tab" id="t3" aria-selected="false" aria-controls="p3" tabindex="-1">Team</button></div><div class="pn" id="pn"><section role="tabpanel" id="p0" aria-labelledby="t0"><b>Overview</b><p>A summary of your account.</p></section><section role="tabpanel" id="p1" aria-labelledby="t1" hidden><b>Activity</b><p>Recent sign-ins and changes.</p></section><section role="tabpanel" id="p2" aria-labelledby="t2" hidden><b>Billing</b><p>Invoices and payment methods.</p></section><section role="tabpanel" id="p3" aria-labelledby="t3" hidden><b>Team</b><p>Members and their roles.</p></section></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}
.tl{position:relative;display:flex;gap:2px;border-bottom:1px solid var(--line);padding:0 2px;overflow:hidden}.tl button{position:relative;z-index:1;padding:10px 12px;font-size:13px;font-weight:600;color:#6b6b6b;transition:color .2s}.tl button[aria-selected=true]{color:#171717}.ind{position:absolute;left:0;bottom:-1px;height:2px;background:#171717;width:0;transition:transform .25s,width .25s,height .25s,bottom .25s,border-radius .25s,background .25s}.tl.pill{border-bottom:0;background:#ececea;border-radius:12px;padding:3px}.tl.pill .ind{bottom:3px;height:calc(100% - 6px);border-radius:9px;background:#171717}.tl.pill button[aria-selected=true]{color:#fff}.pn{padding:12px 4px;font-size:12.5px}.pn p{margin:4px 0 0;color:var(--muted)}@media (prefers-reduced-motion:reduce){.ind,.tl button{transition:none}}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}const tl=document.getElementById('tl'),ind=document.getElementById('ind'),tabs=[...tl.querySelectorAll('[role=tab]')],bs=[...document.querySelectorAll('#pre button')];let cur=0,p=0;
function pos(){const t=tabs[cur];ind.style.width=t.offsetWidth+'px';ind.style.transform='translateX('+t.offsetLeft+'px)';}
function sel(i,f){cur=i;tabs.forEach((t,k)=>{t.setAttribute('aria-selected',String(k===i));t.tabIndex=k===i?0:-1;document.getElementById('p'+k).hidden=k!==i;});if(f)tabs[i].focus();pos();}
tabs.forEach((t,i)=>t.addEventListener('click',()=>sel(i,false)));tl.addEventListener('keydown',e=>{let n=null;if(e.key==='ArrowRight')n=(cur+1)%tabs.length;else if(e.key==='ArrowLeft')n=(cur-1+tabs.length)%tabs.length;else if(e.key==='Home')n=0;else if(e.key==='End')n=tabs.length-1;if(n!==null){e.preventDefault();sel(n,true);}});
function style(){tl.classList.toggle('pill',p===1);bs.forEach(b=>b.setAttribute('aria-pressed',String(+b.dataset.p===p)));pos();}
bs.forEach(b=>b.addEventListener('click',()=>{p=+b.dataset.p;style();}));addEventListener('resize',pos);sel(0,false);style();setTimeout(pos,80);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>Tabs with Sliding Indicator — 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}
.tl{position:relative;display:flex;gap:2px;border-bottom:1px solid var(--line);padding:0 2px;overflow:hidden}.tl button{position:relative;z-index:1;padding:10px 12px;font-size:13px;font-weight:600;color:#6b6b6b;transition:color .2s}.tl button[aria-selected=true]{color:#171717}.ind{position:absolute;left:0;bottom:-1px;height:2px;background:#171717;width:0;transition:transform .25s,width .25s,height .25s,bottom .25s,border-radius .25s,background .25s}.tl.pill{border-bottom:0;background:#ececea;border-radius:12px;padding:3px}.tl.pill .ind{bottom:3px;height:calc(100% - 6px);border-radius:9px;background:#171717}.tl.pill button[aria-selected=true]{color:#fff}.pn{padding:12px 4px;font-size:12.5px}.pn p{margin:4px 0 0;color:var(--muted)}@media (prefers-reduced-motion:reduce){.ind,.tl button{transition:none}}
@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="Style"><button data-p="0" aria-pressed="true">Underline</button><button data-p="1" aria-pressed="false">Pill</button></div><div class="tl" id="tl" role="tablist" aria-label="Account"><i class="ind" id="ind"></i><button role="tab" id="t0" aria-selected="true" aria-controls="p0" tabindex="0">Overview</button><button role="tab" id="t1" aria-selected="false" aria-controls="p1" tabindex="-1">Activity</button><button role="tab" id="t2" aria-selected="false" aria-controls="p2" tabindex="-1">Billing</button><button role="tab" id="t3" aria-selected="false" aria-controls="p3" tabindex="-1">Team</button></div><div class="pn" id="pn"><section role="tabpanel" id="p0" aria-labelledby="t0"><b>Overview</b><p>A summary of your account.</p></section><section role="tabpanel" id="p1" aria-labelledby="t1" hidden><b>Activity</b><p>Recent sign-ins and changes.</p></section><section role="tabpanel" id="p2" aria-labelledby="t2" hidden><b>Billing</b><p>Invoices and payment methods.</p></section><section role="tabpanel" id="p3" aria-labelledby="t3" hidden><b>Team</b><p>Members and their roles.</p></section></div></div>
</div>
<script>
const tl=document.getElementById('tl'),ind=document.getElementById('ind'),tabs=[...tl.querySelectorAll('[role=tab]')],bs=[...document.querySelectorAll('#pre button')];let cur=0,p=0;
function pos(){const t=tabs[cur];ind.style.width=t.offsetWidth+'px';ind.style.transform='translateX('+t.offsetLeft+'px)';}
function sel(i,f){cur=i;tabs.forEach((t,k)=>{t.setAttribute('aria-selected',String(k===i));t.tabIndex=k===i?0:-1;document.getElementById('p'+k).hidden=k!==i;});if(f)tabs[i].focus();pos();}
tabs.forEach((t,i)=>t.addEventListener('click',()=>sel(i,false)));tl.addEventListener('keydown',e=>{let n=null;if(e.key==='ArrowRight')n=(cur+1)%tabs.length;else if(e.key==='ArrowLeft')n=(cur-1+tabs.length)%tabs.length;else if(e.key==='Home')n=0;else if(e.key==='End')n=tabs.length-1;if(n!==null){e.preventDefault();sel(n,true);}});
function style(){tl.classList.toggle('pill',p===1);bs.forEach(b=>b.setAttribute('aria-pressed',String(+b.dataset.p===p)));pos();}
bs.forEach(b=>b.addEventListener('click',()=>{p=+b.dataset.p;style();}));addEventListener('resize',pos);sel(0,false);style();setTimeout(pos,80);
</script>
</body></html>