JSFiddle - React, Tailwind, and code Playground

by robert_schutt

HTML

<div class="socidropdown">
  <button onclick="myFunc()" class="socidropbtn">Dropdown</button>
  <div id="mysociDropdown" class="socidropdown-content">
    <a href="one.php">one</a><hr>
    <a href="two.php">two</a>
    <a href="three.php">three</a>
  </div>
</div>

CSS

.socidropbtn {
  border-radius: 4px;
  background-color: #008781;
  color: black;
  margin-top: -9.5px;
  padding-top: 10px;
  padding-bottom: 14px;
  padding-left: 6px;
  padding-right: 6px;
  font-size: 13px;
  border: none;
  cursor: pointer;
  font-family: helvetica, arial, sans-serif;
  font-weight: bold;
  -webkit-transition-duration: 0.3s;
  transition-duration: 0.3s;
}
/* Dropdown button on hover & focus */

.socidropbtn:hover,
.socidropbtn:focus {
  background-color: #006661;
}
/* The container <div> - needed to position the dropdown content */

.socidropdown {
  padding-bottom: -5px;
  position: relative;
  display: inline-block;
  text-align: center;
}
/* Dropdown Content (Hidden by Default) */

.socidropdown-content {
  display: none;
  position: absolute;
  margin-bottom: -10px;
  background-color: #d9d9d9;
  min-width: 130px;
}
/* Links inside the dropdown */

.socidropdown-content a {
  color: black;
  padding-bottom: 0px;
  margin-bottom: 0px;
  /*need to declare padding prior to padding-bottom*/
  text-decoration: none;
  display: block;
}
/* Change color of dropdown links on hover */

.socidropdown-content a:hover {
  background-color: #006661
}
/* Show the dropdown menu (use JS to add this class to the .dropdown-content container when the user clicks on the dropdown button) */

.show {
  display: block;
}

JavaScript

function myFunc() {
    document.getElementById("mysociDropdown").classList.toggle("show");
}

// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.socidropbtn')) {

    var i, socidropdowns = document.getElementsByClassName("socidropdown-content");
    for (i = 0; i < socidropdowns.length; i++) {
      var opensociDropdown = socidropdowns[i];
      if (opensociDropdown.classList.contains('show')) {
        opensociDropdown.classList.remove('show');
      }
    }
  }
}