Sub topic 5
Navigation inside the header
This chapter assumes you know basic HTML tags and attributes, basic CSS selectors and properties, and how to inspect a page in your browser's developer tools. It covers the primary navigation inside the site header: how to mark it up, how to style it, and the errors that come up most often.
What the navigation does
Primary navigation is the set of links that lets a visitor move between the main areas of a site. It sits inside the header element because it answers the question "where can I go from here?" on every page.
Two elements do the work:
navmarks a block of major navigation links. Browsers and screen readers expose it as a navigation landmark.ulwithliitems lists the links. A list tells assistive technology how many links there are and lets users jump between them.
Not every group of links needs a nav. Use it for major blocks such as the primary menu, a footer menu, or a table of contents. A few links inside a paragraph stay as plain links.
The markup
This header from the page skeleton holds the site name and the primary navigation:
<header>
<a href="/">Acme Bakery</a>
<nav aria-label="Primary">
<ul>
<li><a href="/menu">Menu</a></li>
<li><a href="/about">About</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
</nav>
</header>Three details matter here:
- The site name is a link to
/. This is a convention visitors expect, and it gives them a way back to the start from any page. - The links are real
aelements withhrefvalues. They work with the keyboard, open in new tabs on request, and can be followed by search engines. - The
aria-label="Primary"attribute gives the landmark an accessible name. This matters once the page has more than onenav, for example one in the header and one in the footer. A screen reader then lists "Primary navigation" and "Footer navigation" instead of two identical entries.
Styling the navigation with flexbox
A list of links is stacked vertically by default. Flexbox turns it into a row, and it also places the site name and the menu at opposite ends of the header.
header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 1rem 2rem;
}
header nav ul {
display: flex;
gap: 1.5rem;
list-style: none;
margin: 0;
padding: 0;
}The first rule makes the header a flex container. space-between pushes the site name to the left and the nav to the right, and align-items: center lines them up vertically. The second rule makes the list itself a flex container, so the items sit in a row separated by gap. Removing list-style, margin, and padding strips the default bullets and indentation.
Setting list-style: none removes the bullets, and in some browsers it also stops screen readers from announcing the element as a list. If you want to keep that announcement, add role="list" to the ul.
How the parts relate
Common errors
| Error | What goes wrong | Fix |
|---|---|---|
| Links built from `div` or `span` with click handlers | Keyboard users cannot reach them, and screen readers do not announce them as links | Use `a` with an `href` |
| Several `nav` elements with no labels | Screen reader users cannot tell the landmarks apart | Add a distinct `aria-label` to each |
| `nav` wrapped around every group of links | The landmark list becomes noisy and loses value | Reserve `nav` for major blocks |
| No visible focus style on links | Keyboard users lose track of where they are | Keep the default outline, or replace it with a clear `:focus-visible` style |
| Removing the outline with `outline: none` and nothing in its place | Focus becomes invisible | Never remove it without a replacement |
Marking the current page
Visitors should be able to see which page they are on. Add aria-current="page" to the matching link, and style it:
<li><a href="/menu" aria-current="page">Menu</a></li>nav a[aria-current="page"] {
font-weight: 700;
text-decoration: underline;
}The attribute does two jobs at once. Screen readers announce the link as the current page, and your CSS selector uses the same attribute for the visual style, so the two cannot drift apart.
Check your understanding
Exercise
A page has a nav in the header and another nav in the footer. Both contain only the unlabelled markup <nav><ul>...</ul></nav>. What problem does this cause for screen reader users, and how do you fix it? Also name the attribute that marks the link for the page the visitor is currently on.
Show answer
A screen reader lists two navigation landmarks with the same name, so users cannot tell which is which. Give each a distinct accessible name, for example aria-label="Primary" on the header nav and aria-label="Footer" on the footer nav. The current page link is marked with aria-current="page".
Exercise
In the CSS above, the header uses justify-content: space-between. Which two flex items does it separate, and what would change if you removed that line?
Show answer
It separates the site name link and the nav element, which are the two direct children of the header. Without it, both would sit together at the left edge, because the default value is flex-start.