Sub topic 10
Add a dropdown submenu
This chapter adds a dropdown submenu to the navigation bar you built in the earlier chapters. It assumes you have the horizontal Flexbox bar with reset list styles. You should also know how position: relative and position: absolute work together, and how the :hover and :focus-within pseudo-classes behave.
Nest a list inside the parent item
A submenu is a list inside the list item of the link that opens it. The nesting shows that the child links belong to "Products". Screen readers announce it as a list within a list.
<ul class="nav-list">
<li><a href="/">Home</a></li>
<li class="has-submenu">
<a href="/products">Products</a>
<ul class="submenu">
<li><a href="/products/software">Software</a></li>
<li><a href="/products/hardware">Hardware</a></li>
</ul>
</li>
<li><a href="/contact">Contact</a></li>
</ul>The parent link still points to a real page. Without JavaScript, a visitor who cannot open the menu can still click "Products" and find the child links there.
Position the submenu with CSS
The submenu must leave the normal flow so it does not push the other items sideways. Make the parent item the positioning context, then place the submenu below it.
.has-submenu {
position: relative;
}
.submenu {
display: none;
position: absolute;
top: 100%;
left: 0;
min-width: 12rem;
margin: 0;
padding: 0;
list-style: none;
background: #1f2937;
}
.has-submenu:hover > .submenu,
.has-submenu:focus-within > .submenu {
display: block;
}Each part does one job:
position: relativeon.has-submenumakes it the reference box for the absolute child.top: 100%puts the submenu's top edge at the bottom edge of the parent item.left: 0aligns the left edges of the two boxes.min-width: 12remstops short labels from making a cramped menu.- The
margin,paddingandlist-stylelines repeat the list reset, because.submenuis a newul.
The > child combinator limits the rule to the direct submenu. This matters once you nest menus more than one level deep.
Show the menu on hover and on focus
The last rule has two selectors. :hover serves mouse users. :focus-within matches the parent item whenever any element inside it has focus. When a keyboard user tabs to "Products", the menu opens. Tabbing on moves focus to "Software", which is inside the same item, so the menu stays open. Tabbing past "Hardware" moves focus out and the menu closes.
Common errors
The menu closes when the pointer moves down. If you add a margin to .submenu or use top: calc(100% + 8px), a gap appears between the parent and the submenu. The pointer leaves the parent item while crossing the gap, and :hover stops matching. Keep top: 100% and use padding inside the submenu if you want space.
The submenu appears behind the page content. An absolutely positioned element can be painted under later content. Give the submenu, or the sticky nav from the earlier chapter, a z-index. Set z-index: 10 on the nav and the submenu will sit above the page.
The menu is clipped. If an ancestor has overflow: hidden or overflow: auto, the submenu is cut off at that box. Remove the overflow from the ancestors, or move the dropdown elsewhere in the layout.
The submenu shows its parent's styles. Rules like .nav-list a also match links inside .submenu. The submenu links inherit display: block and the padding, which is usually what you want. If you need different colors, write a more specific selector such as .submenu a.
Touch screens. Phones have no real hover. A tap often triggers :hover and follows the link at once. The toggle button in a later chapter covers small screens. For this chapter, treat the dropdown as a pattern for wide screens with a mouse or keyboard.
Exercise
A user moves the mouse from the "Products" link toward "Software", but the submenu disappears halfway. You find .submenu { top: calc(100% + 10px); }. Why does this break the menu, and how do you fix it?
Show answer
The 10px strip between the parent item and the submenu is not part of either element's hover area. When the pointer crosses it, .has-submenu:hover stops matching and the submenu returns to display: none. Change the rule to top: 100% so the boxes touch. If you want visual space, add padding inside the submenu instead.
Exercise
Which selector makes the submenu open for keyboard users, and why does it keep the menu open while they tab between the child links?
Show answer
.has-submenu:focus-within > .submenu. The :focus-within pseudo-class matches an element when it or any descendant has focus. The child links are descendants of .has-submenu, so the rule keeps matching until focus leaves the item.