Collapsible admin sidebar
An admin sidebar that toggles between full width and an icon rail, keeping labels accessible and remembering nothing server side.
Tailwind CSSNavigationsidebaradmincollapselayout
Live preview
Code
How it works
What it solves
Admin layouts need room for content, so a sidebar that shrinks to icons is a common request. The trick is doing it without duplicating markup or hiding link text from assistive tech when the rail is narrow.
How it works
The aside holds a data-collapsed attribute that Tailwind reads through the data-[collapsed=true]: variant. Width switches between w-60 and w-16 with a transition, and each label span uses group-data-[collapsed=true]:sr-only, so the text stays in the accessibility tree while disappearing visually. Links also carry a title so sighted rail users get a tooltip. The toggle button flips the attribute and updates aria-expanded and aria-label. On small screens the layout is a column, so the sidebar can simply stay full width.
Adapting it
Persist the choice by saving the attribute to localStorage inside a try block and restoring it on load. To overlay on mobile, change the aside to fixed below md and slide it with the drawer pattern. Add a badge by placing a span inside the link with an absolute position.
Pitfalls
- Icon only links without
titleor hidden text are unlabeled. - Animating width reflows the content area; use
will-changesparingly. - Active link styling must not depend on the label being visible.
How to use it
- Wrap your admin page in a
flexcontainer and pastesidebar.htmlas the first child. - Load
sidebar.jswithdefer. - Replace the icon paths and link targets.
- Optionally store the collapsed state in your own preferences store and set the attribute server side.
Licence: DEV3 Snippet Licence. Free to use, change and ship in personal and commercial projects. Attribution is appreciated, not required. Please do not redistribute snippets as a collection.