Skip to the main content

All tutorials

A responsive navigation menu that folds on a phone

  • Level: Beginner
  • About 5 minutes to read (an estimate from the word count: 838 words at 200 words a minute)
  • Last checked:

Write a menu that is a plain list of links inside the right landmarks, sits in a row from 48em and folds behind a button below that.

What you will be able to do

  • Lay out a page skeleton with header, nav, main, one h1 and a skip link
  • Write the menu as a list of links that works without JavaScript
  • Place the list in a row from 48em with one media query
  • Add a menu button that reports its state with aria-expanded
  • Close the menu with Escape and mark the current page

Start with the page skeleton

A menu belongs to a page, so begin with the page. Use header for the top of the page, nav for a block of navigation links and main for the content that belongs to this page alone. Give the page one h1. These elements are landmarks: screen reader users can list them and jump between them, so a page built from plain div elements is harder to move around in.

Put a skip link first. It is a link to the id of main, and it stays out of sight until it receives keyboard focus. The Web Content Accessibility Guidelines (WCAG) ask for a way to bypass blocks of content that repeat on several pages, and a menu is such a block. Add tabindex="-1" to main, so that following the link moves focus to the content as well as scrolling to it.

If a page has more than one nav, give each its own aria-label, such as "Main". Then the landmark list does not show several entries with the same name.

A menu is a list of links. Write it as a ul with one li and one a for each destination. The list tells assistive technology how many items there are. Make the link text name the destination, such as "Guides", and not an instruction, such as "Click here".

The first example has no script at all. Below 48em the links stack in one column. Each link is a block with padding, which makes the target larger.

HTML

<div class="nav-links">
  <a class="nav-links__skip" href="#nav-links-main">Skip to the content</a>
  <header class="nav-links__header">
    <p class="nav-links__brand">Example site</p>
    <nav aria-label="Main">
      <ul class="nav-links__list">
        <li><a href="#nav-links-main">Home</a></li>
        <li><a href="#nav-links-main">Guides</a></li>
        <li><a href="#nav-links-main">Reference</a></li>
        <li><a href="#nav-links-main">Tools</a></li>
        <li><a href="#nav-links-main">About</a></li>
      </ul>
    </nav>
  </header>
  <main id="nav-links-main" class="nav-links__main" tabindex="-1">
    <h1>Welcome</h1>
    <p>The menu above is a list of links inside a nav landmark. It works without any script.</p>
  </main>
</div>

CSS

.nav-links {
  --nl-bg: #ffffff;
  --nl-fg: #1b1f2a;
  --nl-muted: #4a5163;
  --nl-line: #c9ced9;
  --nl-accent: #3730a3;
  --nl-on-accent: #ffffff;
  min-height: 100vh;
  background: var(--nl-bg);
  color: var(--nl-fg);
}
.nav-links__skip {
  position: absolute;
  inset-inline-start: 0.5rem;
  inset-block-start: -4rem;
  padding: 0.5rem 0.75rem;
  background: var(--nl-accent);
  color: var(--nl-on-accent);
}
.nav-links__skip:focus-visible {
  inset-block-start: 0.5rem;
  outline: 3px solid var(--nl-fg);
  outline-offset: 2px;
}
.nav-links__header {
  padding: 0.75rem 1rem;
  border-block-end: 1px solid var(--nl-line);
}
.nav-links__brand { margin: 0 0 0.5rem; font-weight: 700; }
.nav-links__list {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  margin: 0;
  padding: 0;
  list-style: none;
}
.nav-links__list a {
  display: block;
  padding: 0.625rem 0.75rem;
  color: var(--nl-accent);
  text-decoration: none;
  border-radius: 6px;
}
.nav-links__list a:hover { text-decoration: underline; }
.nav-links__list a:focus-visible,
.nav-links__main:focus-visible {
  outline: 3px solid var(--nl-accent);
  outline-offset: 2px;
}
.nav-links__main { padding: 1rem; }
.nav-links__main h1 { margin-block: 0 0.5rem; font-size: 1.5rem; }
@media (min-width: 48em) {
  .nav-links__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1.5rem;
  }
  .nav-links__brand { margin: 0; }
  .nav-links__list { flex-direction: row; gap: 0.5rem; }
}
@media (prefers-color-scheme: dark) {
  .nav-links {
    --nl-bg: #12141c;
    --nl-fg: #eceef5;
    --nl-muted: #b6bccb;
    --nl-line: #444b5c;
    --nl-accent: #b4b8ff;
    --nl-on-accent: #12141c;
  }
}

A row from 48em

Write the narrow layout first, because it is the layout with the fewest rules. Then add one media query that changes the list to a row.

.menu { display: flex; flex-direction: column; }

@media (min-width: 48em) {
  .menu { flex-direction: row; gap: 0.5rem; }
}
The whole switch from a column to a row.

Write the breakpoint in em. In a media query, em is relative to the browser's initial font size, not to any style on your page. At the usual default of 16 px, 48em is 768 px, and it grows when a visitor raises their default text size. The menu then folds earlier, which is what you want when the text no longer fits.

Below 48em the list hides behind a button. Use a real button with type="button", so it can be reached with Tab and pressed with Enter or Space. It reports its state with aria-expanded, which is true while the list is shown and false while it is hidden. It names the list with aria-controls.

Write the button into the HTML with the hidden attribute, and let the script show it. A visitor whose script has not run then keeps a menu that is always open, which is the better failure.

button.hidden = false;
root.classList.add('nav-toggle--ready');

button.addEventListener('click', function () {
  setOpen(button.getAttribute('aria-expanded') !== 'true');
});
The script shows the button, marks the menu as ready to fold, and flips one attribute.

The class nav-toggle--ready is what folds the list in the style sheet. Without the script the class is never added, so the list stays visible. At 48em and wider, a rule with the same weight puts the list back in a row and hides the button.

HTML

<div class="nav-toggle" id="nav-toggle-root">
  <header class="nav-toggle__header">
    <p class="nav-toggle__brand">Example site</p>
    <button type="button" class="nav-toggle__button" id="nav-toggle-button" aria-expanded="false" aria-controls="nav-toggle-list" hidden>Menu</button>
    <nav aria-label="Main">
      <ul class="nav-toggle__list" id="nav-toggle-list">
        <li><a href="#nav-toggle-main">Home</a></li>
        <li><a href="#nav-toggle-main">Guides</a></li>
        <li><a href="#nav-toggle-main">Reference</a></li>
        <li><a href="#nav-toggle-main">Tools</a></li>
        <li><a href="#nav-toggle-main">About</a></li>
      </ul>
    </nav>
  </header>
  <main id="nav-toggle-main" class="nav-toggle__main">
    <h1>Welcome</h1>
    <p>On a narrow screen the list folds behind the Menu button. Press Escape to close it.</p>
  </main>
</div>

CSS

.nav-toggle {
  --nt-bg: #ffffff;
  --nt-fg: #1b1f2a;
  --nt-line: #c9ced9;
  --nt-accent: #3730a3;
  --nt-on-accent: #ffffff;
  min-height: 100vh;
  background: var(--nt-bg);
  color: var(--nt-fg);
}
.nav-toggle__header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem 1rem;
  padding: 0.75rem 1rem;
  border-block-end: 1px solid var(--nt-line);
}
.nav-toggle__brand { margin: 0; font-weight: 700; }
.nav-toggle__button {
  min-height: 44px;
  padding: 0.5rem 1rem;
  font: inherit;
  color: var(--nt-on-accent);
  background: var(--nt-accent);
  border: 1px solid var(--nt-accent);
  border-radius: 8px;
  cursor: pointer;
}
.nav-toggle__button[hidden] { display: none; }
.nav-toggle > .nav-toggle__header > nav { flex-basis: 100%; }
.nav-toggle__list {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  margin: 0;
  padding: 0;
  list-style: none;
}
.nav-toggle__list a {
  display: block;
  padding: 0.625rem 0.75rem;
  color: var(--nt-accent);
  text-decoration: none;
  border-radius: 6px;
}
.nav-toggle__list a:hover { text-decoration: underline; }
.nav-toggle__button:focus-visible,
.nav-toggle__list a:focus-visible {
  outline: 3px solid var(--nt-accent);
  outline-offset: 2px;
}
.nav-toggle__button:focus-visible { outline-color: var(--nt-fg); }
.nav-toggle--ready .nav-toggle__list:not(.nav-toggle__list--open) { display: none; }
.nav-toggle__main { padding: 1rem; }
.nav-toggle__main h1 { margin-block: 0 0.5rem; font-size: 1.5rem; }
@media (min-width: 48em) {
  .nav-toggle > .nav-toggle__header > nav { flex-basis: auto; }
  .nav-toggle__list,
  .nav-toggle--ready .nav-toggle__list:not(.nav-toggle__list--open) { display: flex; flex-direction: row; gap: 0.5rem; }
  .nav-toggle__button,
  .nav-toggle__button[hidden] { display: none; }
}
@media (prefers-color-scheme: dark) {
  .nav-toggle {
    --nt-bg: #12141c;
    --nt-fg: #eceef5;
    --nt-line: #444b5c;
    --nt-accent: #b4b8ff;
    --nt-on-accent: #12141c;
  }
}

JavaScript

// The menu button is hidden in the HTML. This script shows it and folds the list,
// so a visitor without JavaScript keeps a menu that is always open.
var root = document.getElementById('nav-toggle-root');
var button = document.getElementById('nav-toggle-button');
var list = document.getElementById('nav-toggle-list');

function setOpen(open) {
  button.setAttribute('aria-expanded', String(open));
  list.classList.toggle('nav-toggle__list--open', open);
}

button.hidden = false;
root.classList.add('nav-toggle--ready');

button.addEventListener('click', function () {
  setOpen(button.getAttribute('aria-expanded') !== 'true');
});

root.addEventListener('keydown', function (event) {
  if (event.key === 'Escape' && button.getAttribute('aria-expanded') === 'true') {
    setOpen(false);
    button.focus();
  }
});

The preview is as wide as this column, so you see the row on a wide screen and the folded menu on a phone. Narrow the browser window below 48em to see both states.

Closing with Escape

A person who opens a menu with the keyboard expects Escape to close it. The pattern in the WAI-ARIA Authoring Practices does two things: Escape closes the open menu, and focus returns to the button that controls it. Without the second step, focus would be left on an element that has disappeared.

The example listens for keydown on the root element, so Escape works from the button and from any link in the list.

Marking the current page

Show the visitor where they are. Put aria-current="page" on the link that points to the page being shown. Assistive technology then announces it as the current page. Style the same attribute, so that what is announced and what is shown cannot drift apart.

.menu a[aria-current="page"] {
  font-weight: 700;
  border-block-end-color: currentColor;
}
One selector styles the current link and nothing else.

Your server or build step writes the attribute when it makes each page. Do not use colour alone: the example uses weight, a background and a bar under the link.

HTML

<nav class="nav-current" aria-label="Main">
  <ul class="nav-current__list">
    <li><a href="#nav-current-note">Home</a></li>
    <li><a href="#nav-current-note">Guides</a></li>
    <li><a href="#nav-current-note" aria-current="page">Reference</a></li>
    <li><a href="#nav-current-note">Tools</a></li>
    <li><a href="#nav-current-note">About</a></li>
  </ul>
  <p class="nav-current__note" id="nav-current-note">The link with <code>aria-current="page"</code> is the page you are on. Your server writes the attribute when it builds each page.</p>
</nav>

CSS

.nav-current {
  --nc-bg: #ffffff;
  --nc-fg: #1b1f2a;
  --nc-muted: #4a5163;
  --nc-line: #c9ced9;
  --nc-accent: #3730a3;
  --nc-mark: #e4e6fb;
  max-width: 42rem;
  padding: 1rem;
  background: var(--nc-bg);
  color: var(--nc-fg);
}
.nav-current__list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem;
  margin: 0;
  padding: 0;
  list-style: none;
  border-block-end: 1px solid var(--nc-line);
}
.nav-current__list a {
  display: block;
  padding: 0.625rem 0.875rem;
  color: var(--nc-accent);
  text-decoration: none;
  border-block-end: 3px solid transparent;
}
.nav-current__list a:hover { text-decoration: underline; }
.nav-current__list a[aria-current="page"] {
  color: var(--nc-fg);
  font-weight: 700;
  background: var(--nc-mark);
  border-block-end-color: var(--nc-accent);
}
.nav-current__list a:focus-visible {
  outline: 3px solid var(--nc-accent);
  outline-offset: 2px;
}
.nav-current__note { margin: 1rem 0 0; color: var(--nc-muted); }
@media (prefers-color-scheme: dark) {
  .nav-current {
    --nc-bg: #12141c;
    --nc-fg: #eceef5;
    --nc-muted: #b6bccb;
    --nc-line: #444b5c;
    --nc-accent: #b4b8ff;
    --nc-mark: #2a2f45;
  }
}

All examples on this page passed their checks in Chrome 143 on 3 December 2026. They were not tested in other browsers.

References

These snippets in the gallery use the same techniques: