← Back to HTML & CSS, page 7

html-css-0140 · Hard senior

A CSS-only dropdown menu opens on hover. What accessibility problems does it have, and how do you improve it?

A hover-only dropdown fails for keyboard, touch and screen reader users and cannot be dismissed. Use a button with aria-expanded that toggles the menu, close it on Escape and when focus leaves, return focus to the button, and consider whether a dropdown is needed.

Full answer

A common CSS-only dropdown is a list of links inside a wrapper, hidden until .wrap:hover. It works with a mouse and fails in many other cases.

Problems:

  • Keyboard users cannot open it. Hover does not happen with Tab. Adding :focus-within fixes the opening part: the menu shows while any element inside the wrapper has focus. If the menu uses display: none, the links inside cannot be focused at all. Then the trigger has to be a button that is focusable, and the menu opens when the button has focus.
  • Touch users have no hover. A tap on the button should toggle the menu, so a script is needed.
  • No Escape key. The guidelines ask for hidden-on-hover content to be dismissible.
  • No state for screen readers. A button needs aria-expanded that reflects the open state, which CSS alone cannot update.
  • The pointer gap. A gap between the trigger and the menu closes the menu as the pointer crosses it. Make the menu touch the trigger, or bridge the gap with padding.

An improved approach keeps CSS for the look and adds a few lines of script:

  1. A real button with aria-expanded and aria-controls toggles the menu on click, and on Enter and Space.
  2. The menu is hidden with the hidden attribute (or display: none) when closed.
  3. Escape closes it and returns focus to the button.
  4. Clicking outside, or moving focus out of the wrapper, closes it.
  5. Optionally open on hover with @media (hover: hover) as an extra.

For site navigation, a simple list of links that always stays open on small screens is often better than a dropdown.

Clicking or pressing Enter on the button opens the list. Tab moves into the links. Escape closes the list and returns focus to the button. Moving focus out of the wrapper closes it.

Test with a keyboard, a touch screen and a screen reader before shipping navigation like this.

html
<style>
  .wrap { position: relative; display: inline-block; }
  .menu { position: absolute; top: 100%; left: 0; margin: 0; padding: 0; list-style: none; background: #eee; }
  .menu[hidden] { display: none; }
  .menu a { display: block; padding: 6px 12px; }
</style>
<div class="wrap" id="w">
  <button type="button" id="btn" aria-expanded="false" aria-controls="m">Courses</button>
  <ul class="menu" id="m" hidden>
    <li><a href="#html" id="l1">HTML</a></li>
    <li><a href="#css">CSS</a></li>
  </ul>
</div>
<script>
  function setOpen(o) { btn.setAttribute("aria-expanded", o); m.hidden = !o; }
  btn.addEventListener("click", () => setOpen(btn.getAttribute("aria-expanded") !== "true"));
  w.addEventListener("keydown", e => { if (e.key === "Escape") { setOpen(false); btn.focus(); } });
  w.addEventListener("focusout", e => { if (!w.contains(e.relatedTarget)) setOpen(false); });
</script>

Checked with mouse clicks and real key presses in Chrome. No screen reader was run, and touch was not tested. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • Hover does not work with keyboard or touch.
  • :focus-within cannot reach links hidden with display: none.
  • Use a button with aria-expanded and aria-controls.
  • Escape and focus-out should close the menu.
  • Avoid pointer gaps between trigger and menu.

Common mistakes

  • Opening a menu only on :hover.
  • Using display: none and :focus-within and expecting hidden links to receive focus.
  • Leaving a gap between the trigger and the menu so the menu closes as the pointer crosses it.

Follow-ups

  • Why can :focus-within not open a menu hidden with display: none?
  • When is a dropdown not needed at all?

Tips

  • List the missing pieces: keyboard, touch, Escape, state, pointer gap.
  • Say that a button with aria-expanded is the base of the fix.