Sub topic 18
Building a navigation menu from a list
This chapter assumes you can write a elements with href, and that you know ul and li for lists, including nesting. Here you combine them into the standard site menu.
Why a list?
A navigation menu is a set of related links, so it is a list. Marking it up as one lets screen readers announce "list, 3 items" and let users skip it. Wrap the list in nav so assistive technology can find the menu as a landmark.
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>navmarks the block as major navigation.aria-label="Main"names the landmark. This matters when a page has more than onenav, such as a main menu and a footer menu. Screen readers do not repeat the word "navigation" for you, so use "Main", not "Main navigation".aria-current="page"goes on the link for the current page. Screen readers announce it, and you can style it with the selector[aria-current="page"].
Adding a submenu
A submenu is a nested list. It goes inside the li of its parent item, as in the nested-lists chapter.
<nav aria-label="Main">
<ul>
<li><a href="/">Home</a></li>
<li>
<a href="/articles/">Articles</a>
<ul>
<li><a href="/articles/html.html">HTML</a></li>
<li><a href="/articles/css.html">CSS</a></li>
</ul>
</li>
</ul>
</nav>Making it look like a menu
The list is the structure. CSS turns it into a horizontal bar. This page runs as shown.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Menu demo</title>
<style>
nav ul { list-style: none; margin: 0; padding: 0; display: flex; gap: 1rem; }
nav a[aria-current="page"] { font-weight: bold; text-decoration: none; }
</style>
</head>
<body>
<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>
</body>
</html>Common errors
- Putting
aelements directly insideul. Onlyliis allowed as a child oful. - Placing a nested
ulbeside the parentliinstead of inside it. - Using
divorbrinstead of a list, which removes the item count for screen reader users. - Setting
list-style: noneand then forgetting that some screen readers may stop announcing it as a list. Thenavlandmark and the links still work, so this is acceptable here. - Marking several links with
aria-current="page". Use it on one link per menu. - Using
navfor every group of links. Reserve it for major navigation blocks.
Exercise
A page has a main menu at the top and a second menu of legal links in the footer. How do you mark both up so a screen reader user can tell them apart, and where do you put the nested submenu for an item called "Help"?
Show answer
Give each menu its own nav element with a distinct aria-label, for example <nav aria-label="Main"> and <nav aria-label="Legal">. Each nav contains a ul with one li per link. The submenu for "Help" is a second ul placed inside the li that holds the "Help" link, after the a element.