Sub topic 3
Start with semantic nav markup
This chapter assumes you know basic HTML structure and semantic elements, can write CSS with flexbox and media queries, and can select elements and handle events in JavaScript. Every pattern later in this tutorial (hamburger menu, tab bar, priority menu) is built on the markup in this chapter. If the markup is wrong, each pattern inherits the problem, and CSS and JavaScript can only patch some of it.
Why markup comes first
A navigation pattern has two layers. The visual layer decides how links look and where they sit. The structural layer tells browsers, search engines, and assistive technology what the links are. The structural layer is the markup.
When the markup is semantic, you get several things without extra code:
- Screen readers list the page's navigation regions and let users jump straight to them.
- Keyboard users can tab through real links with no scripting.
- The page still works if JavaScript fails to load or throws an error.
- You can restyle the same markup for a hamburger, a tab bar, or a priority menu without rewriting it.
The last point matters most for this tutorial. You will write one navigation structure and change its presentation at different breakpoints.
The baseline: nav, list, links
The smallest correct navigation looks like this:
<nav aria-label="Main">
<ul>
<li><a href="/products">Products</a></li>
<li><a href="/pricing">Pricing</a></li>
<li><a href="/docs">Docs</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
</nav>Each element has a job.
| Element | What it communicates |
|---|---|
| `<nav>` | A landmark: a major block of navigation links. Assistive technology exposes it as a region the user can jump to. |
| `<ul>` and `<li>` | The links form a list. Screen readers announce "list, 4 items", which tells the user how long the menu is before they read it. |
| `<a href>` | Each item goes somewhere. Links are focusable and activate with Enter, and browsers give them features such as open in new tab and copy link address. |
Use real links for destinations
A link navigates. A button performs an action on the current page. In navigation, destinations are links. The toggle that opens a hamburger menu is a button, because it changes the page state and goes nowhere.
This is the most common markup error in mobile navigation:
<!-- Avoid: not focusable, no link semantics, breaks without JavaScript -->
<div class="nav-item" onclick="location.href='/pricing'">Pricing</div>
<!-- Avoid: link that is really a button -->
<a href="#" onclick="openMenu()">Menu</a>
<!-- Prefer -->
<a href="/pricing">Pricing</a>
<button type="button">Menu</button>A div with a click handler cannot receive keyboard focus, is not announced as a link, and does nothing when JavaScript is off. A link with href="#" jumps the page to the top and is announced as a link when it behaves like a button. Choosing the right element removes all of these problems.
Landmarks and labels
<nav> creates a landmark, and a page often has more than one. A typical site has a main navigation, a footer navigation, and perhaps a breadcrumb trail or a section menu. If you have several nav elements, screen reader users see a list of landmarks that all read "navigation". They cannot tell them apart unless you label them.
Use aria-label to give each one a short, distinct name:
<header>
<nav aria-label="Main">
<ul>
<li><a href="/products">Products</a></li>
<li><a href="/pricing">Pricing</a></li>
</ul>
</nav>
</header>
<main>
<nav aria-label="Breadcrumb">
<ol>
<li><a href="/">Home</a></li>
<li><a href="/docs">Docs</a></li>
<li aria-current="page">Install guide</li>
</ol>
</nav>
</main>
<footer>
<nav aria-label="Footer">
<ul>
<li><a href="/privacy">Privacy</a></li>
<li><a href="/terms">Terms</a></li>
</ul>
</nav>
</footer>A few rules keep labels useful:
- Do not repeat the role in the label. Screen readers already say "navigation", so
aria-label="Main"is announced as "Main navigation".aria-label="Main navigation"becomes "Main navigation navigation". - Keep each label unique on the page.
- If you have only one
nav, a label is optional, but adding one costs nothing and helps when a secondnavappears later. - Do not add
role="navigation"to anavelement. It is already implied, so the attribute is redundant.
Do not wrap every group of links in nav
nav is for major navigation blocks. A row of "Related articles" links inside an article, or a pair of social icons, does not need to be a landmark. If every group of links is a landmark, the landmark list becomes noise and the real navigation is harder to find. A good test is whether a user would want to jump to this block from anywhere on the page. If not, a plain list inside a section is enough.
Marking the current page with aria-current
Users need to know where they are. Many sites show this with a CSS class such as active. A class is visible only to people who see the styling. aria-current="page" exposes the same fact to assistive technology, and you can use it as a CSS hook as well:
<nav aria-label="Main">
<ul>
<li><a href="/products">Products</a></li>
<li><a href="/pricing" aria-current="page">Pricing</a></li>
<li><a href="/docs">Docs</a></li>
</ul>
</nav>
<style>
nav a[aria-current="page"] {
font-weight: bold;
border-bottom: 2px solid currentColor;
}
</style>With this, the style and the semantics cannot drift apart, because a single attribute drives both. The tab bar chapter reuses this attribute. Do not rely on color alone to show the current item. The bold weight and underline above also work for users who cannot distinguish colors.
Set aria-current on the server when you render the page, or in your template. If your site is a single-page application, update the attribute whenever the route changes, and remove it from the previous link.
Let the menu work without JavaScript
Menus that start open and get hidden by scripting fail gracefully. Menus that start hidden and depend on scripting to open fail completely. For the hamburger pattern in the next chapters, the order of work matters:
- Write the full list of links as ordinary markup.
- Add the toggle button only through JavaScript, or show it only when scripting runs.
- Hide the list only once the script has set up the toggle.
The topic's later examples place hidden on the list directly in HTML, which is simpler to teach and fine for most sites. If you must support users without JavaScript, apply the hidden attribute from the script instead:
const menu = document.getElementById('site-menu');
const toggle = document.getElementById('menu-toggle');
// Only hide the menu once we know the toggle will work.
if (menu && toggle) {
toggle.hidden = false;
menu.hidden = true;
}Pair it with markup that starts with the toggle hidden:
<button id="menu-toggle" type="button" aria-expanded="false" aria-controls="site-menu" hidden>Menu</button>
<nav aria-label="Main">
<ul id="site-menu">
<li><a href="/products">Products</a></li>
<li><a href="/pricing">Pricing</a></li>
</ul>
</nav>Without JavaScript, the links show as a plain list and the button never appears. With JavaScript, the page upgrades into a hamburger menu. This approach is called progressive enhancement.
Source order, focus order, and the skip link
Keyboard users move through links in the order they appear in the HTML, not in the order they appear on screen. This has two consequences for navigation.
First, avoid using CSS (order, absolute positioning, flex-direction: row-reverse) to move links to a position that disagrees with the source order. A tab bar fixed to the bottom of the screen is fine to place last in the source. It is a footer-like control, and the visual position matches. Problems arise when the visual order and tab order contradict each other, because focus appears to jump around the screen.
Second, a long navigation placed before the content forces keyboard users to tab through every link on every page. A skip link solves this. It is the first focusable element on the page, and it jumps over the navigation to the main content:
<body>
<a class="skip-link" href="#main">Skip to main content</a>
<header>
<nav aria-label="Main">
<ul>
<li><a href="/products">Products</a></li>
<li><a href="/pricing">Pricing</a></li>
</ul>
</nav>
</header>
<main id="main" tabindex="-1">
<h1>Pricing</h1>
</main>
<style>
.skip-link {
position: absolute;
left: 0;
top: -3rem;
padding: 0.75rem 1rem;
background: #fff;
}
.skip-link:focus {
top: 0;
}
</style>
</body>The link sits off screen until it receives focus, then slides into view. The tabindex="-1" on main lets it receive focus when the link is activated, so that the next Tab press continues from the content instead of from the top of the page. On a phone with a hamburger menu the navigation is collapsed, so a skip link matters less there, but it still helps anyone using a keyboard or switch device on a small screen.
Hiding without breaking: hidden, display, and visibility
Collapsed menus must be hidden from everyone, not only from sighted users. If a closed menu is merely moved off screen, keyboard users still tab into invisible links.
| Technique | Visible | In tab order | Read by screen readers |
|---|---|---|---|
| `hidden` attribute | No | No | No |
| `display: none` | No | No | No |
| `visibility: hidden` | No | No | No |
| `opacity: 0` | No | Yes | Yes |
| `left: -9999px` | No | Yes | Yes |
For a closed menu, use hidden or display: none. Avoid opacity: 0 and off-screen positioning for closed menus. They hide the menu visually but leave its links reachable. The skip link above uses off-screen positioning on purpose, because it must stay focusable.
One consequence for CSS: the hidden attribute is only a default display: none rule from the browser. Any author rule that sets display on the same element, such as display: flex, overrides it. That is why the desktop styles in the hamburger example need the selector #site-menu[hidden]. Remember this when something stays visible after you set hidden.
A checklist for the structural layer
Before you add a pattern, check the markup against this list:
- The navigation is inside
<nav>, and each nav has a unique, shortaria-label. - Links are in a
ul(orolfor ordered trails such as breadcrumbs). - Every destination is an
<a>with a realhref. - Every control that changes page state is a
<button type="button">. - The current page is marked with
aria-current="page". - The markup works, as a plain list of links, with CSS and JavaScript turned off.
- Source order matches the visual reading order.
Exercise
A teammate builds a mobile menu where each item is <li class="item" onclick="go('/pricing')">Pricing</li> and the open/close control is <a href="#" id="menu">Menu</a>. Name three problems and give the corrected markup for the control and one item.
Show answer
First, the item is not a link, so it cannot be focused with the keyboard, is not announced as a link, and does nothing without JavaScript. Second, the control is an <a href="#"> that acts as a button, so it jumps to the top of the page and is announced as a link instead of a button. Third, the list has no nav landmark or label, so screen reader users cannot find it. Corrected: <button type="button" id="menu" aria-expanded="false" aria-controls="site-menu">Menu</button> and <li><a href="/pricing">Pricing</a></li>, with the list wrapped in <nav aria-label="Main">.
Exercise
A page has a header navigation and a footer navigation, both written as <nav> with no attributes. What does a screen reader user hear in the landmark list, and how do you fix it?
Show answer
Both landmarks are announced simply as "navigation", so the user cannot tell which is which. Add distinct short labels, for example <nav aria-label="Main"> in the header and <nav aria-label="Footer"> in the footer. Do not include the word "navigation" in the labels, because the role is announced automatically.
Exercise
You set the hidden attribute on #site-menu, but on wide screens your CSS says #site-menu { display: flex; } and the menu is visible even when you do not want it to be. Why does this happen, and what is the safe pattern?
Show answer
The hidden attribute is implemented as a browser default style, display: none. An author rule that sets display on the same element overrides it. Use this on purpose where you want the menu visible on wide screens, as in #site-menu, #site-menu[hidden] { display: flex; } inside a media query. Where you want hidden to keep working, avoid setting display on that element outside the media query, or add [hidden] { display: none; } with enough specificity.