<!--
Forum question answer only:

https://www.sitepoint.com/community/t/dropdown-question-follow-up/387208/2

-->
<div class="dropdown">
  <div>
    <button class="dropbtn">Dropdown 1</button>
    <div class="dropdown-content">
      <a target="_blank" href="http://www.google.com">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
  </div>
  <div>
    <button class="dropbtn">Dropdown 2</button>
    <div class="dropdown-content">
      <a target="_blank" href="http://www.google.com">Link 1b</a>
      <a href="#">Link 2b</a>
      <a href="#">Link 3b</a>
    </div>
  </div>
   <div>
    <button class="dropbtn">Dropdown 3</button>
    <div class="dropdown-content">
      <a target="_blank" href="http://www.google.com">Link 1c</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3c</a>
    </div>
  </div>
</div>
/* The container <div> - needed to position the dropdown content */
.dropdown {
  position: relative;
  z-index: 99;
  display: flex;
  background: blue;
}
.dropdown > div {
  position: relative;
}
/* Dropdown Button */
.dropbtn {
  -moz-appearance: none;
  -webkit-appearance: none;
  appearance: none;
  background-color: #3498db;
  color: white;
  padding: 1rem;
  font-size: 1rem;
  border: none;
  cursor: pointer;
  position: relative;
  z-index: 2;
}

/* Dropdown button on hover & focus */
.dropbtn:hover,
.dropbtn:focus {
  background-color: #2980b9;
}

/* Dropdown Content (Hidden by Default) */
.dropdown-content {
  /* Hide offscreen as display:none is not that friendly and makes animatione awkward */
  position: absolute;
  top: 0;
  /*left:auto;*/
  left: -200vw;
  background-color: #f1f1f1;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
  opacity: 0;
  transition: top 0.5s ease-in-out, opacity 0.5s ease-in-out, left 0s 5s;
}

/* Links inside the dropdown */
.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}

/* Change color of dropdown links on hover */
.dropdown-content a:hover {
  background-color: #ddd;
}

/* Show the dropdown menu (use JS to add this class to the .dropdown-content container when the user clicks on the dropdown button) */
.show {
  left: 0;
  top: 100%;
  opacity: 1;
  transition: top 0.5s ease-in-out, opacity 0.5s ease-in-out, left 0s;
}
/*
Forum question answer only:
https://www.sitepoint.com/community/t/dropdown-question-follow-up/387208/2
*/

(function (d) {
  "use strict";

  const dropdowns = d.querySelectorAll(".dropdown-content");
  const btns = d.querySelectorAll(".dropbtn");

  btns.forEach((btn) => {
    btn.addEventListener("click", showDropDown);
  });

  function showDropDown(event) {
    var thisDrop = event.target.nextElementSibling;

    if (thisDrop.classList.contains("show")) {
      thisDrop.classList.remove("show");
    } else {
      closeDropDown();
      thisDrop.classList.toggle("show");
    }
  }
  
  function closeDropDown() {
    dropdowns.forEach((drop) => {
      drop.classList.remove("show");
    });
  }
  
  // Close the dropdown menu if the user clicks outside of it
  window.onclick = function (event) {
    if (!event.target.matches(".dropbtn")) {
      closeDropDown();
    }
  };
  
})(document);
Run Pen

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.