Keyboard-accessible mega menu navigation

Description:

Mega menus are complex, multi-level menus often used for large websites with extensive navigation options. Making a mega menu keyboard-accessible is essential for users who rely on keyboard navigation, including people with disabilities who use screen readers or other assistive technology.

A keyboard-accessible mega menu should:

  1. Allow users to navigate between menu items using the keyboard.
  2. Follow a logical, predictable tab order.
  3. Use ARIA roles and attributes to provide context for screen readers.
  4. Allow users to open and close submenus with the keyboard.

Below is a basic example of a keyboard-accessible mega menu with recommended accessibility practices.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Accessible Mega Menu</title>
    <style>
        /* Basic styles for the mega menu */
        .menu, .submenu {
            list-style: none;
            padding: 0;
            margin: 0;
        }
        .menu > li {
            position: relative;
            display: inline-block;
            margin-right: 20px;
        }
        .submenu {
            display: none;
            position: absolute;
            top: 100%;
            left: 0;
            background-color: #fff;
            border: 1px solid #ddd;
            padding: 10px;
            width: 200px;
            z-index: 100;
        }
        /* Visible when submenu is opened */
        .submenu[aria-hidden="false"] {
            display: block;
        }
        /* Accessible focus styles */
        .menu-item:focus, .submenu-item:focus {
            outline: 2px solid #005fcc;
        }
    </style>
</head>
<body>
    <!-- Mega Menu Navigation -->
    <nav aria-label="Main Navigation">
        <ul class="menu" role="menubar">
            <li role="none">
                <button class="menu-item" aria-haspopup="true" aria-expanded="false" aria-controls="submenu1" id="menu1">
                    Products
                </button>
                <ul class="submenu" role="menu" id="submenu1" aria-labelledby="menu1" aria-hidden="true">
                    <li role="none"><a href="#" role="menuitem" class="submenu-item">Product 1</a></li>
                    <li role="none"><a href="#" role="menuitem" class="submenu-item">Product 2</a></li>
                    <li role="none"><a href="#" role="menuitem" class="submenu-item">Product 3</a></li>
                </ul>
            </li>
            <li role="none">
                <button class="menu-item" aria-haspopup="true" aria-expanded="false" aria-controls="submenu2" id="menu2">
                    Services
                </button>
                <ul class="submenu" role="menu" id="submenu2" aria-labelledby="menu2" aria-hidden="true">
                    <li role="none"><a href="#" role="menuitem" class="submenu-item">Service 1</a></li>
                    <li role="none"><a href="#" role="menuitem" class="submenu-item">Service 2</a></li>
                    <li role="none"><a href="#" role="menuitem" class="submenu-item">Service 3</a></li>
                </ul>
            </li>
            <li role="none">
                <button class="menu-item" aria-haspopup="true" aria-expanded="false" aria-controls="submenu3" id="menu3">
                    About
                </button>
                <ul class="submenu" role="menu" id="submenu3" aria-labelledby="menu3" aria-hidden="true">
                    <li role="none"><a href="#" role="menuitem" class="submenu-item">Our Story</a></li>
                    <li role="none"><a href="#" role="menuitem" class="submenu-item">Team</a></li>
                    <li role="none"><a href="#" role="menuitem" class="submenu-item">Careers</a></li>
                </ul>
            </li>
        </ul>
    </nav>
    <script>
        // Function to manage focus and expand/collapse states
        document.querySelectorAll('.menu-item').forEach(button => {
            button.addEventListener('click', function() {
                toggleSubmenu(this);
            });
            button.addEventListener('keydown', function(e) {
                if (e.key === 'ArrowDown') {
                    openSubmenu(this);
                    moveToFirstSubmenuItem(this);
                    e.preventDefault();
                } else if (e.key === 'Escape') {
                    closeSubmenu(this);
                }
            });
        });
        document.querySelectorAll('.submenu-item').forEach(item => {
            item.addEventListener('keydown', function(e) {
                if (e.key === 'ArrowUp' && this.parentElement.previousElementSibling) {
                    this.parentElement.previousElementSibling.querySelector('.submenu-item').focus();
                    e.preventDefault();
                } else if (e.key === 'ArrowDown' && this.parentElement.nextElementSibling) {
                    this.parentElement.nextElementSibling.querySelector('.submenu-item').focus();
                    e.preventDefault();
                } else if (e.key === 'Escape') {
                    closeSubmenu(this.closest('.submenu').previousElementSibling);
                    this.closest('.submenu').previousElementSibling.focus();
                    e.preventDefault();
                }
            });
        });
        function toggleSubmenu(button) {
            const expanded = button.getAttribute('aria-expanded') === 'true';
            setSubmenuState(button, !expanded);
        }
        function openSubmenu(button) {
            setSubmenuState(button, true);
        }
        function closeSubmenu(button) {
            setSubmenuState(button, false);
        }
        function setSubmenuState(button, isOpen) {
            button.setAttribute('aria-expanded', isOpen);
            document.getElementById(button.getAttribute('aria-controls')).setAttribute('aria-hidden', !isOpen);
        }
        function moveToFirstSubmenuItem(button) {
            const firstSubmenuItem = document.querySelector(`#${button.getAttribute('aria-controls')} .submenu-item`);
            if (firstSubmenuItem) firstSubmenuItem.focus();
        }
    </script>
</body>
</html>

Explanation of Accessibility Features #

  1. ARIA Roles and Attributes:
    • role="menubar" and role="menuitem" provide context for screen readers, identifying the navigation as a menu.
    • aria-haspopup="true" indicates that each top-level menu item has a submenu.
    • aria-expanded is dynamically set to true or false to indicate if the submenu is open or closed.
    • aria-controls links each button to its submenu, while aria-labelledby links each submenu back to its parent menu item.
    • aria-hidden="true" is used to hide submenus visually and for assistive technologies when closed.
  2. Keyboard Navigation Patterns:
    • Enter/Space: Opens/closes the submenu.
    • ArrowDown: Opens the submenu if closed, then moves focus to the first submenu item.
    • ArrowUp/ArrowDown (within submenu): Moves focus between submenu items.
    • Escape: Closes the submenu and returns focus to the parent menu button.
  3. Focus Management:
    • The focus is set to the first submenu item when the submenu is opened.
    • When Escape is pressed, the submenu closes and focus returns to the main menu button.

Bad Sample #

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Bad Navigation Menu Example</title>
    <style>
        /* Styles for menu items */
        .menu {
            list-style: none;
            display: flex;
            gap: 20px;
        }
        .submenu {
            display: none;
            position: absolute;
            top: 100%;
            background-color: #fff;
            list-style: none;
            padding: 10px;
            border: 1px solid #ccc;
        }
        /* Hover-only submenu display */
        .menu-item:hover .submenu {
            display: block;
        }
    </style>
</head>
<body>
    <nav>
        <ul class="menu">
            <li class="menu-item">
                <a href="#">Home</a>
            </li>
            <li class="menu-item">
                <a href="#">About</a>
                <ul class="submenu">
                    <li><a href="#">Our Team</a></li>
                    <li><a href="#">Our Story</a></li>
                </ul>
            </li>
            <li class="menu-item">
                <a href="#">Services</a>
                <ul class="submenu">
                    <li><a href="#">Consulting</a></li>
                    <li><a href="#">Development</a></li>
                </ul>
            </li>
            <li class="menu-item">
                <a href="#">Contact</a>
            </li>
        </ul>
    </nav>
</body>
</html>

Issues with This Navigation Menu #

  1. Hover-Only Submenu Visibility:
    • Submenus are displayed only when hovering over the top-level items. This makes it inaccessible for keyboard-only users who cannot trigger the hover state, as well as screen reader users who may not even know the submenu exists.
  2. No ARIA Roles or Attributes:
    • The menu lacks ARIA roles (role="navigation", role="menu", role="menuitem", etc.), which help screen readers understand the structure and purpose of the navigation elements.
    • There’s no aria-haspopup or aria-expanded attribute to indicate that a menu item has a submenu, leaving screen reader users unaware of additional content.
  3. Inconsistent Focus Management:
    • Focus does not automatically move to the submenu when it opens, nor is focus trapped within the submenu. Keyboard users can accidentally tab to elements outside the menu, which creates a confusing navigation experience.
  4. No Keyboard Interaction:
    • There’s no way for keyboard users to open and navigate through the submenu items. Standard keyboard interactions, such as using Enter or Space to open submenus and Arrow keys to navigate within them, are not implemented.
  5. Lack of Visible Focus Indicators:
    • The menu items lack visible focus styles, making it difficult for keyboard users to see where they are within the menu.

How to Fix These Issues #

To make this navigation menu accessible, consider the following adjustments:

  • Add ARIA roles and attributes to clarify the menu structure.
  • Implement keyboard navigation so users can open and close submenus with the Enter or Space keys and navigate through them with arrow keys.
  • Use aria-haspopup="true" and aria-expanded attributes to indicate submenus.
  • Ensure focus is managed properly so that keyboard users can navigate in a logical, predictable sequence.
  • Visible focus indicators should be added to highlight focused menu items for users relying on keyboard navigation.

Key Takeaways for Accessible Mega Menu Navigation

Generic Concepts:

  • Clear ARIA Roles and States: Use roles like menubar, menuitem, and aria-expanded attributes to help screen readers and assistive technologies understand the menu structure.
  • Keyboard-Friendly Navigation: Implement keyboard navigation with ArrowDown, ArrowUp, Escape, and Enter keys, following common keyboard interaction patterns for menus.
  • Focus Management: Ensure focus is directed appropriately within submenus

Creating an accessible mega menu requires considering platform-specific behavior and user expectations on Mac OS, iOS, and Windows using assistive tech like NVDA, JAWS and VoiceOver

  1. Keyboard Navigation Consistency:
    • Mac OS and Windows: Ensure keyboard users can navigate the menu using Tab to move between top-level items and Arrow keys for submenu navigation. Both Mac and Windows users expect consistent keyboard shortcuts, so these navigation patterns are standard and easy to follow.
    • iOS: While iOS devices rely primarily on touch, users who use Bluetooth keyboards will benefit from a similar tabbing and arrow key navigation pattern. Ensure elements are focusable and clearly defined so keyboard users can effectively navigate the menu.
  2. ARIA Roles and Attributes for Screen Readers:
    • Mac OS (VoiceOver) and iOS (VoiceOver): VoiceOver users expect clear navigation cues. Use ARIA attributes like aria-haspopup, aria-expanded, and aria-controls to indicate when a submenu opens or closes. Ensure each menu item and submenu is labeled for screen readers to give users proper context.
    • Windows (JAWS, NVDA): Windows screen reader users expect similar ARIA roles and alerts. Test with Windows screen readers to ensure that role="menubar" for the main menu, role="menuitem" for items, and aria-labelledby for submenus are announced correctly.
  3. Logical Focus Management:
    • All Platforms: When a submenu opens, move focus to the first item in the submenu. For iOS users who navigate with VoiceOver, ensure each submenu item is focusable, and clearly announce the navigation context.
    • Return Focus: Upon closing a submenu, return focus to the top-level menu item. This behavior is crucial across all platforms, allowing users to track their position within the menu structure.
  4. Touch Accessibility for iOS:
    • iOS: For touch screen users on iOS, ensure all interactive elements in the menu are large enough for easy tapping, with a minimum touch target size of 44×44 pixels. This size is in line with Apple’s Human Interface Guidelines, providing a comfortable touch experience.
    • Mac OS and Windows: While touch targets are less critical for these systems, it’s beneficial to follow similar sizing guidelines to support users on touch-enabled Windows devices or macOS devices with accessibility tools.
  5. Consistent Focus and Hover States:
    • All Platforms: Ensure clear focus indicators that align with both Mac OS and Windows keyboard navigation expectations. For example, Mac users are accustomed to visual focus rings, while Windows users often expect a high-contrast outline. These focus indicators should be consistent in color and thickness across the menu and should be visible on both hover and focus states.
  6. Platform-Specific Testing:
    • Test with VoiceOver (Mac OS and iOS) and JAWS or NVDA (Windows) to ensure that screen readers interact correctly with the menu. Testing across these platforms helps confirm that ARIA attributes, keyboard interactions, and focus management meet accessibility standards and function as expected for diverse user needs.

By following these key takeaways, you’ll create a mega menu that is accessible and intuitive across platforms, accommodating users with diverse needs and navigation methods.

What are your feelings

Updated on March 6, 2025