So basically I have a menu that expands when you hover over it. How can I rewrite my code so that it also expands on click (expand on click, collapse on click again)? You can use JavaScript if necessary, but I've been trying to do everything with just HTML and CSS.
html, body { font-family: Arial, Helvetica, sans-serif; } .navigation { width: 300px; } .mainmenu, .submenu { list-style: none; padding: 0; margin: 0; } .mainmenu a { display: block; background-color: #CCC; text-decoration: none; padding: 10px; color: #000; } .mainmenu a:hover { background-color: #C5C5C5; } .mainmenu li:hover .submenu { display: block; max-height: 200px; } .submenu a { background-color: #999; } .submenu a:hover { background-color: #666; } .submenu { overflow: hidden; max-height: 0; -webkit-transition: all 0.5s ease-out; }
<nav class="navigation"> <ul class="mainmenu"> <li><a href="">产品</a> <ul class="submenu"> <li><a href="">上装</a></li> <li><a href="">下装</a></li> <li><a href="">鞋类</a></li> </ul> </li> <li><a href="">其他</a> <ul class="submenu"> <li><a href="">联系我们</a></li> <li><a href="">关于我们</a></li> <li><a href="">常见问题</a></li> </ul> </li> </ul> </nav>
I read the details of your question carefully
You can try to fix the code as follows.