On this page9 sections
  1. Learning Objectives
  2. 1. The Basic Dropdown
  3. 2. Right-Aligned Dropdown
  4. 3. A Dropdown in a Navigation Bar
  5. 4. A Click Dropdown With <details>
  6. Common Beginner Mistakes
  7. Practice Questions
  8. Practice Answers
  9. Key Takeaways

Lecture 022: Dropdowns

Category5: Pseudo-classes & Components
Lecture022 of 057
Estimated video length10 min
TargetCSS3 · any modern browser
LevelBeginner

A dropdown hides a list of options until you hover or click. Pure CSS can build one with position, display and :hover. Adding :focus-within makes it work with the keyboard too.


Learning Objectives

  • Build a hover dropdown with position: absolute and :hover
  • Make it keyboard-friendly with :focus-within
  • Align a dropdown to the right, and add one to a navigation bar
  • Know when to use <details> or the Popover API instead

1. The Basic Dropdown

Example: hover over the button:

html
<style>
  .dropdown { position: relative; display: inline-block; }
  .menu {
    display: none;
    position: absolute;
    top: 100%;
    left: 0;
    min-width: 170px;
    background: white;
    border: 1px solid #dee2e6;
    border-radius: 8px;
    box-shadow: 0 8px 20px rgb(0 0 0 / 0.12);
    z-index: 10;
  }
  .menu a { display: block; padding: 10px 14px; color: #343a40; text-decoration: none; }
  .menu a:hover { background: #f1f3f5; }
  .dropdown:hover .menu,
  .dropdown:focus-within .menu { display: block; }
  .btn { padding: 10px 16px; background: #1971c2; color: white; border: 0; border-radius: 6px; }
</style>

<div class="dropdown">
  <button class="btn" type="button">Account ▾</button>
  <div class="menu">
    <a href="#">Profile</a>
    <a href="#">Settings</a>
    <a href="#">Log out</a>
  </div>
</div>
<p style="margin-top: 140px;">The menu floats above this text.</p>

How it works:

  1. The wrapper is position: relative, so the menu is placed relative to it.
  2. The menu is position: absolute; top: 100%, directly below the button, floating over the page.
  3. It's display: none until the wrapper is hovered (:hover) or something inside it has focus (:focus-within).

2. Right-Aligned Dropdown

Swap left: 0 for right: 0, so the menu doesn't run off the right edge of the screen:

css
.menu.right { left: auto; right: 0; }

3. A Dropdown in a Navigation Bar

Example

html
<style>
  nav ul { list-style: none; margin: 0; padding: 0; display: flex; background: #212529; }
  nav a { display: block; padding: 12px 16px; color: #e9ecef; text-decoration: none; }
  nav li { position: relative; }
  nav li:hover > a { background: #343a40; }
  nav .sub { display: none; position: absolute; top: 100%; left: 0; min-width: 160px; background: #343a40; padding: 0; z-index: 10; }
  nav li:hover .sub, nav li:focus-within .sub { display: block; }
</style>

<nav>
  <ul>
    <li><a href="#">Home</a></li>
    <li><a href="#">Courses ▾</a>
      <ul class="sub">
        <li><a href="#">HTML</a></li>
        <li><a href="#">CSS</a></li>
        <li><a href="#">Flutter</a></li>
      </ul>
    </li>
    <li><a href="#">Contact</a></li>
  </ul>
</nav>
<div style="height: 140px;"></div>

4. A Click Dropdown With <details>

Hover doesn't exist on phones. <details> opens on tap, with no JavaScript:

Example

html
<style>
  details.dd { position: relative; display: inline-block; }
  details.dd summary { list-style: none; cursor: pointer; padding: 8px 14px; background: #e7f5ff; border-radius: 6px; }
  details.dd summary::-webkit-details-marker { display: none; }
  details.dd .menu { position: absolute; top: 110%; left: 0; background: white; border: 1px solid #dee2e6; border-radius: 8px; min-width: 150px; }
  details.dd .menu a { display: block; padding: 8px 12px; color: #343a40; text-decoration: none; }
</style>

<details class="dd">
  <summary>Sort by ▾</summary>
  <div class="menu"><a href="#">Newest</a><a href="#">Price: low to high</a><a href="#">Rating</a></div>
</details>
<div style="height: 120px;"></div>

For a pop-up that also closes when you click elsewhere, use the Popover API (HTML Lecture 045).


Common Beginner Mistakes

1. Forgetting position: relative on the wrapper. The menu appears at the top of the page.

2. Hover-only menus. They don't work on touch screens or with the keyboard. Add :focus-within, or use <details> or a popover.

3. The menu hidden behind other content. Give it a z-index.

4. A gap between button and menu. The mouse "falls through" the gap and the menu closes. Use top: 100% with no margin.


Practice Questions

  1. Which position does the dropdown wrapper need?
  2. Which position does the menu itself use?
  3. Which selector shows the menu when the mouse is over the wrapper?
  4. What does :focus-within add?
  5. How do you stop a menu running off the right edge of the screen?
  6. Which HTML element gives a tap-to-open dropdown with no JavaScript?

Practice Answers

  1. position: relative
  2. position: absolute (with top: 100%)
  3. .dropdown:hover .menu { display: block; }
  4. It opens the menu when a keyboard user tabs into it.
  5. Align it with right: 0 instead of left: 0.
  6. <details> with a <summary>.

Key Takeaways

  • Wrapper relative + menu absolute; top: 100% + display: none.
  • Show it with :hover and :focus-within.
  • z-index keeps it on top; right: 0 aligns it right.
  • For touch-friendly menus, use <details> or the Popover API.