There are numerous techniques to design a dropdown menu.
I prefer utilizing a mostly CSS/HTML approach for this task.
Take into account experimenting with something similar to the following:
/* CSS */
ul.navigation > li{
display:inline-block;
position:relative;
list-style:none;
margin:0px;
}
ul.navigation > li > ul{
position:absolute;
background:lightgray;
display:none;
left:0px;
top:20px;
}
ul.navigation > li > a:focus + ul{
display:block;
}
<!-- HTML -->
<ul class="navigation">
<li><a href="#">About</a></li>
<li><a href="#">Services</a></li>
<li>
<a href="javascript:void(0)">Products</a>
<ul>
<li>Bottles</li>
<li>Cans</li>
<li>Cups</li>
</ul>
</li>
</ul>
To start, ensure that the navigation link intended as a toggle instead of a regular hyperlink has a href="javascript:void(0)"
. This will prevent the anchor tag from navigating away from the page by default.
Subsequently, conceal the submenus by applying a display:none
to the nested ul.
Finally, in order to unveil our submenu, users must focus on a specific navigation's anchor tag, triggering the display property of the submenu to change to block.
For an example, refer to:
http://codepen.io/eoghanTadhg/pen/ZWxXEb