Lesson

CSS Dropdowns

10 min reading
Free Course

CSS Dropdowns: Build Hover and Keyboard-Accessible Menus.

CSS dropdowns reveal a hidden submenu or list of actions when someone interacts with a button or navigation link. You can build one with plain CSS position: relative On the wrapper, position: absolute on the menu panel, and:focus-within so keyboard users get the same behavior as mouse users.

This guide shows a complete working dropdown, explains the hover-gap bug that closes menus unexpectedly, and covers when to move to a click-based version.

How CSS Dropdowns Work

A dropdown has three parts:

  • A wrapper that anchors the menu (.dropdown).

  • A trigger, usually a <button> (.dropdown-trigger).

  • A menu panel that stays hidden until the wrapper is hovered or focused (.dropdown-menu).

The wrapper usesposition: relative, so the panel, which usesposition: absolute, is placed relative to it and doesn't push other content around.

A Complete CSS Dropdown Example

The HTML

<div class="dropdown">
  <button class="dropdown-trigger" type="button">Options &#9662;</button>
  <ul class="dropdown-menu">
    <li><a href="/edit">Edit lesson</a></li>
    <li><a href="/export">Export JSON</a></li>
  </ul>
</div>

The CSS

.dropdown {
    position: relative;
    display: inline-block;
}

.dropdown-trigger {
    padding: 10px 16px;
    background-color: #1e293b;
    color: #ffffff;
    border: none;
    border-radius: 6px;
    cursor: pointer;
}

.dropdown-menu {
    position: absolute;
    top: calc(100% + 6px);
    left: 0;
    min-width: 190px;
    margin: 0;
    list-style: none;
    background-color: #ffffff;
    border: 1px solid #cbd5e1;
    border-radius: 8px;
    padding: 6px;
    box-shadow: 0 10px 25px rgba(15, 23, 42, 0.12);
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transition: opacity 0.15s ease, visibility 0.15s;
    z-index: 30;
}

.dropdown-menu a {
    display: block;
    padding: 8px 10px;
    color: #0f172a;
    text-decoration: none;
    border-radius: 4px;
}

.dropdown-menu a:hover,
.dropdown-menu a:focus-visible {
    background-color: #e2e8f0;
}

.dropdown:hover .dropdown-menu,
.dropdown:focus-within .dropdown-menu {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
}

Positioning and Revealing the Dropdown Panel

Anchor the dropdown list directly underneath its parent trigger using position: relative on .dropdown and position: absolute; top: 100%; left: 0; on .dropdown-menu. Using opacity + visibility + pointer-events: none (rather than display: none) allows smooth transitions while preventing accidental clicks when closed.

Pairing .dropdown:hover .dropdown-menu with .dropdown:focus-within .dropdown-menu ensures keyboard users tabbing into the trigger see the submenu open automatically.

.dropdown {
    position: relative;
    display: inline-block;
}

.dropdown-trigger {
    padding: 10px 16px;
    background-color: #1e293b;
    color: #ffffff;
    border: none;
    border-radius: 6px;
    cursor: pointer;
}

.dropdown-menu {
    position: absolute;
    top: calc(100% + 6px);
    left: 0;
    min-width: 190px;
    background-color: #ffffff;
    border: 1px solid #cbd5e1;
    border-radius: 8px;
    padding: 6px;
    box-shadow: 0 10px 25px rgba(15, 23, 42, 0.12);
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transition: opacity 0.15s ease;
    z-index: 30;
}

.dropdown:hover .dropdown-menu,
.dropdown:focus-within .dropdown-menu {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
}

Hover Gap Warning

Notice a subtle trap: if there is an empty 6px gap between the button and.dropdown-menu, moving the mouse slowly downward leaves :hover and snaps the menu shut! Bridge the gap with transparent padding on a ::before pseudo-element, or trigger menus on click with JavaScript + aria-expanded for complex navigation.

How to Check That Your Dropdown Works

  1. Hover the trigger and move the mouse down slowly to the first link. The menu should stay open.

  2. Press Tab from the page. Focus should reach the button, the menu should open, and the next Tab should reach the first link.

  3. Tab past the last link. The menu should close.

  4. Resize to phone width and test with a touch device or device emulation. If tapping doesn't open the menu, switch to the click version.

  5. In DevTools, check that a parent doesn't clip the menu and that it appears above nearby cards.

  6. For the click version, press Escape with the menu open and confirm it closes and returns focus to the button.

Common Mistakes to Avoid

  • Using :hover alone without :focus-within or click support: Touch screens and keyboard-only visitors cannot hover reliably.

  • Putting overflow: hidden on a parent header: Overflow clipping slices dropdown panels right at the navbar's bottom edge.

  • Forgetting z-index on the menu: Other cards further down the page may overlap behind or over your dropdown without z-index.

Frequently Asked Questions

Should main navigation dropdowns open on hover or click?

Click-triggered menus (toggling aria-expanded in small JavaScript) are more reliable on touch devices and screen readers, while :hover + :focus-within works cleanly for quick CSS prototypes.

How do I align a right-edge dropdown so it doesn't overflow off screen?

Replace left: 0; with right: 0; on .dropdown-menu when the button sits near the right edge of the viewport.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
CSS Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum