I am currently working on creating a dropdown menu that pops up when hovering over an item. However, I am facing two challenges:
- Once my mouse moves away from the item, the dropdown disappears. Therefore, I would like the dropdown to remain visible as long as I hover over either the item or the dropdownmenu.
- This issue seems to be related to the first one. The text of the item should stay green as long as I hover over it or the dropdown menu (currently it only changes color while hovering over the item).
Here is the code I have attempted so far:
#menu {
float: left;
}
#menu ul.top-menu {
list-style: none;
padding: 0;
}
#menu ul.top-menu li {
display: inline-block;
float: left;
margin: 0 5px;
position: relative;
font-family: 'Roboto', sans-serif;
font-size: 18px;
}
#menu ul.top-menu li a {
margin: 0;
padding: 0 5px;
font-weight: 400;
font-size: 18px;
color: black;
display: inline-block;
text-decoration: none !important;
outline: none;
}
#menu ul.top-menu li a:hover {
color: green;
}
#menu ul.top-menu li a:hover + div {
display: block;
}
#menu .sub-container {
display: none;
position: absolute;
top: 32px;
left: -20px;
width: 400px;
background: black;
border-top: 2px solid green;
z-index: 10;
}
#menu .sub-container ul.sub-menu {
padding: 10px;
overflow: hidden;
}
#menu .sub-container ul.sub-menu li {
width: 30%;
float: left;
margin: 0 1.65%;
}
#menu .sub-container ul.sub-menu li:hover {
background-color: green;
color:white;
}
#menu .sub-container ul.sub-menu li a {
color:white;
height: 30px;
line-height: 30px;
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
padding: 0 10px;
display: block;
border-bottom: none !important;
}
<div class="container">
<div id="menu">
<ul class="top-menu">
<li>
<a href="" onclick="return false">Sub Menu 1</a>
<div class='sub-container'>
<ul class='sub-menu'>
<li><a href='#'>Sub Option 1.1</a></li>
<li><a href='#'>Sub Option 1.2</a></li>
<li><a href='#'>Sub Option 1.3</a></li>
<li><a href='#'>Sub Option 1.4</a></li>
<li><a href='#'>Sub Option 1.5</a></li>
<li><a href='#'>Sub Option 1.6</a></li>
<li><a href='#'>Sub Option 1.7</a></li>
<li><a href='#'>Sub Option 1.8</a></li>
<li><a href='#'>Sub Option 1.9</a></li>
</ul>
</div>
</li>
<li>
<a href="" onclick="return false">Sub Menu 2</a>
<div class='sub-container'>
<ul class='sub-menu'>
<li><a href='#'>Sub Option 2.1</a></li>
<li><a href='#'>Sub Option 2.2</a></li>
<li><a href='#'>Sub Option 2.3</a></li>
<li><a href='#'>Sub Option 2.4</a></li>
<li><a href='#'>Sub Option 2.5</a></li>
<li><a href='#'>Sub Option 2.6</a></li>
<li><a href='#'>Sub Option 2.7</a></li>
<li><a href='#'>Sub Option 2.8</a></li>
<li><a href='#'>Sub Option 2.9</a></li>
</ul>
</div>
</li>
<li>
<a href="" onclick="return false">No Sub Menu 1</a>
</li>
<li>
<a href="" onclick="return false">No Sub Menu 2</a>
</li>
</ul>
</div>
</div>
Any assistance would be greatly appreciated! Thank you in advance.