Toggle Button Toolbar
A formatting toolbar with Bold, Italic and Underline toggles and an alignment group, navigated with arrow keys.
AI prompt
Clean UI design of a text formatting toolbar: a rounded white bar with three toggle buttons B I U where Bold is pressed with a dark filled background, a thin divider, then three alignment buttons with the middle one selected, below the bar a sample paragraph shown bold and centred, thin border, light theme, flat modern style. Variants: all toggles off, and keyboard focus ring on the Italic button. Negative: heavy shadows, gradients, text errors, clutter. Reduced motion: static design.About this pattern
Definition
A toolbar groups related controls into one tab stop. Toggle buttons (Bold, Italic, Underline) keep their own on or off state with aria-pressed; the alignment buttons are a radio-like group where exactly one is selected. Arrow keys move between buttons.
Purpose & UX principles
- Use the toolbar role with a label, and a roving tabindex so Tab enters once and leaves once.
- Toggles show a clear pressed style that does not rely on colour alone, such as a filled background.
- Apply the change to the selection immediately and show it in the sample.
Interaction states
- Three toggles, three alignment buttons, Left and Right or Home and End on the keyboard.
Motion specification
- Changes apply instantly; no animation.
- Reduced motion: nothing to change.
Responsive behaviour
- Keep buttons at least 32px wide and tall; 44px on touch.
- Provide visible tooltips or labels for icon-only buttons.
Code
<div class="bx"><div class="tl" id="tl" role="toolbar" aria-label="Text formatting"><button type="button" class="t" data-f="b" aria-pressed="false" aria-label="Bold"><b>B</b></button><button type="button" class="t" data-f="i" aria-pressed="false" aria-label="Italic"><i>I</i></button><button type="button" class="t" data-f="u" aria-pressed="false" aria-label="Underline"><u>U</u></button><span class="dv"></span><button type="button" class="t" data-a="left" aria-pressed="true" aria-label="Align left">⇤</button><button type="button" class="t" data-a="center" aria-pressed="false" aria-label="Align centre">≡</button><button type="button" class="t" data-a="right" aria-pressed="false" aria-label="Align right">⇥</button></div><p class="sm" id="sm">Formatting is applied to this sample text.</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}
.tl{display:inline-flex;gap:3px;padding:4px;border:1px solid var(--line);border-radius:10px;background:var(--surface);align-items:center}.t{width:34px;height:32px;border-radius:7px;font-size:14px;display:grid;place-items:center}.t:hover{background:var(--pv)}.t[aria-pressed=true]{background:#171717;color:#fff}.dv{width:1px;height:18px;background:var(--line);margin:0 4px}
.sm{margin-top:10px;padding:12px;border:1px solid var(--line);border-radius:10px;background:var(--surface);font-size:14px;line-height:1.5;min-height:70px}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}const tl=document.getElementById('tl'),sm=document.getElementById('sm'),bt=[...tl.querySelectorAll('.t')];let cur=0;
function ap(){const on=f=>tl.querySelector('[data-f='+f+']').getAttribute('aria-pressed')==='true',al=tl.querySelector('[data-a][aria-pressed=true]').dataset.a;sm.style.fontWeight=on('b')?'700':'400';sm.style.fontStyle=on('i')?'italic':'normal';sm.style.textDecoration=on('u')?'underline':'none';sm.style.textAlign=al;}
function rv(i){cur=i;bt.forEach((b,k)=>b.tabIndex=k===i?0:-1);}
bt.forEach((b,i)=>{b.addEventListener('click',()=>{rv(i);if(b.dataset.f)b.setAttribute('aria-pressed',String(b.getAttribute('aria-pressed')!=='true'));else{tl.querySelectorAll('[data-a]').forEach(x=>x.setAttribute('aria-pressed',String(x===b)));}ap();});});
tl.addEventListener('keydown',e=>{const k={ArrowRight:1,ArrowLeft:-1}[e.key];if(k){e.preventDefault();rv((cur+k+bt.length)%bt.length);bt[cur].focus();}else if(e.key==='Home'){e.preventDefault();rv(0);bt[0].focus();}else if(e.key==='End'){e.preventDefault();rv(bt.length-1);bt[cur].focus();}});tl.addEventListener('focusin',e=>{const i=bt.indexOf(e.target);if(i>=0)rv(i);});rv(0);ap();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>Toggle Button Toolbar — 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{display:inline-flex;gap:3px;padding:4px;border:1px solid var(--line);border-radius:10px;background:var(--surface);align-items:center}.t{width:34px;height:32px;border-radius:7px;font-size:14px;display:grid;place-items:center}.t:hover{background:var(--pv)}.t[aria-pressed=true]{background:#171717;color:#fff}.dv{width:1px;height:18px;background:var(--line);margin:0 4px}
.sm{margin-top:10px;padding:12px;border:1px solid var(--line);border-radius:10px;background:var(--surface);font-size:14px;line-height:1.5;min-height:70px}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}
</style></head>
<body><div class="stage">
<div class="bx"><div class="tl" id="tl" role="toolbar" aria-label="Text formatting"><button type="button" class="t" data-f="b" aria-pressed="false" aria-label="Bold"><b>B</b></button><button type="button" class="t" data-f="i" aria-pressed="false" aria-label="Italic"><i>I</i></button><button type="button" class="t" data-f="u" aria-pressed="false" aria-label="Underline"><u>U</u></button><span class="dv"></span><button type="button" class="t" data-a="left" aria-pressed="true" aria-label="Align left">⇤</button><button type="button" class="t" data-a="center" aria-pressed="false" aria-label="Align centre">≡</button><button type="button" class="t" data-a="right" aria-pressed="false" aria-label="Align right">⇥</button></div><p class="sm" id="sm">Formatting is applied to this sample text.</p></div>
</div>
<script>
const tl=document.getElementById('tl'),sm=document.getElementById('sm'),bt=[...tl.querySelectorAll('.t')];let cur=0;
function ap(){const on=f=>tl.querySelector('[data-f='+f+']').getAttribute('aria-pressed')==='true',al=tl.querySelector('[data-a][aria-pressed=true]').dataset.a;sm.style.fontWeight=on('b')?'700':'400';sm.style.fontStyle=on('i')?'italic':'normal';sm.style.textDecoration=on('u')?'underline':'none';sm.style.textAlign=al;}
function rv(i){cur=i;bt.forEach((b,k)=>b.tabIndex=k===i?0:-1);}
bt.forEach((b,i)=>{b.addEventListener('click',()=>{rv(i);if(b.dataset.f)b.setAttribute('aria-pressed',String(b.getAttribute('aria-pressed')!=='true'));else{tl.querySelectorAll('[data-a]').forEach(x=>x.setAttribute('aria-pressed',String(x===b)));}ap();});});
tl.addEventListener('keydown',e=>{const k={ArrowRight:1,ArrowLeft:-1}[e.key];if(k){e.preventDefault();rv((cur+k+bt.length)%bt.length);bt[cur].focus();}else if(e.key==='Home'){e.preventDefault();rv(0);bt[0].focus();}else if(e.key==='End'){e.preventDefault();rv(bt.length-1);bt[cur].focus();}});tl.addEventListener('focusin',e=>{const i=bt.indexOf(e.target);if(i>=0)rv(i);});rv(0);ap();
</script>
</body></html>