A CSS dropdown menu is a small panel of links or actions that appears when a user hovers over or clicks a button. You can build simple, fully functional dropdowns using only :hover and CSS positioning — no JavaScript required.
Dropdowns are commonly used for navigation menus, user profile menus, or option pickers in modern web UIs.
.dropdown): wraps the button and the hidden menu..dropdown-content): the hidden list of links/options.position: relative; on the container and position: absolute; on the content.display: none; and display: block;, often triggered using .dropdown:hover.z-index so the menu appears above other elements.A basic CSS-only dropdown uses a parent container with relative positioning and a child menu with absolute positioning. The child menu is hidden by default and revealed when the parent is hovered.
/* Dropdown container and hover-based menu */
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown:hover .dropdown-content {
display: block;
}
Here’s a simple HTML structure for a dropdown menu with three links.
<!-- Simple dropdown menu -->
<div class="dropdown">
<button class="dropdown-button">Menu</button>
<div class="dropdown-content">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</div>
Common CSS properties you’ll use when styling dropdown menus:
| Property | Description | Example Value |
|---|---|---|
position |
Controls how the dropdown content is positioned. | relative, absolute |
display |
Controls visibility of dropdown content. | none, block |
background-color |
Sets the background of dropdown content. | #f1f1f1 |
box-shadow |
Adds a soft shadow around the dropdown panel. | 0px 8px 16px 0px rgba(0,0,0,0.2) |
z-index |
Controls stacking order so the menu appears on top. | 1 or higher |
cursor |
Changes cursor when hovering over the button. | pointer |
position: relative; on the dropdown container so the menu is positioned correctly.position: absolute; on the dropdown content to place it just below the button.display: none; and reveal it with .dropdown:hover .dropdown-content.padding to dropdown links for a larger, more comfortable clickable area.border-radius and box-shadow to create a clean, modern menu panel.z-index so the dropdown appears above nearby elements.:hover effect (e.g., darker background and bold text on hover).