Check 07.06 · Mobile-first and responsive design

Optimize mobile menus

Build a mobile menu that is complete, crawlable and usable with a thumb: accessible button, accordion submenus, focus, scrolling, on CMSs and frameworks.

Priority
Critical
Difficulty
Intermediate
Estimated time
3 h to 1 day
Free, no account: your progress is saved in this browser.

Ask an AI to check this point on your site

Opens the assistant with a ready-made question: this check, its goal and your site URL.

For Gemini, Copilot or another assistant, copy the question. The link opens the chosen service directly: nothing goes through this site.

In short

On mobile, the main menu collapses behind a button, often the three-line icon known as the “hamburger”. Optimizing it means guaranteeing three things: it contains the same links as on desktop, present in the HTML; it opens, closes and can be browsed easily with a finger, a keyboard and a screen reader; it gets in the way of neither reading nor performance. Once the task is done, the mobile menu opens with one tap, its submenus expand as an accordion, the Escape key closes it, and a curl request with a mobile user agent finds all its links.

Why it matters

Google indexes the mobile version of pages. The menu is the only block of links repeated on every page: if the mobile version contains fewer links than the desktop version, the missing links lose this internal signal. Google only follows <a href> links and its crawlers do not click buttons; a menu whose links are only loaded on opening, with JavaScript, is invisible to them. A menu present in the HTML but hidden with CSS until it is opened, on the other hand, remains readable.

For visitors, the mobile menu piles up pitfalls: a button with no label, submenus that only open on hover, a page that scrolls behind the open menu, no way to go back, a menu that stays open after tapping an anchor. Each one costs visits to deep pages and conversions.

The desktop menu itself falls under another task: optimize menus and submenus (opens in a new tab) covers entries, labels and structure. This guide focuses on the mobile version.

What it improves

  • Crawling and internal linking All the menu links are available in the indexed version.
  • Mobile navigation Sections reached in two taps, without zooming or missed targets.
  • Accessibility A menu usable with a keyboard and a screen reader, with its open or closed state announced.
  • Performance A menu in HTML and CSS, with little JavaScript, responds quickly to taps, which helps INP (Interaction to Next Paint).
  • Rankings Indirect effect, through internal linking and usage.

How to check

Are the links in the HTML served to mobile devices? Count the navigation links with a mobile user agent, then a desktop one:

MOBILE="Mozilla/5.0 (Linux; Android 14; Pixel 8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0 Mobile Safari/537.36"
DESKTOP="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0 Safari/537.36"

for UA in "$MOBILE" "$DESKTOP"; do
  curl -s -A "$UA" https://example.com/ | tr '\n' ' ' | grep -o '<nav.*</nav>' | grep -o '<a [^>]*href="[^"]*"' | sort -u | wc -l
done

The two numbers must be close, and equal to the number of menu entries. A much lower number on mobile signals a trimmed-down menu or one loaded with JavaScript. To compare the links one by one, see check internal linking parity (opens in a new tab).

Does it work with a finger? On a real phone: open the menu, expand a submenu, tap a link, go back. Check that the page does not scroll behind the open menu and that the menu closes after navigating.

Does it work with a keyboard? In Chrome's device mode, on desktop: Tab to the button, Enter to open, Tab through the links, Escape to close; focus returns to the button.

Is it announced correctly? Inspect the button: it has aria-expanded="false" when closed and "true" when open, and an accessible name (“Menu”).

A compliant mobile menu: a labeled button of at least 44 px, the same entries as on desktop, each parent section reachable through a link, submenus that expand on tap, closing with Escape, with the button and after navigating, and no scrolling of the page in the background.

How to implement

Before you start

Design rules that apply everywhere:

  1. Same entries as on desktop. If the desktop menu is too rich for a phone, trim it for everyone rather than cutting on mobile only.
  2. An explicit button. An icon alone is ambiguous for some visitors; adding the word “Menu” next to it is a widespread convention. Place it at the top of the screen, always in the same spot.
  3. Priority links visible. On an e-commerce or service site, keep the one to three key actions outside the collapsed menu: search, cart, phone or quote.
  4. Accordion submenus, opened on tap, never on hover. The parent label remains a link to the section page; a separate button expands the submenu. If the design rules this out, add a “View all” link to the parent page at the top of each submenu.
  5. Limited depth: two levels on mobile. Beyond that, visitors get lost; send them to the section page, which lists the rest.
  6. Comfortable targets: each menu link is at least 44 px high. See size tap targets appropriately (opens in a new tab).

Reference implementation, in HTML, CSS and JavaScript with no library. All the links are in the page; the CSS hides them until the menu is opened.

<header class="site-header">
  <a href="/" class="site-logo">Thé des Lisières</a>

  <button type="button" class="menu-toggle" aria-expanded="false" aria-controls="menu-principal">
    <svg aria-hidden="true" width="24" height="24" viewBox="0 0 24 24"><path d="M3 6h18M3 12h18M3 18h18" stroke="currentColor" stroke-width="2"/></svg>
    <span>Menu</span>
  </button>

  <nav id="menu-principal" class="main-nav" aria-label="Main navigation">
    <ul class="menu">
      <li class="menu-item">
        <a href="/the-vert/">Green tea</a>
        <button type="button" class="submenu-toggle" aria-expanded="false" aria-controls="sous-menu-the-vert">
          <span class="visually-hidden">Show the Green tea submenu</span>
        </button>
        <ul class="submenu" id="sous-menu-the-vert">
          <li><a href="/the-vert/sencha/">Sencha</a></li>
          <li><a href="/the-vert/matcha/">Matcha</a></li>
        </ul>
      </li>
      <li class="menu-item"><a href="/guides/">Guides</a></li>
    </ul>
  </nav>
</header>
:root {
  --header-height: 3.5rem;
}

html {
  scroll-padding-top: var(--header-height);
}

html.menu-open {
  overflow: hidden;
}

.site-header {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: var(--header-height);
  padding-inline: 1rem;
  background: #fff;
}

.menu-toggle {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  min-height: 44px;
  padding: 0.5rem 0.75rem;
}

.main-nav {
  display: none;
}

.main-nav.is-open {
  display: block;
  position: fixed;
  inset: var(--header-height) 0 0 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  background: #fff;
  padding: 1rem;
}

.menu-item {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
}

.menu-item > a {
  flex: 1;
  padding-block: 0.75rem;
}

.submenu-toggle {
  min-width: 44px;
  min-height: 44px;
}

.submenu {
  display: none;
  flex-basis: 100%;
  padding-left: 1rem;
}

.submenu.is-open {
  display: block;
}

.submenu a {
  display: block;
  padding-block: 0.75rem;
}

@media (min-width: 64em) {
  .menu-toggle {
    display: none;
  }

  .main-nav,
  .main-nav.is-open {
    display: block;
    position: static;
    padding: 0;
  }
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}
const menuToggle = document.querySelector('.menu-toggle');
const mainNav = document.getElementById(menuToggle.getAttribute('aria-controls'));
const background = document.querySelectorAll('main, footer');
const desktop = window.matchMedia('(min-width: 64em)');

function setMenu(isOpen, { restoreFocus = true } = {}) {
  menuToggle.setAttribute('aria-expanded', String(isOpen));
  mainNav.classList.toggle('is-open', isOpen);
  document.documentElement.classList.toggle('menu-open', isOpen);
  background.forEach((element) => {
    element.inert = isOpen;
  });

  if (isOpen) {
    mainNav.querySelector('a, button').focus();
    return;
  }

  if (restoreFocus) {
    menuToggle.focus();
  }
}

menuToggle.addEventListener('click', () => {
  setMenu(menuToggle.getAttribute('aria-expanded') !== 'true');
});

document.addEventListener('keydown', (event) => {
  if (event.key === 'Escape' && menuToggle.getAttribute('aria-expanded') === 'true') {
    setMenu(false);
  }
});

mainNav.addEventListener('click', (event) => {
  if (event.target.closest('a')) {
    setMenu(false, { restoreFocus: false });
  }
});

desktop.addEventListener('change', (event) => {
  if (event.matches) {
    setMenu(false, { restoreFocus: false });
  }
});

document.querySelectorAll('.submenu-toggle').forEach((button) => {
  button.addEventListener('click', () => {
    const submenu = document.getElementById(button.getAttribute('aria-controls'));
    const isOpen = button.getAttribute('aria-expanded') === 'true';

    button.setAttribute('aria-expanded', String(!isOpen));
    submenu.classList.toggle('is-open', !isOpen);
  });
});

On desktop, add the horizontal display and the opening of submenus on hover or focus described in the guide on the main menu. What this code does on mobile: the button announces its state with aria-expanded; on opening, focus moves into the menu and the rest of the page becomes inactive thanks to the inert attribute, which keeps focus from wandering there; the page no longer scrolls in the background; Escape closes the menu and returns focus to the button; tapping a link closes the menu, which is useful for internal anchors; switching to desktop width resets the state. The pattern follows the disclosure button pattern described by the W3C.

Good to know: without JavaScript, this menu stays closed but its links are in the HTML, so crawlers read them. If you want it to open without JavaScript too, use a <details> element with a “Menu” <summary>: the browser handles opening natively. The popover attribute, supported by current browsers since 2025, is another native option, which also handles closing with Escape.

For the sticky header, keep a constant height, defined from the first render: a header that changes size on load or while scrolling causes layout shifts. See target CLS ≤ 0.1 (opens in a new tab).

WordPress

Versions WordPress 6.4 to 7.1, block themes and classic themes.

Classic theme: the mobile menu depends on the theme. Assign the same menu to the mobile location if there is a separate one (Appearance > Menus > Manage Locations), and check with the curl command that the links are in the HTML. Be wary of mega menu plugins and page builders that generate a second, reduced “mobile” menu, or that load submenus on tap.

Block theme: select the Navigation block in the Site Editor (Appearance > Editor). The settings moved with WordPress 7.0.

WordPress 7.0 and later

In the Settings tab, “Overlay” section, set the overlay visibility to “Mobile”: the menu collapses behind an icon on small screens. Tap the icon preview and turn off the icon button option (“Show icon button”) to display the text labels “Menu” and “Close”. In the display section, set submenu visibility to “Click” rather than “Hover” and turn on the arrow. The overlay template section (“Overlay template”, “Create overlay” button) lets you customize the open menu, as a template part; keep the same links in it.

WordPress 6.4 to 6.9

In the Settings tab, display section, set “Overlay Menu” to “Mobile”. Tap the icon preview and turn off the icon button option to get the “Menu” and “Close” labels. In the submenus section, turn on “Open on click” and “Show arrow”. The content of the open menu cannot be customized: only its colors can be set, in the Styles tab.

Shopify

Versions Horizon and Dawn Online Store 2.0 themes, all plans, November 2026.

In the official Horizon and Dawn themes, the mobile menu is a drawer built with <details> and <summary> elements, from the same menu as the desktop version (Content > Menus, main menu): the links are in the HTML. In this drawer, an entry that has child items is just an expandable label, with no link to its own page. So add a “View all” link to the matching collection as the first child item of each parent entry. The drawer's color scheme and appearance are set in the Header section of the theme editor.

PrestaShop

Versions PrestaShop 8 and 9, Classic and Hummingbird themes, Main menu module (ps_mainmenu).

The “Main menu” module feeds both the desktop and the mobile version. In the Classic theme, the theme's script moves the same menu block into the mobile container when the width drops below the breakpoint: the links are not duplicated. The Hummingbird theme generates, from the same data, a second tree in a side panel (offcanvas): the links in it are rendered server-side, and each parent section keeps its link, with a separate button to open its subcategories. Check that subcategories expand on tap, and that third-party mega menu modules do not generate a reduced mobile version.

Magento

Versions Magento Open Source and Adobe Commerce 2.4.6 to 2.4.9, Luma theme.

The Luma theme reuses the same category navigation on mobile, presented in a side panel with “Menu” and “Account” tabs and language settings. Categories excluded with “Include in Menu” disappear from both versions. Mega menu extensions often have a separate mobile rendering: check its HTML with the curl command.

Drupal

Versions Drupal 10 and 11, Olivero theme.

Olivero provides a “Menu” button and an accessible mobile navigation, built from the “Main navigation” menu. Set the depth displayed in the menu block configuration (Structure > Block layout) and check the “Expand all menu links” option so that child items are rendered in the HTML.

Joomla

Versions Joomla 5 and 6, Cassiopeia template.

In Content > Site Modules, open the main menu module. Module tab: set the end level to include child items and turn on Show Submenu Items, otherwise they are not in the HTML. Advanced tab: choose a collapsible layout, such as the “collapse-metismenu” or “dropdown-metismenu” layouts provided by Cassiopeia, which collapse the menu behind a button on small screens.

Wix

Versions Wix Editor, all plans, November 2026.

Wix generates a mobile menu from the site menu. In the mobile editor, select the menu button to set its appearance and that of the open menu. Pages hidden from the menu disappear from both versions; subpages are displayed as an accordion.

Squarespace

Versions Squarespace 7.1, all plans, November 2026.

The mobile menu reuses the pages of the main navigation. In the header editor, mobile style section, set the icon, layout and alignment. Folders become submenus: since they have no page of their own, add a link to the section page at the top of each folder.

Webflow

Versions Webflow Designer, Navbar component, all site plans, November 2026.

Use the Navbar component: its settings define the breakpoint at which the menu collapses behind the button (Menu Button). The links remain those of the Nav Menu element, published in HTML. For submenus, the Dropdown component opens on tap; add a link to the parent page in the list, because the toggle is not a link.

Laravel

Versions Laravel 10 to 13, Alpine.js 3.

Generate the menu in a single Blade component, used for both versions, and add the reference CSS and JavaScript. With Alpine.js, the state takes a few attributes, without removing the links from the HTML:

<header class="site-header" x-data="{ open: false }" @keydown.escape.window="open = false">
    <button type="button" class="menu-toggle" :aria-expanded="open.toString()" aria-controls="menu-principal" @click="open = !open">
        Menu
    </button>

    <nav id="menu-principal" class="main-nav" :class="{ 'is-open': open }" aria-label="Main navigation">
        <x-main-nav-items />
    </nav>
</header>

Do not use x-if to display the menu: it removes the links from the DOM as long as the menu is closed. :class or x-show keep them in the page.

Symfony

Versions Symfony 6.4, 7.4 and 8.x; Symfony UX 2 and 3 (Stimulus, Twig Components).

Same principle with a Twig component or a Stimulus controller that toggles the is-open class and aria-expanded. The menu is rendered once, server-side, with its links.

Next.js

Versions Next.js 15 and 16 (App Router).

Render the list of links in a server component and isolate the open state in a client component. Close the menu on every page change:

// app/components/mobile-menu.tsx
'use client'

import { useEffect, useState, type ReactNode } from 'react'
import { usePathname } from 'next/navigation'

export default function MobileMenu({ children }: { children: ReactNode }) {
  const [isOpen, setIsOpen] = useState(false)
  const pathname = usePathname()

  useEffect(() => {
    setIsOpen(false)
  }, [pathname])

  return (
    <>
      <button type="button" className="menu-toggle" aria-expanded={isOpen} aria-controls="menu-principal" onClick={() => setIsOpen(!isOpen)}>
        Menu
      </button>
      <nav id="menu-principal" className={isOpen ? 'main-nav is-open' : 'main-nav'} aria-label="Main navigation">
        {children}
      </nav>
    </>
  )
}

Pass the links, rendered by the server with Link, as children. Do not write {isOpen && <nav>…</nav>}: when closed, the menu would disappear from the HTML. Add the Escape and inert handling from the reference JavaScript in a useEffect.

Nuxt

Versions Nuxt 3 and 4.

Same principle with a Vue component: use v-show or a bound class, not v-if, so that the NuxtLink links stay in the server-rendered HTML. Close the menu by watching useRoute().fullPath with watch.

Astro

Versions Astro 5 to 7.

The menu is rendered as static HTML by default. Add the reference JavaScript in a <script> tag in the header component; Astro bundles it and loads it without a framework.

Common mistakes

  • Reduced mobile menu: links removed from the mobile version, and therefore from the indexed version.
  • Links loaded on opening with JavaScript, or rendered conditionally by the framework: missing from the HTML.
  • Parent section without a link: the label only expands the submenu, and the section page is no longer linked from the menu.
  • Submenus on hover: unusable with a finger.
  • Icon with no accessible name: the screen reader announces “button” and nothing more.
  • Page scrolling behind the menu and focus escaping to the page.
  • Menu that stays open after tapping an anchor or after a page change in a single-page application.
  • Desktop mega menu displayed as is on tablets, without the room it needs.

Frequently asked questions

Do you need a “Menu” label next to the hamburger icon?

It is a widespread good practice. Not every visitor understands the three-line icon, and the word “Menu” removes the ambiguity at minimal cost in space. It also gives the button an accessible name read by screen readers. On WordPress, the Navigation block lets you replace the icon with the “Menu” and “Close” labels.

How do you make a mobile menu accessible?

Use a real button with aria-expanded and aria-controls, and a name such as “Menu”. On opening, move focus into the menu and make the rest of the page inactive, for example with the inert attribute. Close the menu with the Escape key and return focus to the button. Submenus expand through a separate button, and each link is at least 44 pixels high.

Updated on

Topic checklist (PDF)

And now?

In the “Mobile-first and responsive design” topic

  1. Check responsive layout on smartphones and tablets
  2. Check the viewport meta tag
  3. Eliminate unintended horizontal scrolling
  4. Ensure legible text size
  5. Size tap targets appropriately
  6. Optimize mobile menus
  7. Check mobile/desktop content parity
  8. Check metadata parity
  9. Check structured data parity
  10. Check internal linking parity
  11. Avoid intrusive interstitials
  12. Test mobile forms and conversions
  13. Test Googlebot Smartphone rendering