On this page9 sections
Lecture 022: Dropdowns
| Category | 5: Pseudo-classes & Components |
| Lecture | 022 of 057 |
| Estimated video length | 10 min |
| Target | CSS3 · any modern browser |
| Level | Beginner |
A dropdown hides a list of options until you hover or click. Pure CSS can build one with
position,displayand:hover. Adding:focus-withinmakes it work with the keyboard too.
Learning Objectives
- Build a hover dropdown with
position: absoluteand: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:
<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:
- The wrapper is
position: relative, so the menu is placed relative to it. - The menu is
position: absolute; top: 100%, directly below the button, floating over the page. - It's
display: noneuntil 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:
.menu.right { left: auto; right: 0; }3. A Dropdown in a Navigation Bar
Example
<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
<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
- Which position does the dropdown wrapper need?
- Which position does the menu itself use?
- Which selector shows the menu when the mouse is over the wrapper?
- What does
:focus-withinadd? - How do you stop a menu running off the right edge of the screen?
- Which HTML element gives a tap-to-open dropdown with no JavaScript?
Practice Answers
position: relativeposition: absolute(withtop: 100%).dropdown:hover .menu { display: block; }- It opens the menu when a keyboard user tabs into it.
- Align it with
right: 0instead ofleft: 0. <details>with a<summary>.
Key Takeaways
- Wrapper
relative+ menuabsolute; top: 100%+display: none. - Show it with
:hoverand:focus-within. z-indexkeeps it on top;right: 0aligns it right.- For touch-friendly menus, use
<details>or the Popover API.