CSS Dropdowns

Creating a dropdown menu in CSS allows you to hide a predefined list of options within a button or other elements, keeping your user interface clean and uncluttered. While many developers reach for JavaScript frameworks to handle menus, you can achieve professional, high-performance dropdowns using pure CSS. Let’s explore two common approaches for creating dropdown menus:

Developer Tip: Pure CSS dropdowns are great for performance because they don't require the browser to execute scripts, making your page feel snappier.

Hoverable Dropdown:

A hoverable dropdown appears when the user moves the mouse over an element. This is perfect for "Tooltips" or simple informational overlays. Here’s a simple example:

HTML

<style>
  /* The container holds both the trigger and the content */
  .dropdown {
    position: relative;
    display: inline-block;
  }

  /* The hidden content box */
  .dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
    padding: 12px 16px;
    z-index: 1; /* Ensures the menu sits on top of other elements */
  }

  /* Show the content when the user hovers over the container */
  .dropdown:hover .dropdown-content {
    display: block;
  }
</style>

<div class="dropdown">
  <span>Mouse over me</span>
  <div class="dropdown-content">
    <p>Hello World!</p>
  </div>
</div>

In this example:

  • The .dropdown class uses position: relative. This is crucial because it acts as the "anchor" for the hidden content.
  • The .dropdown-content class uses position: absolute. This allows it to float right under the trigger without pushing other elements on the page around.
  • The magic happens with the :hover pseudo-class. When the parent is hovered, we change the child's display property from none to block.
Common Mistake: Forgetting to set position: relative on the parent container. Without it, the absolutely positioned dropdown will align itself to the whole browser window instead of the button!

Dropdown Menu with Links:

In real-world applications, dropdowns are most commonly used in navigation bars. If you want a dropdown menu with clickable links, consider the following example:

HTML

<style>
  /* Style The Dropdown Button */
  .dropbtn {
    background-color: #4CAF50;
    color: white;
    padding: 16px;
    font-size: 16px;
    border: none;
    cursor: pointer;
    border-radius: 4px;
  }

  /* Container for Dropdown Content */
  .dropdown {
    position: relative;
    display: inline-block;
  }

  /* Dropdown Content (Hidden by Default) */
  .dropdown-content {
    display: none;
    position: absolute;
    background-color: #ffffff;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;
    border-radius: 4px;
    overflow: hidden; /* Ensures child links don't spill over rounded corners */
  }

  /* Links inside the Dropdown */
  .dropdown-content a {
    color: #333;
    padding: 12px 16px;
    text-decoration: none;
    display: block; /* Makes the entire row clickable */
  }

  /* Change Link Color on Hover */
  .dropdown-content a:hover {
    background-color: #f1f1f1;
  }

  /* Show Dropdown Menu on Hover */
  .dropdown:hover .dropdown-content {
    display: block;
  }

  /* Change Button Background Color when Dropdown Content is Shown */
  .dropdown:hover .dropbtn {
    background-color: #3e8e41;
  }
</style>

<div class="dropdown">
  <button class="dropbtn">Settings</button>
  <div class="dropdown-content">
    <a href="#">Account Profile</a>
    <a href="#">Privacy Settings</a>
    <a href="#">Logout</a>
  </div>
</div>

In this example:

  • The .dropbtn class styles the button that triggers the dropdown. We use cursor: pointer to signal to the user that it’s interactive.
  • Inside .dropdown-content a, we set display: block. This is a vital UX improvement because it makes the entire width of the menu clickable, rather than just the text.
  • The box-shadow adds a layer of depth, making the menu appear to "float" above the rest of the content.
Watch Out: Hover-based dropdowns don't work well on mobile devices because there is no "mouse hover" on a touchscreen. For production sites, you may need a small amount of JavaScript to toggle the menu on a "click" event for mobile users.
Best Practice: Always use a <button> element for the dropdown trigger rather than a <div> or <span>. Buttons are natively focusable via the keyboard, which is essential for web accessibility (A11y).