I have the following code, I’m stuck on how to move the mouse from the menu to the submenu (the submenu always disappears when I tried doing so).
I did not use <ul>
and <li>
in my code and not sure if some little modify can solve this problem… I feel like I did not figure out the relationship between the parent and the child (menu), but I have no clue about how to deal with it. Thanks a lot!
.dropdown .dropbtn {
border: none;
outline: none;
color: #0f4391;
padding-top: 8.5px;
padding-right: 15px;
padding-bottom: 8.5px;
padding-left: 15px;
background-color: inherit;
font-family: inherit;
}
.dropdown-content,
.sub-1 {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}
.dropdown-content a,
.sub-1 a {
float: none;
color: #0f4391;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropbtn:hover {
background-color: lightgray;
color: black;
}
.dropdown:hover .dropdown-content {
display: block;
}
/* To let css detect next element of hovered element to take action */
.dropdown .dropdown-content a:nth-child(1):hover + .sub-1 {
background-color: hotpink;
display: block;
margin-left: 10em;
margin-top : -2.5em;
}
<div class="dropdown" id="myDropdown">
<!-- -->
<button class="dropbtn">
<i style="font-size: 24px" class="fa"></i> level_0
<i class="fa fa-caret-down"></i>
</button>
<!-- -->
<div class="dropdown-content">
<!-- Logic : div after element (to be hovered) to detect for action-->
<a href="#">level_1</a>
<div class="sub-1">
<a href="#">level_1-1</a>
<a href="#">level_1-2</a>
<a href="#">level_1-3</a>
</div>
<!-- -->
<a href="#">level_2</a>
<a href="#">level_3</a>
</div>
</div>
Based on my code, how to modify CSS to solve the problem (moving from menu to submenu to sub submenu. I just updated my question to make it clear.
2
Answers
Okay, I think I finally solved the problem by figuring out the relationships between parent and child div when hovering. Also, it seems like the part of menu and submenu should be overlapped (at least no gap).
Here is the updated css and html. Thanks!
Ok, the problem is the following, you are showing the submenu only when the parent menu is being hovered and when you move from the parent menu to the submenu, the parent menu is no longer being hovered and the submenu disappears.
try with this CSS
and modify your HTML div nest with this code: