Sub topic 3
Start with semantic nav markup
This chapter assumes you know basic HTML structure, can write flexbox layouts and media queries, and can select elements and handle events in JavaScript. Before building a hamburger menu, a tab bar, or a priority menu, you need markup that works without any of them. All three patterns are presentation and behavior layered on top of the same foundation: a real nav element holding a real list of links.
Why markup comes first
Every navigation pattern in this tutorial changes how links are shown, not what they are. If the underlying HTML is a pile of div elements with click handlers, each pattern has to rebuild what the browser already provides. If the HTML is a nav containing a list of a elements, you get these things for free:
- Links are focusable with the Tab key and activate with Enter.
- Screen readers announce a navigation landmark, and users can jump straight to it.
- Search engines and crawlers can follow the links.
- The links still work if your JavaScript fails to load.
Think of the markup as the contract. Patterns can hide it, move it, or collapse part of it. They should never replace it.
The building blocks
A solid navigation block uses four elements.
| Element | Role in navigation |
|---|---|
| `nav` | Marks a block of major navigation links as a landmark. |
| `ul` and `li` | Group the links as a list, so assistive technology can announce "list, 4 items". |
| `a` with `href` | Moves the user to another page or section. |
| `button` | Performs an action on the current page, such as opening a menu. |
The a versus button distinction matters later. A link goes somewhere. A button does something. The toggle that opens a hamburger menu is a button. The items inside the menu are links.
A minimal example
This is a complete, working navigation. It needs no CSS or JavaScript to be usable.
<header>
<a href="/">Site name</a>
<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>
</header>On a phone this renders as a plain vertical list of links. It is not pretty, but every link is reachable. The next chapters start from this baseline and add the patterns.
Labeling landmarks
A page often has more than one nav. A site might have a main menu, a footer menu, and a breadcrumb trail. Screen readers list landmarks by role, so several unlabeled "navigation" entries are indistinguishable.
Give each nav a distinct aria-label. The label should name the purpose and should not repeat the word "navigation", because the screen reader already announces the role. Write aria-label="Main", which is announced as "Main, navigation". Writing aria-label="Main navigation" produces "Main navigation, navigation".
<nav aria-label="Main">…</nav>
<nav aria-label="Breadcrumb">…</nav>
<nav aria-label="Footer">…</nav>Not every group of links needs to be a nav. Reserve it for major navigation blocks. Marking up every small link list as a landmark makes the landmark list long and less useful.
Marking the current page
Users need to know where they are. The aria-current="page" attribute tells assistive technology which link points to the current page. You can also use it as a styling hook.
<nav aria-label="Main">
<ul>
<li><a href="/products" aria-current="page">Products</a></li>
<li><a href="/pricing">Pricing</a></li>
</ul>
</nav>nav a[aria-current="page"] {
font-weight: bold;
}Styling from the attribute keeps the visual state and the announced state in sync, because they come from one source.
Common errors
- Using
divorspanwithonclickinstead ofa. These elements are not focusable and are not announced as links. Usea href. - Using
awithouthref, orhref="#"for actions. An anchor with nohrefis not keyboard focusable. If the control opens a menu, use abutton. - Putting links outside a list. Wrapping them in
ulandligives screen reader users the item count and a way to skip the list. - Leaving several
navelements unlabeled. Landmarks become indistinguishable. - Marking the current page by color alone. Color is invisible to a screen reader and can be missed by users with color vision deficiency. Add
aria-currentand, ideally, a second visual cue such as weight or an underline.
Where this leads
The hamburger menu will add a button and the hidden attribute to this structure. The tab bar will reuse the same nav and list of links with different CSS. The priority menu will move list items between two lists. In every case the links stay real links inside a labeled nav.
Exercise
A teammate builds a site header with a div class="menu" containing span elements that open pages through click handlers. Name three problems for keyboard and screen reader users, and describe the markup you would use instead.
Show answer
The span elements cannot receive focus, so keyboard users cannot reach them. Screen readers do not announce them as links or as a navigation landmark, so users cannot jump to the menu or know what the items are. Without href values, the destinations are not real URLs, so the menu fails when JavaScript fails and crawlers cannot follow it. Replace it with <nav aria-label="Main"><ul><li><a href="...">...</a></li></ul></nav>, using one li and one a href per destination.
Exercise
A page has a main menu and a footer menu, both inside nav elements with no other attributes. What should you add, and why should the label not be "Main navigation"?
Show answer
Add a distinct aria-label to each nav, such as aria-label="Main" and aria-label="Footer", so screen reader users can tell the two landmarks apart. The label should omit the word "navigation" because screen readers already announce the landmark role, so "Main navigation" would be read as "Main navigation, navigation".