Is it possible to achieve a dropdown menu using only CSS without the need for JavaScript? I am trying to display a submenu when hovering over the top-level menu item, but the submenu disappears as soon as I unhover. Is there a CSS-only solution to keep the submenu visible?
I attempted to use
.has-children:hover + .sub-menu {display: block;}
to show the submenu on top-level menu hover, but I am unsure how to make the submenu remain visible.
The HTML code is:
.header ul {
list-style: none;
padding: 20px 30px;
}
.header li {
float: left;
position: relative;
}
.header li a {
padding: 20px 30px;
}
.menu {
clear: none;
float: right;
max-height: none;
}
.menu li ul {
position: absolute;
}
.sub-menu {
display: none;
}
.open-menu-link {
display: none;
}
.has-children:hover + .sub-menu {
display: block;
}
<header class="header">
<ul class="menu">
<li><a href="#work">Work</a></li>
<li>
<a class="has-children" href="#about">Haschildren</a>
<ul class="sub-menu">
<li><a href="#link1">Child 1</a></li>
<li><a href="#link2">Child 2</a></li>
<li><a href="#link3">Child 3</a></li>
</ul>
</li>
<li><a href="#careers">Careers</a></li>
<li>
<a class="has-children" href="#careers">About</a>
<ul class="sub-menu">
<li><a href="#link1">Child 3</a></li>
<li><a href="#link2">Child 4</a></li>
</ul>
</li>
<li><a href="#">end</a></li>
</ul>
</header>