CSS – Dropdowns

By Published 3 min read

Certainly! In CSS, “dropdowns” refer to a common user interface element often used in navigation menus or forms. A dropdown is essentially a list of options that remains hidden until a user interacts with it, typically by clicking or hovering over a specific trigger element, like a button or a navigation link.

Dropdown menus are widely used to save space on a webpage and provide a more organized way to present multiple choices without cluttering the screen. When a user activates a dropdown, it “drops down” to reveal the available options, and the user can then select one of them.

CSS Hoverable Dropdowns

Certainly! In CSS, “hoverable dropdowns” are interactive elements often used in web design to create menus or submenus that appear when a user hovers their cursor over a specific trigger element. These dropdowns enhance user experience by providing quick access to additional options or information.

To implement hoverable dropdowns in CSS, you typically use a combination of HTML and CSS. Here’s a basic example:

Imagine you have a navigation menu, and you want a dropdown to appear when a user hovers over a menu item:

<!DOCTYPE html>
<html>
<head>
<style>
  /* CSS for the dropdown container */
  .dropdown {
    position: relative;
    display: inline-block;
  }

  /* CSS for the trigger button or link */
  .dropdown-trigger {
    background-color: #3498db;
    color: white;
    padding: 10px;
    cursor: pointer;
  }

  /* CSS for the dropdown content */
  .dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;
  }

  /* CSS for dropdown options */
  .dropdown-option {
    padding: 12px 16px;
    display: block;
    color: #333;
    text-decoration: none;
  }

  /* Show the dropdown content when hovering over the trigger */
  .dropdown:hover .dropdown-content {
    display: block;
  }
</style>
</head>
<body>

<div class="dropdown">
  <div class="dropdown-trigger">Dropdown</div>
  <div class="dropdown-content">
    <a class="dropdown-option" href="#">Option 1</a>
    <a class="dropdown-option" href="#">Option 2</a>
    <a class="dropdown-option" href="#">Option 3</a>
  </div>
</div>
</body>
</html>Code language: HTML, XML (xml)

Output :

In this example, we have a simple dropdown menu. The trigger element with the class “dropdown-trigger” is styled like a button. When you hover over it, the content inside “dropdown-content” becomes visible, showing a list of options. Users can then click on one of the options to make a selection.

CSS Clickable Dropdowns

Certainly! In CSS, “clickable dropdowns” are interactive elements commonly used in web design to create menus or submenus that appear when a user clicks on a specific trigger element. These dropdowns offer user-friendly navigation and provide access to additional options or content upon user interaction.

To implement clickable dropdowns in CSS, you usually employ a combination of HTML, CSS, and JavaScript. Here’s a basic example:

Imagine you have a navigation menu, and you want a dropdown to appear when a user clicks on a menu item:

<!DOCTYPE html>
<html>
<head>
<style>
  /* Style for the dropdown container */
  .dropdown {
    position: relative;
    display: inline-block;
  }

  /* Style for the trigger element (menu item) */
  .menu-item {
    padding: 10px;
    cursor: pointer;
    background-color: blue;
    color: white;
  }

  /* Style for the dropdown content */
  .dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;
  }

  /* Style for dropdown options */
  .dropdown-option {
    padding: 12px 16px;
    display: block;
    color: #333;
    text-decoration: none;
  }
</style>
</head>
<body>

<div class="dropdown">
  <div class="menu-item" onclick="toggleDropdown()">Click Here</div>
  <div class="dropdown-content" id="dropdown">
    <a class="dropdown-option" href="#">Option 1</a>
    <a class="dropdown-option" href="#">Option 2</a>
    <a class="dropdown-option" href="#">Option 3</a>
  </div>
</div>

<script>
  function toggleDropdown() {
    var dropdown = document.getElementById("dropdown");
    if (dropdown.style.display === "block") {
      dropdown.style.display = "none";
    } else {
      dropdown.style.display = "block";
    }
  }
</script>

</body>
</html>
Code language: HTML, XML (xml)

Output :

In this example, the dropdown content is initially hidden, and it becomes visible when the user clicks on “Click Here.” This behavior is achieved through JavaScript that toggles the display of the dropdown content.

Clickable dropdowns are a practical UI pattern that offers a clear and user-friendly way to present additional choices or information to users when they interact with specific elements on a webpage. They can improve the overall user experience by providing organized access to content without overwhelming the screen.

Dropdowns are a practical and user-friendly way to manage complex navigation or input choices on a webpage, enhancing the overall user experience.

Khan Waseem is a tech lead and full-stack engineer in Bengaluru, building hospitality and SaaS software with Node.js, React and React Native. See the resume