All notes

Hover menus that close before you get there

A dropdown that vanishes on the way to its third item is a gap problem. One CSS transition delay fixes it, no JavaScript.

By 3 min readCSS, UX

Move your cursor from "Products" down to the third item in its menu, and on a lot of sites the menu closes halfway there.

Try it below. The two menus are identical except for one line of CSS. Move down slowly on the left one and you lose it in the gap. Do the same on the right one and it waits for you.

Plain :hover (no delay)
Try to click “Integrations”
Close delayed 300ms
Try to click “Integrations”
Move down slowly, then quickly, from each button to its third item

Why the menu closes

Most menus open with :hover on the nav item, and the menu sits a little below it. That space is usually there on purpose, because a menu glued to the button looks cramped.

The trouble is the cursor has to cross it. For the few frames it spends in the gap, it's hovering neither the trigger nor the menu, so :hover turns off and the menu hides before the cursor arrives.

.nav-item .menu {
  visibility: hidden;
}
.nav-item:hover .menu {
  visibility: visible;
}

Delay the close, not the open

visibility can be transitioned. It isn't animated in steps, it flips at the end of the transition, and that's exactly what we want: a timer in CSS.

Give the hidden state a delay, and remove the delay on hover:

menu.css
.nav-item .menu {
  visibility: hidden;
  transition: visibility 0s 300ms; /* stay visible 300ms after hover ends */
}
 
.nav-item:hover .menu,
.nav-item:focus-within .menu {
  visibility: visible;
  transition-delay: 0s; /* but open right away */
}

Which rule's transition runs depends on the state you're moving into. Going to hover uses the hover rule, so the delay is 0s and the menu opens immediately. Leaving hover uses the base rule, so it waits 300ms. A cursor that crosses the gap in less than that is back inside the menu before it hides.

If you also fade the menu, delay the fade by the same amount, or the menu will be invisible but still clickable while it waits. That's what the demo does:

.nav-item .menu {
  opacity: 0;
  visibility: hidden;
  transition:
    opacity 120ms ease 300ms,
    visibility 0s linear 420ms;
}

Adding :focus-within keeps the menu open while someone tabs through its links with the keyboard.

How long?

Drag the slider in the demo to 800ms and then try to leave. The menu hangs around after you've clearly moved on, and it starts to feel sticky. Somewhere around 200 to 300ms covers the trip across a normal gap without that.

Wide menus need more than a delay

A delay covers a straight trip down. On a mega menu, people cut diagonally from the trigger to an item on the far side, and on the way the cursor brushes over other nav items, which open their own menus.

The fix for that is the "safe triangle". Amazon's department menu was the well-known example: while the cursor is moving toward the open submenu, inside a triangle between the pointer and the submenu's edges, the menu ignores the items it passes over. Ben Kamens wrote it up and released it as jQuery-menu-aim, and most later implementations borrow its idea of checking the pointer's direction instead of just waiting.

For a normal dropdown, the CSS delay is all you need.

Need this on your site?

I build interfaces like this for agencies, startups and SaaS teams.

Let's talk