Sub topic 18
Building a navigation menu from a list
This chapter assumes you can write a elements with href values, you know the difference between relative and root-relative paths, and you have written a basic ul with li items. Here you combine those pieces into a navigation menu, which nearly every website uses.
Why a menu is a list of links
A navigation menu is a set of related items with no meaningful order. That is the definition of an unordered list. Marking the menu up as a list gives you three things:
- Screen readers announce "list, 3 items" before reading the links, so users know how big the menu is.
- Each link sits in its own
li, which gives CSS one element per item to style, space, and lay out. - Without CSS, the menu still renders as a readable, clickable list.
You wrap the list in a nav element. nav is a landmark: assistive technology can list it and let users jump straight to it.
The basic pattern
<nav aria-label="Main">
<ul>
<li><a href="/" aria-current="page">Home</a></li>
<li><a href="/articles/">Articles</a></li>
<li><a href="/contact.html">Contact</a></li>
</ul>
</nav>The structure has three nested layers, shown below.
Each part has a job:
navmarks the region as a major navigation block.ulgroups the items.liholds exactly one link. Onlylielements may be direct children of aul. Putting anastraight inside theulis invalid.acarries the destination inhref.
Root-relative links in menus
The example uses root-relative paths such as /articles/. A menu is usually copied onto every page, and pages live in different folders. A relative link such as articles/ would point to a different place on a page inside /docs/. A root-relative link resolves the same way on every page of the site.
Labelling with aria-label
A page often has more than one nav, for example a main menu in the header and a menu of links in the footer. If they have no names, a screen reader user hears "navigation" twice and cannot tell them apart. Give each one a short aria-label:
<nav aria-label="Main">
<!-- main menu list -->
</nav>
<nav aria-label="Footer">
<!-- footer menu list -->
</nav>Do not include the word "navigation" in the label. The screen reader already says "navigation", so "Main navigation" is read as "Main navigation navigation".
If a page has only one nav, the label is optional but harmless.
Marking the current page
Visitors need to know where they are. The aria-current="page" attribute marks the link that points to the page being shown:
<li><a href="/articles/" aria-current="page">Articles</a></li>Assistive technology announces this link as "current page". You can also select it in CSS with a[aria-current="page"], so one attribute drives both the spoken and the visual indicator. Set it on at most one link per menu. Colour alone is a poor indicator, because some users cannot distinguish colours. Pair it with a second cue such as bold text or an underline.
Making the list look like a menu
By default, a ul shows bullets and indents its items vertically. A horizontal menu needs a little CSS:
<style>
nav ul {
list-style: none;
margin: 0;
padding: 0;
display: flex;
gap: 1rem;
}
nav a {
text-decoration: none;
padding: 0.5rem 0.75rem;
}
nav a[aria-current="page"] {
font-weight: bold;
border-bottom: 2px solid currentColor;
}
</style>
<nav aria-label="Main">
<ul>
<li><a href="/" aria-current="page">Home</a></li>
<li><a href="/articles/">Articles</a></li>
<li><a href="/contact.html">Contact</a></li>
</ul>
</nav>display: flex on the ul puts the li items in a row, and gap spaces them. The padding goes on the a, not the li, so the whole padded area is clickable.
A known gotcha with list-style: none
Some browsers, notably Safari with VoiceOver, stop announcing an element as a list when list-style: none is applied to it. The result is that the "list, 3 items" announcement disappears. The markup is still correct, and other screen reader and browser pairs behave normally. If you care about that case, add role="list" to the ul. Keep it in mind as a trade-off rather than a rule: the extra attribute restores the announcement, and it does no harm elsewhere.
<ul role="list">
<li><a href="/">Home</a></li>
</ul>Submenus with nested lists
A site with sections can nest a list inside a menu item. As you learned for nested lists, the inner ul goes inside the parent li, not directly inside the outer ul.
<nav aria-label="Main">
<ul>
<li><a href="/">Home</a></li>
<li>
<a href="/guides/">Guides</a>
<ul>
<li><a href="/guides/links.html">Links</a></li>
<li><a href="/guides/images.html">Images</a></li>
<li><a href="/guides/lists.html">Lists</a></li>
</ul>
</li>
<li><a href="/contact.html">Contact</a></li>
</ul>
</nav>Keep the parent link pointing to a real page. If it does nothing and only toggles the submenu, it should be a button, not a link, because links navigate and buttons act. Building a toggle needs JavaScript and ARIA state attributes, which are outside this chapter. Shallow menus, one or two levels deep, work best for keyboard and touch users.
Common errors
| Mistake | Problem | Fix |
|---|---|---|
| `a` placed directly inside `ul` | Invalid HTML, and the list structure may be lost | Wrap each link in an `li` |
| Inner `ul` placed beside the parent `li` | Invalid nesting | Put the inner `ul` inside the parent `li` |
| Two unlabelled `nav` elements | Users cannot tell the landmarks apart | Add a distinct `aria-label` to each |
| Relative `href` values in a shared menu | Links break on pages in other folders | Use root-relative or absolute URLs |
| Current page shown by colour only | Invisible to some users | Add `aria-current="page"` and a non-colour cue |
| `nav` around every group of links | Too many landmarks to navigate | Use `nav` for major blocks only |
That last row matters. A short list of links in the middle of an article does not need nav. A plain ul is enough.
Putting it together
A complete page combines the pieces from this tutorial: a nav with a list of links, an image used as a linked logo, and fragment links inside the page.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example Co.</title>
</head>
<body>
<header>
<a href="/">
<img src="/images/logo.svg" alt="Example Co. home" width="120" height="40">
</a>
<nav aria-label="Main">
<ul>
<li><a href="/" aria-current="page">Home</a></li>
<li><a href="/articles/">Articles</a></li>
<li><a href="/contact.html">Contact</a></li>
</ul>
</nav>
</header>
<main>
<h1>Welcome</h1>
</main>
</body>
</html>Exercise
A page has a main menu in the header and a list of legal links (Privacy, Terms) in the footer. Write the footer markup so a screen reader user can tell the two navigation landmarks apart, and explain why each link is inside an li.
Show answer
Use a second nav with its own label, for example <nav aria-label="Footer"><ul><li><a href="/privacy.html">Privacy</a></li><li><a href="/terms.html">Terms</a></li></ul></nav>. The label "Footer" differs from the header's "Main", so the landmarks have distinct names. Each link is inside an li because a ul may only contain li elements as direct children, and the list structure lets assistive technology announce the number of items.
Exercise
You have a "Guides" menu item with three sub-pages. Where does the inner ul go, and why should the parent "Guides" link still have a real href?
Show answer
The inner ul goes inside the "Guides" li, after its a element, not directly inside the outer ul. The parent link needs a real href so that it navigates somewhere and works without JavaScript. If the item only toggles a submenu, it should be a button instead of a link.