← Back to Chapters

CSS Dropdown Menus

⬇️ CSS Dropdown Menus

⚡ Quick Overview

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.

? Key Concepts

  • Dropdown container (.dropdown): wraps the button and the hidden menu.
  • Toggle button: the visible part the user interacts with (e.g., “Menu”).
  • Dropdown content (.dropdown-content): the hidden list of links/options.
  • Positioning: use position: relative; on the container and position: absolute; on the content.
  • Show / hide: control visibility with display: none; and display: block;, often triggered using .dropdown:hover.
  • Layering: use z-index so the menu appears above other elements.

? Syntax / Theory

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.

? View Basic Dropdown CSS
/* 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;
}

? Code Example

Here’s a simple HTML structure for a dropdown menu with three links.

? View HTML Structure
<!-- 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>

? Live Output

Hover over the Menu button to see the dropdown.

? Useful Dropdown Properties

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

? Tips & Best Practices

  • Set position: relative; on the dropdown container so the menu is positioned correctly.
  • Use position: absolute; on the dropdown content to place it just below the button.
  • Hide the menu initially with display: none; and reveal it with .dropdown:hover .dropdown-content.
  • Add padding to dropdown links for a larger, more comfortable clickable area.
  • Use border-radius and box-shadow to create a clean, modern menu panel.
  • Set an appropriate z-index so the dropdown appears above nearby elements.
  • For more complex behavior (click-to-open, closing on outside click, animations), combine CSS with a bit of JavaScript.

? Try It Yourself

  1. Create a dropdown menu with at least 4 links (e.g., Home, About, Services, Contact).
  2. Change the background color of dropdown items and add a :hover effect (e.g., darker background and bold text on hover).
  3. Experiment with positioning by placing the dropdown so it opens to the left or right of the button instead of directly below.
  4. Add icons next to each dropdown link using emoji or icon fonts to improve visual clarity.