TExcelTAD
Home › CSS › CSS Dropdowns

CSS Dropdowns

A dropdown shows extra content — a menu, a panel of options — when the user hovers or focuses a trigger. You can build one with pure CSS using three ingredients: a relatively positioned wrapper, an absolutely positioned panel that's hidden by default, and a :hover / :focus-within rule that reveals it.

The basic pattern

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; padding: 16px; }
  .dropdown { position: relative; display: inline-block; }
  .trigger { padding: 10px 16px; background: #3949ab; color: white; border: none; border-radius: 6px; font-size: 15px; }
  .panel {
    display: none;
    position: absolute;
    top: 100%;
    left: 0;
    min-width: 170px;
    background: white;
    box-shadow: 0 8px 20px rgba(0,0,0,.18);
    border-radius: 8px;
    padding: 10px 12px;
    z-index: 10;
  }
  .dropdown:hover .panel { display: block; }
</style>
</head>
<body>
  <div class="dropdown">
    <button class="trigger">Hover me ▾</button>
    <div class="panel">Hello! I'm a dropdown panel.</div>
  </div>
  <p>Text underneath is covered, not pushed down.</p>
</body>
</html>
Piece Why
position: relative on wrapper Makes it the containing block for the panel
position: absolute; top: 100% on panel Places the panel right below the trigger, out of flow
display: none → block on hover Hides and shows it
z-index Keeps the panel above later content
min-width Stops the panel from being narrower than its contents need

Dropdown menu of links

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; margin: 0; }
  nav { display: flex; background: #212121; }
  nav > a, .dd > button { padding: 14px 16px; color: white; text-decoration: none; background: none; border: none; font: inherit; cursor: pointer; }
  nav > a:hover, .dd:hover > button, .dd:focus-within > button { background: #424242; }
  .dd { position: relative; }
  .menu { display: none; position: absolute; top: 100%; left: 0; min-width: 160px; background: white; box-shadow: 0 6px 16px rgba(0,0,0,.25); z-index: 1; }
  .menu a { display: block; padding: 10px 14px; color: #212121; text-decoration: none; }
  .menu a:hover { background: #fff3e0; color: #e65100; }
  .dd:hover .menu, .dd:focus-within .menu { display: block; }
</style>
</head>
<body>
  <nav>
    <a href="#">Home</a>
    <div class="dd">
      <button>Products ▾</button>
      <div class="menu"><a href="#">Laptops</a><a href="#">Phones</a><a href="#">Accessories</a></div>
    </div>
    <a href="#">Contact</a>
  </nav>
</body>
</html>

Tip: Adding :focus-within next to :hover lets keyboard users open the menu with Tab — the menu stays open while focus is anywhere inside it.

Alignment and animation

Use right: 0 instead of left: 0 to align the panel with the trigger's right edge (useful near the screen edge). To animate, you can't transition display, so toggle opacity, visibility and transform instead.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; display: flex; justify-content: flex-end; padding: 16px; }
  .dd { position: relative; }
  .avatar { width: 44px; height: 44px; border-radius: 50%; background: linear-gradient(135deg, #26c6da, #7e57c2); border: none; cursor: pointer; }
  .menu {
    position: absolute; right: 0; top: calc(100% + 8px); width: 180px;
    background: white; border-radius: 10px; box-shadow: 0 10px 24px rgba(0,0,0,.2); padding: 6px 0;
    opacity: 0; visibility: hidden; transform: translateY(-8px);
    transition: opacity .2s, transform .2s, visibility .2s;
  }
  .dd:hover .menu, .dd:focus-within .menu { opacity: 1; visibility: visible; transform: none; }
  .menu a { display: block; padding: 9px 14px; color: #333; text-decoration: none; }
  .menu a:hover { background: #ede7f6; }
</style>
</head>
<body>
  <div class="dd">
    <button class="avatar" aria-label="Account"></button>
    <div class="menu"><a href="#">Profile</a><a href="#">Billing</a><a href="#">Sign out</a></div>
  </div>
</body>
</html>

Click-to-open with details/summary

The HTML <details> element gives you a click-toggled dropdown with no JavaScript, and it's accessible by default.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; padding: 16px; }
  details { position: relative; display: inline-block; }
  summary { list-style: none; cursor: pointer; padding: 10px 14px; background: #00897b; color: white; border-radius: 6px; }
  summary::-webkit-details-marker { display: none; }
  summary::after { content: " ▾"; }
  details[open] summary::after { content: " ▴"; }
  .opts { position: absolute; top: 110%; left: 0; background: white; border: 1px solid #ddd; border-radius: 6px; padding: 6px 0; min-width: 150px; }
  .opts label { display: block; padding: 6px 12px; }
</style>
</head>
<body>
  <details>
    <summary>Filter</summary>
    <div class="opts">
      <label><input type="checkbox"> In stock</label>
      <label><input type="checkbox"> On sale</label>
      <label><input type="checkbox"> Free shipping</label>
    </div>
  </details>
</body>
</html>

Test yourself

Why does the dropdown wrapper need position: relative?

  1. To make it clickable
  2. So the absolutely positioned panel is placed relative to it
  3. To add a shadow
  4. To hide the panel

Why can't you fade a dropdown in by transitioning display?

  1. display is not animatable between none and block in the classic way
  2. display only accepts numbers
  3. Transitions don't work on hover
  4. It needs !important

Which selector keeps a dropdown open for keyboard users tabbing through its links?

  1. .dd:active .menu
  2. .dd:focus-within .menu
  3. .dd:visited .menu
  4. .menu:checked