Menu
Coddy logo textTech

Dropdown menu

Part of the Practical Frontend section of Coddy's HTML journey — lesson 20 of 35.

A dropdown menu is a navigation item that shows more links when the user hovers over or focuses on it. While dropdowns can be built using JavaScript, in this chapter we focus on a CSS-only approach, which uses :hover or :focus-within to reveal the hidden menu.

Why use them:

  • Saves space in navigation bars.
  • Keeps the interface clean and organized.
  • Works well for mobile-first and responsive designs when adapted properly.

HTML:

<div class="dropdown">
  <button class="dropdown-button">Menu</button>
  <div class="dropdown-content">
    <a href="#">Option 1</a>
    <a href="#">Option 2</a>
    <a href="#">Option 3</a>
  </div>
</div>

CSS: 

//The .dropdown is position: relative 
//so the dropdown content can be positioned correctly with position: absolute.
.dropdown {
  position: relative;
  display: inline-block;
}
//The .dropdown-content is hidden by default using display: none.
.dropdown-content {
  display: none;
  position: absolute;
}
//When the user hovers over the .dropdown container, 
//the .dropdown-content is shown using display: block.
.dropdown:hover .dropdown-content {
  display: block;
}

With this code, when you hover over the dropdown container (including the button), the dropdown content will appear.

challenge icon

Challenge

Easy

You are given the HTML for a dropdown menu with a button labeled "Select Color" and three options: Red, Green, Blue.

Your task is to style the dropdown with CSS so that:

  1. The options are hidden by default.
  2. The options become visible when the user hovers over the dropdown container.

Cheat sheet

A dropdown menu shows more links when the user hovers over or focuses on it. Use :hover or :focus-within to reveal the hidden menu.

HTML structure:

<div class="dropdown">
  <button class="dropdown-button">Menu</button>
  <div class="dropdown-content">
    <a href="#">Option 1</a>
    <a href="#">Option 2</a>
    <a href="#">Option 3</a>
  </div>
</div>

CSS implementation:

.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-content {
  display: none;
  position: absolute;
}

.dropdown:hover .dropdown-content {
  display: block;
}

The dropdown container uses position: relative so the dropdown content can be positioned with position: absolute. The content is hidden by default with display: none and shown on hover with display: block.

Try it yourself

<!DOCTYPE html>
<html>
<head>
  <title>Dropdown menu</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      padding: 50px;
      background-color: #f0f0f0;
    }

    .dropdown {
      position: relative;
      display: inline-block;
    }

    .dropdown-button {
      padding: 10px 20px;
      font-size: 16px;
      cursor: pointer;
      border: 1px solid #333;
      border-radius: 5px;
      background-color: #ffffff;
    }

    .dropdown-content {
      position: absolute;
      top: 100%;
      left: 0;
      background-color: #ffffff;
      min-width: 160px;
      border: 1px solid #ccc;
      border-radius: 5px;
      box-shadow: 0 2px 5px rgba(0,0,0,0.2);
      z-index: 1;
    }

    .dropdown-content a {
      display: block;
      padding: 10px;
      text-decoration: none;
      color: #333;
    }

    .dropdown-content a:hover {
      background-color: #f2f2f2;
    }

    /* Show dropdown on hover or focus */
    .dropdown:hover .dropdown-content,
    .dropdown:focus-within .dropdown-content {

    }
  </style>
</head>
<body>

  <div class="dropdown">
    <button class="dropdown-button">Select Color</button>
    <div class="dropdown-content">
      <a href="#">Red</a>
      <a href="#">Green</a>
      <a href="#">Blue</a>
    </div>
  </div>

</body>
</html>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Practical Frontend