<!--
Forum question answer only:

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

-->

<ul class="dropdown nav">
  <li>
    <a href="#" class="dropbtn">Dropdown 1</a>
    <ul class="dropdown-content">
      <!-- target="_blank" is just for testing on codepen -->
      <li><a target="_blank" href="http://www.google.com">Link 1a</a></li>
      <li><a href="#">Link 2a</a></li>
      <li><a href="#">Link 3a</a></li>
    </ul>
  </li>
  <li>
    <a href="#" class="dropbtn">Dropdown 2</a>
    <ul class="dropdown-content">
      <li><a target="_blank" href="http://www.google.com">Link 1b</a></li>
      <li><a href="#">Link 2b</a></li>
      <li><a href="#">Link 3b</a></li>
    </ul>
  </li>
  <li>
    <a href="#" class="dropbtn">Dropdown 3</a>
    <ul class="dropdown-content">
      <li><a target="_blank" href="http://www.google.com">Link 1c</a></li>
      <li><a href="#">Link 2c</a></li>
      <li><a href="#">Link 3c</a></li>
    </ul>
  </li>
</ul>

/* The container <div> - needed to position the dropdown content */
.dropdown {
  position: relative;
  z-index: 99;
  display: flex;
  background: blue;
}
.dropdown,
.dropdown ul {
  margin: 0;
  padding: 0;
  list-style: none;
}
.dropdown li {
  position: relative;
  display: flex;
  flex-direction: column;
}
/* Dropdown Button */
.dropbtn {
  background-color: #3498db;
  color: white;
  padding: 1rem;
  font-size: 1rem;
  position: relative;
  z-index: 2;
  text-decoration: none;
  border-right: 1px solid #fff;
}

/* 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%;
  z-index: 1;
  opacity: 1;
  transition: top 0.5s ease-in-out, opacity 0.5s ease-in-out, left 0s;
}

/* lets add some arrows*/
/* arrows */
.nav li a:first-child:not(:last-child) {
  padding-right: 2rem; /* make space for arrows*/
}
.nav li a:first-child:not(:last-child):after {
  content: "";
  position: absolute;
  right: 0.5rem;
  top: 50%;
  margin-top: -3px;
  width: 0;
  height: 0;
  border-left: 6px solid transparent;
  border-right: 6px solid transparent;
  border-top: 6px solid yellow;
  transition: 0.3s ease;
}
.nav a:first-child:not(:last-child):focus:after {
  border-top: 6px solid red;
}

/* need to implement a hamburger here to show this display*/
@media screen and (max-width: 800px) {
  .dropdown {
    display: block;
  }
  .dropbtn {
    border: none;
    border-bottom: 1px solid #fff;
    z-index: 0;
  }
  .dropdown-content {
    min-width: 100%;
  }
}
/*
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;
    event.preventDefault()
    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.