Skip to content
Tutorials Center
My learning

Topics

Lesson 92 of 139 ยท UI Components

Dropdown Menus

Build dropdown menus with :hover and :focus-within, details and summary, and the popover attribute. Position panels well and keep them usable with a keyboard.

A dropdown is a small panel that appears when you ask for it: a list of account options under your avatar, a "Products" submenu in a navigation bar, a set of sort choices above a table. It saves space by hiding choices until they're needed.

Dropdowns look simple, but they hide a surprising number of decisions. How does the panel open: on hover, on click, or both? How does it close? Does it work on a phone, where there is no hover? Can a keyboard user reach every item? In this lesson you will build dropdowns three ways, from the classic CSS hover menu to modern HTML that handles opening and closing for you, and learn which one to choose.

The anatomy of a dropdown

Every dropdown has the same two parts:

  • A trigger: the button (or link) that opens the panel.
  • A panel: the list of choices, positioned next to the trigger and drawn above the content around it.

The classic positioning recipe uses a wrapper as the containing block:

CSS
.dropdown {
  position: relative;          /* the panel is placed relative to this */
}
.dropdown-panel {
  position: absolute;
  top: 100%;                   /* right below the trigger */
  left: 0;
  min-width: 12rem;
  z-index: 10;                 /* above the page content */
}

top: 100% puts the panel's top edge at the bottom of the wrapper. To align it to the right edge instead (useful near the right side of the screen), use right: 0 instead of left: 0. The z-index lifts it above later content, although a parent with overflow: hidden or its own stacking context can still clip or bury it. z-index and Stacking Contexts explains those traps, and the popover approach later in this lesson avoids them completely.

Hover and :focus-within

The oldest CSS-only dropdown shows the panel while the pointer is over the wrapper:

CSS
.dropdown-panel { display: none; }
.dropdown:hover .dropdown-panel { display: block; }

That alone fails keyboard users: they can Tab to the trigger, but the panel never opens. The fix is :focus-within, which matches an element when it or anything inside it has focus. When focus lands on the trigger or moves into the panel's links, the panel stays open; when focus leaves, it closes:

CSS
.dropdown:hover .dropdown-panel,
.dropdown:focus-within .dropdown-panel {
  display: block;
}
A navigation dropdown with hover and focus

A few details in this demo matter more than they look:

  • No gap between trigger and panel. If there's a margin between them, the pointer crosses empty space on its way down, :hover stops matching, and the menu snaps shut. The demo uses padding-top on the panel instead, so the hover area is continuous.
  • visibility instead of display. display can't be transitioned in the classic way, but visibility can be delayed. When closing, visibility waits 150 ms (transition: visibility 0s 150ms) so the fade-out can finish; when opening, the delay is reset to 0s. This also hides the links from keyboard focus and screen readers while the panel is closed. Transitions covers the technique.
  • The trigger is a real link. "Courses" goes to a courses page, so on a phone, where hover doesn't exist, tapping it still takes the user somewhere useful.

Hover menus have limits. Touch devices have no hover, so a tap usually triggers the link straight away. Hover menus also close when the pointer drifts slightly, which is hard for people with tremors. And screen reader users get no hint that a submenu exists. Hover dropdowns are acceptable for site navigation where the trigger is itself a link, but for anything else, prefer a menu that opens on click.

Click to open: <details> and <summary>

The <details> element is a built-in disclosure widget: clicking its <summary> toggles the content, and it works with the keyboard (Enter or Space) and with screen readers, which announce it as expanded or collapsed. No JavaScript needed. Collapsible Sections with Details covers the element; here we style it into a dropdown.

The tricks are to remove the default triangle marker, position the content absolutely, and target the open state with the [open] attribute:

CSS
.menu summary { list-style: none; }               /* hide the marker */
.menu summary::-webkit-details-marker { display: none; }  /* older Safari */
.menu[open] summary { background: #eef2ff; }
A details-based dropdown

The arrow is a rotated border on ::after, so it flips when the menu opens. Because the gap is fine here (top: calc(100% + 6px)), there's no hover to lose.

<details> has two limits as a dropdown. It doesn't close when you click elsewhere or press Escape; that takes a few lines of JavaScript. And its content is still inside the page's normal stacking, so a parent with overflow: hidden will clip it. For a group of dropdowns where only one should be open at a time, give them the same name attribute and the browser closes the others automatically (supported in all current browsers since 2024).

Click to open with a little JavaScript

Before popover existed, click-driven menus were built with a few lines of script, and you will still find this pattern in many code bases. It gives you full control over behavior, at the cost of writing the accessibility details yourself. The recipe has four parts:

  1. A real <button> as the toggle, with aria-expanded="false" and aria-controls pointing at the menu.
  2. CSS that shows the menu only when its wrapper has a class such as is-open.
  3. A click handler that toggles the class and updates aria-expanded.
  4. Two closing rules: clicking anywhere outside, and pressing Escape (which should also return focus to the button).
A click dropdown with outside-click and Escape

This works, but compare it with the popover version below. You wrote the outside click, Escape handling, focus return and aria-expanded yourself, and the menu can still be clipped by an ancestor with overflow: hidden, because it is not in the top layer. If the browsers you must support have popover, prefer it. Keep the script approach when you need behavior the attribute does not give you, such as keeping one menu open at a time or closing on scroll.

Modern menus with the popover attribute

The popover attribute solves both of those problems in plain HTML. A button with popovertarget toggles the element, and the browser:

  • Puts the popover in the top layer, above everything, so no overflow or z-index can clip it.
  • Light-dismisses it: clicking outside or pressing Escape closes it.
  • Returns focus to the button when it closes with Escape.
  • Exposes the button's expanded state to assistive technology, so you don't need to maintain aria-expanded yourself.

The catch is positioning. A popover is in the top layer, so position: absolute relative to a wrapper no longer works; by default it's centered in the viewport. Anchor positioning attaches it to its button, and the demo wraps that part in a feature query so older browsers get a centered panel instead.

A popover dropdown with transitions

The animation uses two newer features. @starting-style gives the "before" values for an element that has just appeared, so the opening fades in. transition-behavior: allow-discrete (written as the allow-discrete keyword in the shorthand) lets display and overlay wait until the fade-out finishes before the popover disappears. Browsers without them simply show and hide the menu instantly, which is a fine fallback.

Popovers are supported in all current browsers; anchor positioning is newer, so keep the @supports guard.

Keyboard and screen reader basics

Whichever technique you use, check these:

  1. The trigger is a <button> (or a <summary>, or a link when it navigates). A <div> with a click handler can't be focused or activated by keyboard without extra work.
  2. The expanded state is announced. <details> and popover triggers do this automatically. If you build a dropdown with JavaScript that toggles a class, toggle aria-expanded="true"/"false" on the button too. You can even style from it: .trigger[aria-expanded="true"].
  3. Escape closes the panel, and focus returns to the trigger.
  4. Tab moves through the items in a logical order. The panel should come right after the trigger in the HTML, so the focus order matches what you see.
  5. Visible focus on the trigger and every item; see Accessible CSS.

Be careful with role="menu". It sounds right, but it tells screen readers to expect application-style menus, navigated with arrow keys instead of Tab. If you add it, you must also implement that keyboard behavior in JavaScript. For navigation links and simple action lists, a plain list of links or buttons, as in this lesson, is more accessible.

What about styling <select>?

If your dropdown is really a choice of one option for a form, start with a native <select>. It's accessible and works on every device. Styling Forms shows how to restyle its box with appearance: none. Chromium browsers have also started shipping a fully customizable select (appearance: base-select), where you can style the options list itself; until other browsers support it, treat it as an enhancement.

Choosing a technique

Need Use
Navigation submenu whose trigger is also a link Hover + :focus-within
Simple toggle, no JavaScript, inside normal flow <details> + <summary>
Action menu that must float above everything and close on outside click popover + anchor positioning
Choosing a value in a form A native <select>

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why add :focus-within to a hover dropdown?

    :focus-within matches when the wrapper or anything inside it has focus, so tabbing into the dropdown keeps it open.
  2. 2. A hover dropdown closes when the pointer moves from the trigger to the panel. What is the most likely cause?

    Crossing empty space ends the hover. Use padding on the panel, or a pseudo-element bridge, so the hover area is continuous.
  3. 3. What does the popover attribute give you that a <details> dropdown doesn't?

    Both work with the keyboard. Popovers also render above everything and light-dismiss without JavaScript.
  4. 4. You build a dropdown that toggles a CSS class with JavaScript. What should the trigger button also get?

    aria-expanded tells screen reader users whether the panel is open. <details> and popover triggers handle this for you.

Summary

  • A dropdown is a trigger plus a positioned panel; the classic recipe is a position: relative wrapper and a position: absolute panel with top: 100%.
  • Hover menus need :focus-within for keyboards, no gap between trigger and panel, and a trigger that still works on touch screens.
  • <details>/<summary> give a click-to-open dropdown with keyboard and screen reader support and no JavaScript; style the open state with [open].
  • The popover attribute adds top-layer rendering and light dismiss; pair it with anchor positioning inside @supports, and animate it with @starting-style and allow-discrete.
  • Use real buttons, announce the expanded state, close on Escape, and avoid role="menu" unless you implement its full keyboard pattern.

Next: Tooltips, where you build small hint bubbles with pseudo-elements, arrows and accessible markup.