Nested dropdowns
Written byPhuoc Nguyen
Created
13 Jan, 2020
Category
Navigation
#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 about front-end development? If so, feel free to create a new issue on GitHub using the button below. I'm happy to help with any topic you'd like to learn more about, even beyond what's covered in this post.
While I have a long list of upcoming topics, I'm always eager to prioritize your questions and ideas for future content. Let's learn and grow together! Sharing knowledge is the best way to elevate ourselves 🥷.
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