← Back toCSS Layout

Nested dropdowns

Written byPhuoc Nguyen
Category
Navigation
Created
13 Jan, 2020

HTML

index.html
<ul class="nested-dropdowns">
<li>A</li>
<li>
<div class="nested-dropdowns__item">
B
<div class="nested-dropdowns__arrow">...</div>
</div>
<ul>
<li>B.1</li>
<li>B.2</li>
<li>
<div class="nested-dropdowns__item">
B.3
<div class="nested-dropdowns__arrow">...</div>
</div>
<ul>
<li>B.3.1</li>
<li>B.3.2</li>
<li>B.3.3</li>
<li>B.3.4</li>
</ul>
</li>
<li>B.4</li>
<li>B.5</li>
</ul>
</li>
<li>C</li>
<li>D</li>
</ul>

CSS

styles.css
.nested-dropdowns {
/* Border */
border: 1px solid #d1d5db;
display: flex;

/* Reset list styles */
list-style-type: none;
margin: 0;
padding: 0;
}

.nested-dropdowns li {
cursor: pointer;

/* Spacing */
padding: 0.25rem;

/* Used to position the sub nested-dropdowns */
position: relative;
}

/* The sub nested-dropdowns */
.nested-dropdowns ul {
/* Border */
border: 1px solid #d1d5db;

/* Hidden by default */
display: none;

/* Absolute position */
left: 0;
position: absolute;
top: 100%;

/* Reset styles */
list-style-type: none;
margin: 0;
padding: 0;
}

/* The second level sub nested-dropdowns */
.nested-dropdowns ul ul {
left: 100%;
position: absolute;
top: 0;
}

/* Change background color of list item when being hovered */
.nested-dropdowns li:hover {
background-color: rgba(0, 0, 0, 0.1);
}

/* Show the direct sub nested-dropdowns when hovering the list item */
.nested-dropdowns li:hover > ul {
display: block;
}

Questions? 🙋

Do you have any questions? Not just about this specific post, but about any topic in front-end development that you'd like to learn more about? If so, feel free to send me a message on Twitter or send me an email. You can find them at the bottom of this page.
I have a long list of upcoming posts, but your questions or ideas for the next one will be my top priority. Let's learn together! Sharing knowledge is the best way to grow 🥷.

Recent posts ⚡

Newsletter 🔔

If you're into front-end technologies and you want to see more of the content I'm creating, then you might want to consider subscribing to my newsletter.
By subscribing, you'll be the first to know about new articles, products, and exclusive promotions.
Don't worry, I won't spam you. And if you ever change your mind, you can unsubscribe at any time.
Phước Nguyễn