CSS Nested Sidebar Menu
A docs-style sidebar with collapsible groups built on details and summary, plus a guide line for children.
<nav class="tree-nav" aria-label="Docs">
<a href="#">Introduction</a>
<details open>
<summary>Components</summary>
<a href="#">Buttons</a>
<a href="#" aria-current="page">Cards</a>
<a href="#">Forms</a>
</details>
<details>
<summary>Guides</summary>
<a href="#">Theming</a>
<a href="#">Dark mode</a>
</details>
</nav>
.tree-nav {
display: grid;
gap: 2px;
width: 200px;
color: #334155;
font: 500 14px/1.4 system-ui, sans-serif;
}
.tree-nav a,
.tree-nav summary {
display: block;
padding: 7px 10px;
border-radius: 8px;
color: inherit;
text-decoration: none;
cursor: pointer;
}
.tree-nav summary {
position: relative;
color: #0f172a;
font-weight: 600;
list-style: none;
}
.tree-nav summary::-webkit-details-marker {
display: none;
}
/* Chevron */
.tree-nav summary::after {
content: "";
position: absolute;
top: 50%;
right: 12px;
width: 6px;
height: 6px;
margin-top: -5px;
border: solid #94a3b8;
border-width: 0 2px 2px 0;
transform: rotate(45deg);
transition: transform 0.2s ease;
}
.tree-nav details[open] > summary::after {
margin-top: -2px;
transform: rotate(-135deg);
}
.tree-nav details > a {
margin-left: 14px;
padding-left: 14px;
border-left: 1px solid #e2e8f0;
border-radius: 0 8px 8px 0;
color: #64748b;
}
.tree-nav a:hover,
.tree-nav summary:hover {
background: #f1f5f9;
}
.tree-nav a[aria-current="page"] {
border-left-color: #2563eb;
background: #eff6ff;
color: #1d4ed8;
}
.tree-nav a:focus-visible,
.tree-nav summary:focus-visible {
outline: 2px solid #2563eb;
outline-offset: -2px;
}
About this CSS Nested Sidebar Menu
This CSS Nested Sidebar Menu is 73 lines of pure CSS (1.2 KB) with no JavaScript and no dependencies. It uses transitions, transforms, CSS Grid and keyboard focus styles. 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:
#334155#0f172a#94a3b8#e2e8f0#64748b#f1f5f9#2563eb#eff6ff#1d4ed8
How to use this nested sidebar menu
- Copy the HTML and paste it where you want the element to appear.
- Copy the CSS into your stylesheet, or inside a
<style>tag. - Keep the
.tree-navclass name, or rename it in both places. Then tweak colors, sizes and timing to match your design. - Using React or Next.js? Change
classtoclassNamein 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.