Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to avoid the dropdown from disappearing when removing cursor from the button?

When I hover over the button I see the dropdown menu however, when I navigate to any of the item on the menu the dropdown menu just disappears. I think I know the problem is that I have hover added on the button only, but how can I make it work like a normal dropdown ?

.dropdown {
  display: inline-block;
  padding: 5px;
  /* background:#f1f1f1; */
}

.dropbtn {
  padding: 5px;
  margin-bottom: 0px;
  width: 100%;
}

.dropdown-content {
  display: none;
  list-style: none;
  background: #f1f1f1;
  position: relative;
  top: 0px;
  left: 0px;
  padding: 0px 0px 0px 0px;
  margin: 0px;
  text-align: center;
  z-index: 1;
  width: 100%;
}

.dropdown-content li {
  margin-bottom: 0px;
  padding: 3px;
}

.dropdown-content li a {
  text-decoration: none;
  color: black;
  padding: 3px;
}

.dropdown-content li:hover,
.dropdown-content li a:hover {
  text-decoration: none;
  color: white;
  background-color: #333;
}

.dropbtn:hover+.dropdown-content {
  display: block;
}
<div class="dropdown">
  <button class="dropbtn">More</button>
  <ul class="dropdown-content">
    <li><a href="#">Home</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Contact</a></li>
  </ul>
</div>

I want the dropdown menu to stay visible while I am hovering over the child elements in the dropdown menu as any other dropdown menu works.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You could set the :hover property directly on the .dropdown element like this :

.dropdown {
  display: inline-block;
  padding: 5px;
  /*background:#f1f1f1; */
}

.dropbtn {
  padding: 5px;
  margin-bottom: 0px;
  width: 100%;
}

.dropdown-content {
  display: none;
  list-style: none;
  background: #f1f1f1;
  position: relative;
  top: 0px;
  left: 0px;
  padding: 0px 0px 0px 0px;
  margin: 0px;
  text-align: center;
  z-index: 1;
  width: 100%;
}

.dropdown-content li {
  margin-bottom: 0px;
  padding: 3px;
}

.dropdown-content li a {
  text-decoration: none;
  color: black;
  padding: 3px;
}

.dropdown-content li:hover,
.dropdown-content li a:hover {
  text-decoration: none;
  color: white;
  background-color: #333;
}

.dropdown:hover .dropdown-content {
  display: block;
}
<div class="dropdown">
  <button class="dropbtn">More</button>
  <ul class="dropdown-content">
    <li><a href="#">Home</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Contact</a></li>
  </ul>
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading