The trick is the pseudo-class :hover .
<ul class="menu"> <li> <a href="...">Menu Item 1</a> <ul class="submenu"> <li><a href="...">Submenu 1</a></li> <li><a href="...">Submenu 2</a></li> </ul> </li> <li> <a href="...">Menu Item 2</a> <ul class="submenu"> <li><a href="...">Submenu 3</a></li> <li><a href="...">Submenu 4</a></li> </ul> </li> </ul>
Ok Thus, your entire submenu should go inside the <li> element of the main menu to which it corresponds. Then for CSS:
.submenu { display: none; } .menu>li:hover>.submenu { display: block; }
Do some styling and get the job done.
Edit: For a different menu level, this is very simple. Use this CSS:
.menu li>ul { display: none; } .menu li:hover>ul { display: block; }
Note that I replaced .menu>li:hover with .menu li:hover . This tells the browser that all li elements are under the main menu (and not just the descendants themselves) and show their submenu when they hang. I also got rid of using the submenu class because it is not needed if you base CSS on descendants. This will allow you to add as many levels as you want.
source share