Sidebar
A vertical navigation that collapses to an icon rail and expands on demand.
AI prompt
Build a collapsible sidebar navigation with five items (Overview, Projects, Messages, Reports, Settings), each an inline SVG icon plus a label. Expanded width is 220px; collapsed width is 64px showing only icons. A toggle button at the top switches modes using aria-expanded, animating width over 260ms with cubic-bezier(0.4,0,0.2,1); labels fade out in 120ms. The current item has a 3px accent bar on its left edge and aria-current="page". In the collapsed state each item shows a tooltip to its right on hover and keyboard focus, using the label text; set aria-label on each item so the name is available when text is hidden. Hover lightens the item surface. Persist nothing; keep state in JS. Make the layout work in a 520px-wide demo frame. Respect prefers-reduced-motion by removing the transition.About this pattern
Definition
A vertical list of destinations pinned to the left edge. It has two widths: an expanded form with icons and labels and a collapsed rail with icons only, toggled by a button.
Purpose & UX principles
- Suits products with many sections, because a vertical list scales to dozens of items where a top bar cannot.
- The collapsed rail keeps navigation available while giving the workspace more room.
- In the rail, every icon needs an accessible name and a tooltip, since the label is hidden.
Interaction states
- Expanded and collapsed, stored in
aria-expandedon the toggle. - Item hover: surface lightens.
- Current item: accent bar on the left edge, brighter text,
aria-current="page". - Collapsed item hover: a tooltip with the label appears to the right.
Motion specification
- Width: 260ms cubic-bezier(0.4, 0, 0.2, 1).
- Labels: fade out in 120ms before the width finishes shrinking.
- Toggle icon: rotates 180deg in 260ms.
- Reduced motion: width snaps with no animation.
Responsive behaviour
- Desktop: expanded by default at 220px, collapsible to 64px.
- Tablet: start collapsed.
- Phone: replace with a drawer opened from a menu button, or with a bottom navigation.
Code
<div class="shell">
<aside class="side" id="side" aria-label="Sidebar">
<button class="toggle" id="tg" aria-expanded="true" aria-label="Collapse sidebar"><svg viewBox="0 0 24 24"><path d="M15 6l-6 6 6 6"/></svg></button>
<a href="#" class="item" aria-current="page" aria-label="Overview"><svg viewBox="0 0 24 24"><path d="M4 4h7v7H4zm9 0h7v7h-7zM4 13h7v7H4zm9 0h7v7h-7z"/></svg><span>Overview</span></a>
<a href="#" class="item" aria-label="Projects"><svg viewBox="0 0 24 24"><path d="M3 6h7l2 2h9v11H3z"/></svg><span>Projects</span></a>
<a href="#" class="item" aria-label="Messages"><svg viewBox="0 0 24 24"><path d="M3 5h18v12H8l-5 4z"/></svg><span>Messages</span></a>
<a href="#" class="item" aria-label="Reports"><svg viewBox="0 0 24 24"><path d="M5 20V10h3v10zm5 0V4h3v16zm5 0v-7h3v7z"/></svg><span>Reports</span></a>
<a href="#" class="item" aria-label="Settings"><svg viewBox="0 0 24 24"><path d="M12 8a4 4 0 100 8 4 4 0 000-8zm-1-6h2l.5 3 2 1 2.7-1.5 1.4 1.4L18 8.5l1 2 3 .5v2l-3 .5-1 2 1.6 2.6-1.4 1.4L15.5 18l-2 1-.5 3h-2l-.5-3-2-1-2.7 1.6-1.4-1.4L6 15l-1-2-3-.5v-2l3-.5 1-2-1.6-2.6 1.4-1.4L8.5 6l2-1z"/></svg><span>Settings</span></a>
</aside>
<section class="main"><h2>Workspace</h2><p>Collapse the sidebar to get more room.</p></section>
</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}
.shell{display:flex;width:min(560px,100%);height:380px;border:1px solid var(--line);border-radius:18px;overflow:hidden;background:var(--surface)}
.side{width:200px;display:flex;flex-direction:column;gap:4px;padding:10px;background:var(--surface-2);border-right:1px solid var(--line);transition:width .26s cubic-bezier(.4,0,.2,1)}
.side.collapsed{width:64px}
.toggle{align-self:flex-end;width:36px;height:36px;border-radius:10px;border:0;background:none;color:var(--muted);cursor:pointer}
.toggle svg{width:20px;height:20px;fill:none;stroke:currentColor;stroke-width:2;transition:transform .26s}
.side.collapsed .toggle{align-self:center}.side.collapsed .toggle svg{transform:rotate(180deg)}
.item{position:relative;display:flex;align-items:center;gap:12px;height:42px;padding:0 11px;border-radius:10px;color:var(--muted);text-decoration:none;font-size:14px;white-space:nowrap}
.item svg{flex:none;width:20px;height:20px;fill:currentColor}
.item span{transition:opacity .12s}
.side.collapsed .item span{opacity:0;pointer-events:none}
.item:hover{background:rgba(0,0,0,.05);color:var(--text)}
.item[aria-current=page]{color:var(--text);background:rgba(0,0,0,.05)}
.item[aria-current=page]::before{content:"";position:absolute;left:-10px;top:10px;bottom:10px;width:3px;border-radius:3px;background:var(--accent)}
.side.collapsed .item:hover::after,.side.collapsed .item:focus-visible::after{content:attr(aria-label);position:absolute;left:calc(100% + 10px);top:50%;transform:translateY(-50%);background:var(--text);color:var(--bg);padding:4px 9px;border-radius:7px;font-size:12px;z-index:3}
.item:focus-visible,.toggle:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.main{flex:1;padding:24px}.main p{color:var(--muted)}
@media (prefers-reduced-motion:reduce){.side,.toggle svg,.item span{transition:none}}const side=document.getElementById('side'),tg=document.getElementById('tg');
tg.addEventListener('click',()=>{const c=side.classList.toggle('collapsed');tg.setAttribute('aria-expanded',!c);tg.setAttribute('aria-label',c?'Expand sidebar':'Collapse sidebar');});
side.querySelectorAll('.item').forEach(a=>a.addEventListener('click',e=>{e.preventDefault();side.querySelectorAll('.item').forEach(x=>x.removeAttribute('aria-current'));a.setAttribute('aria-current','page');}));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>Sidebar — 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}}
.shell{display:flex;width:min(560px,100%);height:380px;border:1px solid var(--line);border-radius:18px;overflow:hidden;background:var(--surface)}
.side{width:200px;display:flex;flex-direction:column;gap:4px;padding:10px;background:var(--surface-2);border-right:1px solid var(--line);transition:width .26s cubic-bezier(.4,0,.2,1)}
.side.collapsed{width:64px}
.toggle{align-self:flex-end;width:36px;height:36px;border-radius:10px;border:0;background:none;color:var(--muted);cursor:pointer}
.toggle svg{width:20px;height:20px;fill:none;stroke:currentColor;stroke-width:2;transition:transform .26s}
.side.collapsed .toggle{align-self:center}.side.collapsed .toggle svg{transform:rotate(180deg)}
.item{position:relative;display:flex;align-items:center;gap:12px;height:42px;padding:0 11px;border-radius:10px;color:var(--muted);text-decoration:none;font-size:14px;white-space:nowrap}
.item svg{flex:none;width:20px;height:20px;fill:currentColor}
.item span{transition:opacity .12s}
.side.collapsed .item span{opacity:0;pointer-events:none}
.item:hover{background:rgba(0,0,0,.05);color:var(--text)}
.item[aria-current=page]{color:var(--text);background:rgba(0,0,0,.05)}
.item[aria-current=page]::before{content:"";position:absolute;left:-10px;top:10px;bottom:10px;width:3px;border-radius:3px;background:var(--accent)}
.side.collapsed .item:hover::after,.side.collapsed .item:focus-visible::after{content:attr(aria-label);position:absolute;left:calc(100% + 10px);top:50%;transform:translateY(-50%);background:var(--text);color:var(--bg);padding:4px 9px;border-radius:7px;font-size:12px;z-index:3}
.item:focus-visible,.toggle:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.main{flex:1;padding:24px}.main p{color:var(--muted)}
@media (prefers-reduced-motion:reduce){.side,.toggle svg,.item span{transition:none}}
</style></head>
<body><div class="stage">
<div class="shell">
<aside class="side" id="side" aria-label="Sidebar">
<button class="toggle" id="tg" aria-expanded="true" aria-label="Collapse sidebar"><svg viewBox="0 0 24 24"><path d="M15 6l-6 6 6 6"/></svg></button>
<a href="#" class="item" aria-current="page" aria-label="Overview"><svg viewBox="0 0 24 24"><path d="M4 4h7v7H4zm9 0h7v7h-7zM4 13h7v7H4zm9 0h7v7h-7z"/></svg><span>Overview</span></a>
<a href="#" class="item" aria-label="Projects"><svg viewBox="0 0 24 24"><path d="M3 6h7l2 2h9v11H3z"/></svg><span>Projects</span></a>
<a href="#" class="item" aria-label="Messages"><svg viewBox="0 0 24 24"><path d="M3 5h18v12H8l-5 4z"/></svg><span>Messages</span></a>
<a href="#" class="item" aria-label="Reports"><svg viewBox="0 0 24 24"><path d="M5 20V10h3v10zm5 0V4h3v16zm5 0v-7h3v7z"/></svg><span>Reports</span></a>
<a href="#" class="item" aria-label="Settings"><svg viewBox="0 0 24 24"><path d="M12 8a4 4 0 100 8 4 4 0 000-8zm-1-6h2l.5 3 2 1 2.7-1.5 1.4 1.4L18 8.5l1 2 3 .5v2l-3 .5-1 2 1.6 2.6-1.4 1.4L15.5 18l-2 1-.5 3h-2l-.5-3-2-1-2.7 1.6-1.4-1.4L6 15l-1-2-3-.5v-2l3-.5 1-2-1.6-2.6 1.4-1.4L8.5 6l2-1z"/></svg><span>Settings</span></a>
</aside>
<section class="main"><h2>Workspace</h2><p>Collapse the sidebar to get more room.</p></section>
</div>
</div>
<script>
const side=document.getElementById('side'),tg=document.getElementById('tg');
tg.addEventListener('click',()=>{const c=side.classList.toggle('collapsed');tg.setAttribute('aria-expanded',!c);tg.setAttribute('aria-label',c?'Expand sidebar':'Collapse sidebar');});
side.querySelectorAll('.item').forEach(a=>a.addEventListener('click',e=>{e.preventDefault();side.querySelectorAll('.item').forEach(x=>x.removeAttribute('aria-current'));a.setAttribute('aria-current','page');}));
</script>
</body></html>