CSS Expanding Icon Sidebar

A slim icon-only sidebar rail that widens to show labels on hover or keyboard focus.

  • sidebar
  • icon rail
  • collapsible
  • hover
  • focus-within
  • dashboard
HTML
<nav class="rail" aria-label="Main">
  <a href="#" aria-current="page">
    <svg viewBox="0 0 24 24" aria-hidden="true"><path d="M3 11l9-8 9 8"/><path d="M5 10v10h14V10"/></svg>
    <span>Home</span>
  </a>
  <a href="#">
    <svg viewBox="0 0 24 24" aria-hidden="true"><circle cx="11" cy="11" r="7"/><path d="m20 20-3.5-3.5"/></svg>
    <span>Search</span>
  </a>
  <a href="#">
    <svg viewBox="0 0 24 24" aria-hidden="true"><path d="M3 20h18M7 16V9M12 16V5M17 16v-4"/></svg>
    <span>Reports</span>
  </a>
  <a href="#">
    <svg viewBox="0 0 24 24" aria-hidden="true"><circle cx="12" cy="8" r="4"/><path d="M4 21a8 8 0 0 1 16 0"/></svg>
    <span>Profile</span>
  </a>
</nav>
CSS
.rail {
  display: flex;
  flex-direction: column;
  gap: 2px;
  box-sizing: border-box;
  width: 60px;
  padding: 8px;
  overflow: hidden;
  border-radius: 16px;
  background: #0f172a;
  transition: width 0.25s ease;
}

.rail:hover,
.rail:focus-within {
  width: 180px;
}

.rail a {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 9px 11px;
  border-radius: 10px;
  color: #94a3b8;
  font: 500 14px system-ui, sans-serif;
  text-decoration: none;
  white-space: nowrap;
}

.rail svg {
  flex: none;
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* Labels stay in the accessibility tree, only faded out */
.rail span {
  opacity: 0;
  transition: opacity 0.2s ease;
}

.rail:hover span,
.rail:focus-within span {
  opacity: 1;
}

.rail a:hover,
.rail a[aria-current="page"] {
  background: #1e293b;
  color: #f8fafc;
}

.rail a:focus-visible {
  outline: 2px solid #38bdf8;
  outline-offset: -2px;
}

@media (prefers-reduced-motion: reduce) {
  .rail,
  .rail span { transition: none; }
}

About this CSS Expanding Icon Sidebar

This CSS Expanding Icon Sidebar is 67 lines of pure CSS (1.1 KB) with no JavaScript and no dependencies. It uses transitions, Flexbox, keyboard focus styles and reduced-motion support. It works in current versions of Chrome, Edge, Safari and Firefox. It works in any stack: plain HTML, React, Vue, Svelte, Astro, WordPress or Webflow.

CSS features used

Customize it

The colors in this snippet. Search and replace them to match your brand:

  • #0f172a
  • #94a3b8
  • #1e293b
  • #f8fafc
  • #38bdf8

How to use this expanding icon sidebar

  1. Copy the HTML and paste it where you want the element to appear.
  2. Copy the CSS into your stylesheet, or inside a <style> tag.
  3. Keep the .rail class name, or rename it in both places. Then tweak colors, sizes and timing to match your design.
  4. Using React or Next.js? Change class to className in the HTML. The CSS works as is, including in Tailwind projects (put it in your global stylesheet).

Building with an AI tool?

“Copy for AI” copies a short prompt for Cursor, Claude Code, Lovable, Bolt or v0. Your AI tool opens the Markdown version of this snippet and reads the HTML, CSS and setup notes there. If your tool can't open links, with the code instead. More ways to use copy CSS with AI.

Feedback