Segmented Control
A pill of options with a sliding thumb; arrow keys move between options and the content below changes.
AI prompt
Build a four-option segmented control (Day, Week, Month, Year) as a radiogroup. A near-black rounded thumb with white label slides beneath the selected segment using transform translateX with a 220 millisecond ease-out; unselected labels are grey. Arrow Left and Right (and Up and Down) move the selection and focus, Home and End jump to ends, each segment is a role radio with aria-checked and a roving tabindex. Below the control show a short panel whose text changes with the selection, for example "Showing the last 7 days". Light minimal style, 40 pixel tall pill, 1 pixel border, prefers-reduced-motion fallback. Plain HTML, CSS and JavaScript.About this pattern
Definition
A segmented control is a row of mutually exclusive options styled as one pill. A thumb slides to the chosen segment, which makes the single choice obvious.
Purpose & UX principles
- Use for two to five short, related options that change a view (Day, Week, Month) or a setting; not for navigating between pages.
- Implement as a radio group so screen readers announce "1 of 4" and arrow keys move the choice.
- Keep labels short and equal in weight; do not mix icons and text inconsistently.
Interaction states
- Selected, unselected, hover, focus-visible, disabled.
Motion specification
- The thumb slides over 220ms with a gentle ease-out; text colour crossfades 150ms.
- Reduced motion: the thumb jumps with no slide.
Responsive behaviour
- Segments share the width equally; on narrow screens shorten labels or allow horizontal scroll rather than wrapping.
- Minimum segment height 36px for touch.
Code
<div class="bx"><div class="sg" id="sg" role="radiogroup" aria-label="Range"><i class="th" id="th" aria-hidden="true"></i></div><p class="pn" id="pn" aria-live="polite"></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}
.bx{width:min(320px,100%);display:grid;gap:12px}
.sg{position:relative;display:grid;grid-template-columns:repeat(4,1fr);padding:3px;border-radius:99px;background:#e8e8e4;border:1px solid var(--line)}
.th{position:absolute;left:3px;top:3px;bottom:3px;width:calc((100% - 6px) / 4);border-radius:99px;background:var(--text);transform:translateX(calc(var(--i,0) * 100%));transition:transform .22s cubic-bezier(.2,.8,.2,1)}
.sg button{position:relative;z-index:1;height:34px;border:0;background:none;border-radius:99px;font-size:13px;font-weight:600;color:#6a6a66;cursor:pointer;transition:color .15s}
.sg button[aria-checked=true]{color:#fff}
.pn{padding:14px;border-radius:12px;background:var(--surface);border:1px solid var(--line);font-size:13px;color:var(--muted)}.pn b{color:var(--text)}
button:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
@media (prefers-reduced-motion:reduce){.th,.sg button{transition:none}}const O=[['Day','the last 24 hours, hour by hour'],['Week','the last 7 days'],['Month','the last 30 days, grouped by week'],['Year','the last 12 months, grouped by month']],sg=document.getElementById('sg'),pn=document.getElementById('pn'),bs=[];
O.forEach((o,i)=>{const b=document.createElement('button');b.type='button';b.setAttribute('role','radio');b.textContent=o[0];b.addEventListener('click',()=>sel(i));
b.addEventListener('keydown',e=>{const k={ArrowRight:1,ArrowDown:1,ArrowLeft:-1,ArrowUp:-1}[e.key];if(k){e.preventDefault();sel((i+k+O.length)%O.length,1);}else if(e.key==='Home'){e.preventDefault();sel(0,1);}else if(e.key==='End'){e.preventDefault();sel(O.length-1,1);}});sg.append(b);bs.push(b);});
function sel(i,f){sg.style.setProperty('--i',i);bs.forEach((b,k)=>{b.setAttribute('aria-checked',String(k===i));b.tabIndex=k===i?0:-1;});if(f)bs[i].focus();pn.innerHTML='Showing <b>'+O[i][1]+'</b>.';}
sel(1);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>Segmented Control — 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}}
.bx{width:min(320px,100%);display:grid;gap:12px}
.sg{position:relative;display:grid;grid-template-columns:repeat(4,1fr);padding:3px;border-radius:99px;background:#e8e8e4;border:1px solid var(--line)}
.th{position:absolute;left:3px;top:3px;bottom:3px;width:calc((100% - 6px) / 4);border-radius:99px;background:var(--text);transform:translateX(calc(var(--i,0) * 100%));transition:transform .22s cubic-bezier(.2,.8,.2,1)}
.sg button{position:relative;z-index:1;height:34px;border:0;background:none;border-radius:99px;font-size:13px;font-weight:600;color:#6a6a66;cursor:pointer;transition:color .15s}
.sg button[aria-checked=true]{color:#fff}
.pn{padding:14px;border-radius:12px;background:var(--surface);border:1px solid var(--line);font-size:13px;color:var(--muted)}.pn b{color:var(--text)}
button:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
@media (prefers-reduced-motion:reduce){.th,.sg button{transition:none}}
</style></head>
<body><div class="stage">
<div class="bx"><div class="sg" id="sg" role="radiogroup" aria-label="Range"><i class="th" id="th" aria-hidden="true"></i></div><p class="pn" id="pn" aria-live="polite"></p></div>
</div>
<script>
const O=[['Day','the last 24 hours, hour by hour'],['Week','the last 7 days'],['Month','the last 30 days, grouped by week'],['Year','the last 12 months, grouped by month']],sg=document.getElementById('sg'),pn=document.getElementById('pn'),bs=[];
O.forEach((o,i)=>{const b=document.createElement('button');b.type='button';b.setAttribute('role','radio');b.textContent=o[0];b.addEventListener('click',()=>sel(i));
b.addEventListener('keydown',e=>{const k={ArrowRight:1,ArrowDown:1,ArrowLeft:-1,ArrowUp:-1}[e.key];if(k){e.preventDefault();sel((i+k+O.length)%O.length,1);}else if(e.key==='Home'){e.preventDefault();sel(0,1);}else if(e.key==='End'){e.preventDefault();sel(O.length-1,1);}});sg.append(b);bs.push(b);});
function sel(i,f){sg.style.setProperty('--i',i);bs.forEach((b,k)=>{b.setAttribute('aria-checked',String(k===i));b.tabIndex=k===i?0:-1;});if(f)bs[i].focus();pn.innerHTML='Showing <b>'+O[i][1]+'</b>.';}
sel(1);
</script>
</body></html>