Accessible side menus for mobile navigation

Accessible Side Menus for Mobile Navigation #

Description:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Accessible Mobile Side Menu</title>
    <style>
        /* Basic styling for page */
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            overflow-x: hidden;
        }
        main {
            padding: 20px;
        }
        /* Toggle button for mobile menu */
        .menu-toggle {
            background-color: #333;
            color: #fff;
            border: none;
            padding: 10px 20px;
            font-size: 1em;
            cursor: pointer;
            position: relative;
            z-index: 2000;
        }
        /* Side menu styling */
        .side-menu {
            position: fixed;
            top: 0;
            left: -250px;
            width: 250px;
            height: 100%;
            background-color: #333;
            color: #fff;
            overflow-y: auto;
            padding: 20px;
            transition: left 0.3s ease;
            z-index: 1500;
        }
        /* Show the menu when it's open */
        .side-menu.open {
            left: 0;
        }
        .side-menu a {
            display: block;
            color: #fff;
            text-decoration: none;
            padding: 10px;
            margin-bottom: 10px;
            font-size: 1.2em;
        }
        .side-menu a:focus, .side-menu a:hover {
            background-color: #444;
            outline: 2px solid #fff;
        }
        /* Overlay for background dimming */
        .overlay {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            opacity: 0;
            visibility: hidden;
            transition: opacity 0.3s ease;
            z-index: 1000;
        }
        .overlay.show {
            opacity: 1;
            visibility: visible;
        }
    </style>
</head>
<body>
    <!-- Menu Toggle Button -->
    <button class="menu-toggle" aria-controls="side-menu" aria-expanded="false" id="menu-toggle">
        ☰ Menu
    </button>
    <!-- Side Menu Navigation -->
    <nav class="side-menu" id="side-menu" aria-labelledby="menu-toggle" role="dialog" aria-hidden="true">
        <a href="#home" role="menuitem">Home</a>
        <a href="#about" role="menuitem">About</a>
        <a href="#services" role="menuitem">Services</a>
        <a href="#contact" role="menuitem">Contact</a>
        <button class="menu-toggle" aria-controls="side-menu" id="close-menu">Close Menu</button>
    </nav>
    <!-- Overlay to dim background when menu is open -->
    <div class="overlay" id="menu-overlay"></div>
    <!-- Main Content Area -->
    <main>
        <h1>Welcome to Our Website</h1>
        <p>Explore our content and learn more about our services.</p>
    </main>
    <script>
        const menuToggle = document.getElementById('menu-toggle');
        const sideMenu = document.getElementById('side-menu');
        const closeMenuButton = document.getElementById('close-menu');
        const overlay = document.getElementById('menu-overlay');
        
        // Function to open the menu
        function openMenu() {
            sideMenu.classList.add('open');
            overlay.classList.add('show');
            menuToggle.setAttribute('aria-expanded', 'true');
            sideMenu.setAttribute('aria-hidden', 'false');
            closeMenuButton.focus();
        }
        // Function to close the menu
        function closeMenu() {
            sideMenu.classList.remove('open');
            overlay.classList.remove('show');
            menuToggle.setAttribute('aria-expanded', 'false');
            sideMenu.setAttribute('aria-hidden', 'true');
            menuToggle.focus();
        }
        // Toggle menu when the menu button is clicked
        menuToggle.addEventListener('click', openMenu);
        closeMenuButton.addEventListener('click', closeMenu);
        overlay.addEventListener('click', closeMenu);
        // Close menu when pressing Escape
        sideMenu.addEventListener('keydown', (e) => {
            if (e.key === 'Escape') {
                closeMenu();
            }
        });
    </script>
</body>
</html>

Explanation #

  1. ARIA Roles and Attributes:
    • The side menu is labeled with aria-labelledby="menu-toggle" so that screen readers associate the “Menu” button with the menu itself.
    • aria-controls="side-menu" on the toggle button defines which element it controls.
    • aria-expanded toggles between true and false on the menu button, indicating the open or closed state of the menu.
    • The side menu has role="dialog" and aria-hidden="true" by default, making it identifiable as a modal-like component. aria-hidden switches to false when the menu is open.
  2. Keyboard Navigation:
    • The Enter or Space key on the menu button opens the side menu and moves focus to the “Close Menu” button inside the menu.
    • Users can navigate through menu items with the Tab key.
    • Escape Key: Pressing Escape closes the menu, and focus returns to the menu toggle button.
  3. Overlay for Background Dimming:
    • The overlay dims the background content and intercepts clicks outside the side menu, providing a more focused user experience.
    • Clicking on the overlay closes the menu.
  4. Focus Management:
    • When the menu opens, focus is moved to the close button inside the menu to contain navigation within the menu.
    • When the menu closes, focus returns to the menu toggle button for consistent user experience and logical flow.
  5. Responsive Design:
    • This example is mobile-friendly, making it ideal for small screens where side menus are common.

Key Takeaways #

  • ARIA Attributes for Accessibility: Use aria-controls, aria-expanded, and aria-hidden to indicate the state of the menu to assistive technologies.
  • Keyboard-Accessible Menu: Ensure the menu can be opened and closed with the Enter, Space, and Escape keys for smooth keyboard navigation.
  • Focus Management: Move focus to the first interactive element in the menu when opened and return focus to the menu toggle button upon closing.
  • Overlay for Visual Focus: Adding an overlay improves the visual focus by dimming the background when the menu is open.


Screen reader: NVDA
Video:

What are your feelings

Updated on March 6, 2025