'Drop-down menu using button, hovering over one button displays blocks from all buttons
I'm trying to create a menu where there are 3 buttons horizontally and when hovering over each button, it should show the block listing the various sub-menu option under that button. The only problem I am facing with the below code is:
- When I hover over the last button in the row, then it shows the drop-down submenu options from the "all the three" button.
- When I hover on the first button it correctly shows the sub-menu of only that button, when when I transition my hover to the second button in the row, then the sub-menu displayed for the first button doesn't close itself (instead now both buttons are showing the sub-menu drop-down expanded).
Here's the simple code I have:
<style type="text/css">
.dropbtn-01 {
background-color: transparent;
color: yellow;
padding: 3px;
font-size: 12px;
}
.dropdown-01 {
display: inline-block;
position: relative
}
.dropdown-content-01 {
position: absolute;
background-color: lightgrey;
min-width: 10px;
display: none;
z-index: 1;
}
.dropdown-content-01 a {
color: black;
padding: 6px 8px;
text-decoration: none;
display: block;
}
.dropdown-content-01 a:hover {
background-color: orange;
}
.dropdown-01:hover .dropdown-content-01 {
display: block;
}
.dropdown-01:hover .dropbtn-01 {
background-color: grey;
}
.dropbtn-02 {
background-color: transparent;
color: yellow;
padding: 4px;
font-size: 10px;
}
.dropdown-02 {
display: inline-block;
position: relative
}
.dropdown-content-02 {
position: absolute;
background-color: lightgrey;
min-width: 30px;
display: none;
z-index: 2;
}
.dropdown-content-02 a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown-content-02 a:hover {
background-color: orange;
}
.dropdown-02:hover .dropdown-content-02 {
display: block;
}
.dropdown-02:hover .dropbtn-02 {
background-color: grey;
}
.dropbtn-03 {
background-color: transparent;
color: yellow;
padding: 7px;
font-size: 12px;
}
.dropdown-03 {
display: inline-block;
position: relative
}
.dropdown-content-03 {
position: absolute;
background-color: lightgrey;
min-width: 30px;
display: none;
z-index: 3;
}
.dropdown-content-03 a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown-content-03 a:hover {
background-color: orange;
}
.dropdown-03:hover .dropdown-content-03 {
display: block;
}
.dropdown-03:hover .dropbtn-03 {
background-color: grey;
}
</style>
<div class="dropdown-01"><span><button class="dropbtn-01">Main</button> </span>
<div class="dropdown-content-01"><span><a href="Link-1">Home</a> <a href="Link-2">The Mama</a> < </span></div>
<span style="display:inline-block; width: 60px;"></span>
<div class="dropdown-02"><span><button class="dropbtn-02">About</button> </span>
<div class="dropdown-content-02"><span><a href="Link-3">Credentials</a> <a href="Link-4">Aboutx</a> < </span></div>
<span style="display:inline-block; width: 60px;"></span>
<div class="dropdown-03"><span><button class="dropbtn-03">Contact</button> </span>
<div class="dropdown-content-03"><span><a href="Link-5">Contact Us</a> < </span></div>
Solution 1:[1]
You have no closing tags for some of your div containers. e.g. You didn't close your first div container with the class dropdown-01. I've cleaned up your code and removed unnecessary tags. See the snippet below:
.wrapper {
display: flex;
justify-content: space-around;
}
.dropbtn-01 {
background-color: transparent;
color: yellow;
padding: 3px;
font-size: 12px;
}
.dropdown-01 {
display: inline-block;
position: relative
}
.dropdown-content-01 {
position: absolute;
background-color: lightgrey;
min-width: 10px;
display: none;
z-index: 1;
}
.dropdown-content-01 a {
color: black;
padding: 6px 8px;
text-decoration: none;
display: block;
}
.dropdown-content-01 a:hover {
background-color: orange;
}
.dropdown-01:hover .dropdown-content-01 {
display: block;
}
.dropdown-01:hover .dropbtn-01 {
background-color: grey;
}
.dropbtn-02 {
background-color: transparent;
color: yellow;
padding: 4px;
font-size: 10px;
}
.dropdown-02 {
display: inline-block;
position: relative
}
.dropdown-content-02 {
position: absolute;
background-color: lightgrey;
min-width: 30px;
display: none;
z-index: 2;
}
.dropdown-content-02 a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown-content-02 a:hover {
background-color: orange;
}
.dropdown-02:hover .dropdown-content-02 {
display: block;
}
.dropdown-02:hover .dropbtn-02 {
background-color: grey;
}
.dropbtn-03 {
background-color: transparent;
color: yellow;
padding: 7px;
font-size: 12px;
}
.dropdown-03 {
display: inline-block;
position: relative
}
.dropdown-content-03 {
position: absolute;
background-color: lightgrey;
min-width: 30px;
display: none;
z-index: 3;
}
.dropdown-content-03 a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown-content-03 a:hover {
background-color: orange;
}
.dropdown-03:hover .dropdown-content-03 {
display: block;
}
.dropdown-03:hover .dropbtn-03 {
background-color: grey;
}
<div class="wrapper">
<div class="dropdown-01">
<button class="dropbtn-01">Main</button>
<div class="dropdown-content-01"><a href="Link-1">Home</a> <a href="Link-2">The Mama</a> <
</div>
</div>
<div class="dropdown-02">
<button class="dropbtn-02">About</button>
<div class="dropdown-content-02"><a href="Link-3">Credentials</a> <a href="Link-4">Aboutx</a> <
</div>
</div>
<div class="dropdown-03">
<button class="dropbtn-03">Contact</button>
<div class="dropdown-content-03"><a href="Link-5">Contact Us</a> <
</div>
</div>
</div>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Yong |
