Sub topic 4
The header: identity and orientation
This chapter assumes you know basic HTML tags and attributes, basic CSS selectors and properties, and how to open a page's developer tools. You will build the site header: the strip at the top of every page that tells visitors where they are and where they can go.
What the header is for
A header does two jobs.
- Identity. It shows whose site this is, usually as a name or logo. It links back to the home page.
- Orientation. It shows the main places a visitor can go, and which of them is the current one.
Anything else in the header costs attention. A search box or a cart link can earn its place. A promotional banner, a rotating announcement, and three levels of dropdown menus usually do not. Visitors see the header on every page, so keep it small and stable.
The header sits above the hero and the content sections. Its contents are the brand link and the primary navigation, with an optional utility area for items like search or an account link.
The header element and the banner landmark
The <header> element has two meanings, and the meaning depends on where you put it.
- As a direct child of
<body>, it is the page's banner landmark. Screen reader users can list landmarks and jump straight to it. - Inside
<article>,<section>,<aside>,<main>, or<nav>, it is only the introductory group for that element. It is not a landmark.
| Where the `<header>` sits | Landmark exposed | Typical content |
|---|---|---|
| Direct child of `<body>` | banner | Brand, primary navigation |
| Inside `<article>` | none | Article title, author, date |
| Inside `<section>` | none | Section heading and a short lead |
A page should have one banner. If you have a site header and also an article header, only the first is the banner, so you do not need to do anything extra. Do not add role="banner" to the <header> element. The browser already provides it. Do not add it to a <div> either, because a <header> does the same job with less markup.
Brand: the identity half
The brand is a link to the home page. It contains either text or an image.
<header>
<a class="brand" href="/">
<img src="/images/logo.svg" alt="Acme Bakery" width="160" height="40">
</a>
<nav aria-label="Primary">
<ul>
<li><a href="/menu">Menu</a></li>
<li><a href="/about" aria-current="page">About</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
</nav>
</header>A link takes its accessible name from its content. Here the link's name comes from the image's alt text, so a screen reader announces "Acme Bakery, link". Three details matter:
- Describe the destination, not the picture.
alt="Acme Bakery"is right.alt="Logo"andalt="Acme Bakery logo, click to go home"are not. Screen readers already announce that it is a link. - Never leave the link empty. An
<a>that wraps a CSS background image or an icon font has no name. Screen reader users hear "link" with no label. - Set
widthandheighton the image. The browser can then reserve space for the logo, so the header does not jump when the image arrives.
If the logo is plain text, skip the image and write the name inside the link, as the first skeleton in this tutorial did with <a href="/">Acme Bakery</a>. Text scales with the user's font settings and needs no alt.
Should the brand be the h1?
No. The <h1> belongs to the page's own subject. On the home page that is the hero heading, "Fresh bread, baked every morning". On the menu page it is "Menu". If the brand were the <h1>, every page would have the same top-level heading, and the outline would say nothing about what each page covers. The next chapter on the hero covers this in more detail. For now, use a link, not a heading, for the brand.
Orientation: navigation inside the header
The header usually holds the primary navigation. Three practices help:
- Use
<nav>with a list. Navigation is a list of links, so<ul>and<li>let screen readers announce "list, 3 items". - Give the nav an accessible name.
aria-label="Primary"separates it from a footer navigation, which you will label differently. Without names, a screen reader lists two landmarks called "navigation". - Mark the current page.
aria-current="page"tells assistive technology which link points at the page the visitor is viewing. You can also style it with an attribute selector, so the visual cue and the programmatic cue never drift apart.
header nav a[aria-current="page"] {
font-weight: 700;
text-decoration: underline;
text-underline-offset: 0.25em;
}Do not rely on colour alone for the current-page cue. Weight or an underline works for people who cannot distinguish the colours.
Not every link in the header needs to be in the <nav>. A login button or a search form is a utility. It can sit beside the nav inside the header. A search form can be its own landmark with role="search" on the <form>.
Laying out the header with flexbox
A header is a one-dimensional row: brand on the left, links on the right. Flexbox suits that. The page-level grid from the layout chapter gives the header its natural height, and flexbox arranges the contents inside it.
body {
margin: 0;
min-height: 100vh;
display: grid;
grid-template-rows: auto 1fr auto;
}
header {
display: flex;
justify-content: space-between;
align-items: center;
flex-wrap: wrap;
gap: 1rem;
padding: 1rem 2rem;
}
header nav ul {
display: flex;
gap: 1.5rem;
list-style: none;
margin: 0;
padding: 0;
}justify-content: space-between pushes the brand and nav to opposite ends. align-items: center aligns them vertically. flex-wrap: wrap is an addition to the earlier example. On a narrow screen the nav drops below the brand instead of overflowing sideways. gap sets spacing between wrapped rows as well as columns, so no margin hacks are needed.
For a header with three or more links, wrapping is enough on phones. Once you have five or more links or dropdowns, you need a different pattern, such as a disclosure button that shows and hides the list. That is beyond this chapter. Whatever pattern you pick, make sure the button is a real <button> with an accessible name and an aria-expanded state.
A header that stays in view
A sticky header keeps navigation reachable on long pages.
header {
position: sticky;
top: 0;
z-index: 10;
background: #fff;
}
html {
scroll-padding-top: 5rem;
}Two points trip people up.
- A background is required. A sticky header with a transparent background lets content show through it as you scroll.
- A sticky header covers things. When a visitor follows an in-page link such as
#pricing, or tabs to a link near the top of the viewport, the browser scrolls the target to the top edge, behind your header.scroll-padding-toponhtmltells the browser to leave that much room. Set it to at least the header's height. WCAG 2.2 requires that a focused element is not entirely hidden by author-created content, and a sticky header is the common way to fail that.
A tall sticky header also eats vertical space on phones in landscape. Keep it compact, or make it sticky only above a width breakpoint using a media query.
Skip link: the first thing in the body
Keyboard users tab through every link in the header before reaching the content. A skip link, placed as the first focusable element, lets them bypass it.
<body>
<a class="skip-link" href="#main">Skip to main content</a>
<header>
<nav aria-label="Primary">...</nav>
</header>
<main id="main">
...
</main>
</body>It goes before the <header>, not inside it, so it is the first stop on the first Tab press. The accessibility chapter covers its styling and testing in full.
Common errors
- A second banner by mistake. Putting a site-wide
<header>inside<main>or<div role="main">removes the banner landmark. Keep it a direct child of<body>. - A heading for the brand and another for the tagline. Headings in the header add noise to the outline. Use a link and a paragraph.
- Identical
aria-labelvalues. Two navs both named "Primary" are indistinguishable. - Hiding the nav with
display: noneand forgetting the replacement. If a small-screen layout hides the links, the visitor needs a working button to reveal them. - A fixed pixel height.
height: 80pxon the header breaks when text wraps or the user enlarges the font. Use padding and let the content decide the height.
You can check most of these in the browser's developer tools. The Accessibility panel shows the computed role (look for "banner") and the accessible name of the brand link and the nav.
Exercise
A page's brand link wraps a logo image with alt="Logo". The current page is "Menu" and the nav list is <li><a href="/menu">Menu</a></li>. Name two changes that improve the header for screen reader users, and give the attribute or value for each.
Show answer
First, change the image's alt text to describe the destination, for example alt="Acme Bakery", so the link is announced with the site name instead of "Logo". Second, add aria-current="page" to the Menu link so assistive technology can tell which link represents the current page. Optionally, add aria-label="Primary" to the <nav> so it is distinguishable from other navigation landmarks.
Exercise
You make the header position: sticky; top: 0 and now a link to #pricing scrolls so that the "Pricing" heading is hidden behind the header. Which CSS declaration fixes this, and on which element do you put it?
Show answer
Add scroll-padding-top to the html element, with a value at least as large as the header's height, for example html { scroll-padding-top: 5rem; }. The browser then leaves that much space above scroll targets and focused elements.