Multi Level Menu That Works on Every Screen
In this article
This guide shows you how to build a multi-level (dropdown) navigation menu that functions reliably on phones, tablets, and desktops. The core challenge is that hover-based menus fail on touch devices, so we'll use a combination of CSS and a small amount of JavaScript to create a menu that works with both mouse and touch input. You'll end up with a menu that's accessible, responsive, and doesn't require a framework.
What You'll Need
- Basic knowledge of HTML, CSS, and JavaScript
- A text editor (like VS Code, Sublime Text, or Notepad++)
- A web browser for testing (Chrome, Firefox, Safari, Edge)
- Optional: a mobile device or browser developer tools to simulate touch screens
Step-by-Step
- Write semantic HTML structure. Use nested
<ul>elements inside<li>items to create the hierarchy. Give the top-level menu a class likemenuand each dropdown container a class likesubmenu. Include a button (or a link with a class) inside each parent<li>to act as the toggle for touch devices. For example:<nav> <ul class="menu"> <li> <button class="menu-toggle" aria-expanded="false">Products</button> <ul class="submenu"> <li><a href="#">Sub Item 1</a></li> <li> <button class="menu-toggle" aria-expanded="false">Deeper</button> <ul class="submenu"> <li><a href="#">Deep Item</a></li> </ul> </li> </ul> </li> <li><a href="#">About</a></li> </ul> </nav> - Style the desktop menu with CSS. Hide submenus by default using
display: none. On screens wider than a breakpoint (e.g., 768px), display them on hover of the parent<li>. Use:hoverand:focus-withinto make it keyboard accessible. Example:.submenu { display: none; } @media (min-width: 768px) { .menu > li:hover > .submenu, .menu > li:focus-within > .submenu { display: block; } } - Add JavaScript for touch devices. Write a script that toggles the
submenuvisibility when a.menu-togglebutton is clicked. Also close other open menus when a new one is opened. Use event delegation to handle dynamically added elements if needed. Example:document.querySelectorAll('.menu-toggle').forEach(btn => { btn.addEventListener('click', function(e) { e.stopPropagation(); const submenu = this.nextElementSibling; const isOpen = submenu.classList.contains('open'); // Close all submenus document.querySelectorAll('.submenu.open').forEach(s => { s.classList.remove('open'); s.previousElementSibling.setAttribute('aria-expanded', 'false'); }); if (!isOpen) { submenu.classList.add('open'); this.setAttribute('aria-expanded', 'true'); } }); }); - Make submenus visible on mobile. In your CSS, for screens below the breakpoint, change the submenu display to
noneby default, and use a class like.opento show it. This overrides the hover rule. Example:@media (max-width: 767px) { .submenu { display: none; } .submenu.open { display: block; } } - Close menus on outside click. Add a listener on the document that closes all open submenus when clicking anywhere outside the menu. This prevents sticky menus.
document.addEventListener('click', function(e) { if (!e.target.closest('.menu')) { document.querySelectorAll('.submenu.open').forEach(s => { s.classList.remove('open'); s.previousElementSibling.setAttribute('aria-expanded', 'false'); }); } }); - Ensure keyboard accessibility. Make sure that the toggle buttons are focusable and that pressing Enter or Space toggles the submenu (which is native for buttons). Also, ensure that all links are reachable via Tab. Use
aria-expandedto indicate state to screen readers. - Test on multiple devices. Use browser developer tools to simulate different screen sizes and touch events. Test on actual mobile devices if possible. Check that the menu opens on tap, closes on outside tap, and that hover works on desktop.
Common Mistakes
- Using only
:hoverwithout:focus-within– this makes the menu inaccessible to keyboard users. - Not preventing the click event from bubbling up, which can cause the menu to close immediately after opening.
- Forgetting to close other open submenus when opening a new one, leading to multiple open menus.
- Using
display: noneon mobile but not having a way to show it (like the.openclass). - Not setting
aria-expandedcorrectly, which confuses screen reader users.
Tips
- Use a CSS breakpoint that matches your design, but 768px is a common starting point.
- Consider using a small delay on hover (e.g., 300ms) to prevent accidental opening when moving the mouse across the menu bar.
- For very deep menus, consider using an accordion style on mobile rather than a flyout, as it's easier to use.
- Use
aria-haspopup="true"on toggle buttons to indicate that they have a popup. - Test with a screen reader to ensure the menu is announced correctly.
Troubleshooting
- Menu doesn't open on touch: Check that the JavaScript is loaded and that the click event is attached. Also verify that the submenu is not hidden by a CSS rule that has higher specificity.
- Menu opens but closes immediately: This is usually due to the click event bubbling up to the document listener. Make sure to call
e.stopPropagation()in the toggle handler. - Hover works on desktop but not on touch: That's expected. Ensure that the touch fallback (the click handler) is working and that the submenu is visible when the
.openclass is added. - Menu items not clickable on mobile: Sometimes the submenu might be overlapped by another element. Check the z-index of the submenu and ensure it's above other content.
- Keyboard navigation skips submenu items: Make sure the submenu is not
display: nonewhen it should be visible. For keyboard users, use:focus-withinto show the submenu on focus.
Frequently Asked Questions
Can I do this without JavaScript?
Yes, you can use the :focus-within CSS pseudo-class to show submenus when a parent is focused, which works for keyboard navigation. However, for touch devices, you'll need JavaScript to toggle on tap, because :hover doesn't work reliably on touch.
How do I make the menu close when I tap outside?
Add a document-level click listener that removes the .open class from all submenus when the click target is not inside the menu. This is shown in Step 5.
What is the best breakpoint for mobile vs desktop?
It depends on your design, but 768px (tablet portrait) is a common choice. You can also use 992px if you want the mobile menu to appear on tablets as well.
How do I handle multiple levels of submenus on mobile?
You can use the same pattern recursively. Each submenu is a <ul> with its own toggle button. The JavaScript should work for all levels because it uses querySelectorAll.
Is it better to use a CSS-only approach or JavaScript?
CSS-only is lighter, but it's limited for touch devices and complex interactions. JavaScript gives you more control and better accessibility. A hybrid approach (CSS for desktop, JS for touch) is often best.
How do I make the menu accessible for screen readers?
Use proper ARIA attributes: aria-expanded on toggle buttons, aria-haspopup="true" on buttons that open submenus, and ensure that submenus are not hidden from the accessibility tree when closed (use visibility: hidden instead of display: none for screen readers, or use hidden attribute).