I have a dropdown menu that looks like this:
.dropdown { display: inline-block; position: relative; } .dropdown button > a { display: block; color: #000000; text-decoration: none; } .dropdown-content { display: none; position: absolute; width: auto; overflow: auto; box-shadow: 0px 10px 10px 0px rgba(0,0,0,0.4); } .dropdown:hover .dropdown-content { display: block; } .dropdown-content a { display: block; color: #000000; background-color: #e9e9ed; padding: 5px; text-decoration: none; } .dropdown-content a:hover { color: #FFFFFF; background-color: #0080bf; }
<DIV class="dropdown"><BUTTON>Menu</BUTTON><DIV class="dropdown-content"> <A href="/1.php">Option 1</A> <A href="/2.php">Option 2</A> <A href="/3.php">Option 3</A> <DIV class="dropdown"><BUTTON><A href="/4.php">Option 4</A></BUTTON><DIV class="dropdown-content"> <A href="/4-1.php">Option 4-1</A> <A href="/4-2.php">Option 4-2</A> <A href="/4-3.php">Option 4-3</A> </DIV></DIV> </DIV></DIV>But the second level menu doesn't work (press "Run Code Snippet" to see it). I need it to appear on the right side of the first level, and the width of each level must be elastic (increase and decrease automatically based on the content). Additionally, the width of the root button must not be bound to the width of the first level. Ideally any level should have a common style, but it doesn't matter since I don't have dozens of levels. Is there any solution without rewriting all the code?
You can do the following:
Remove
overflow: auto;
from.dropdown-content
so that overflowed sublevels are visible. Add the>
selector in the.dropdown:hover .dropdown-content
line so that the direct children will be shown on hover. The last thing is to add the sublevel style to show it in the top right corner.dropdown-content .dropdown-content { left: 100%; top: 0; }