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 inspect a page with your browser's developer tools. It covers what belongs in a site header, how to mark it up, and the mistakes that most often break it.
What the header is for
A site header answers two questions as soon as a page loads: "Where am I?" and "Where can I go?" The first is identity. It is usually the site name or logo. The second is orientation. It is usually the main navigation.
Keep the header to those two jobs. Search boxes, a cart link, or a language switcher can fit, but a long banner or a promotional message belongs in the hero, which comes next in the page.
Marking up the header
Use the header element, not a div with a class named "header". The element tells browsers and assistive technology what the region is.
<header>
<a href="/" class="brand">Acme Bakery</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>Three details matter here:
- The site name is a link to
/. Visitors expect to click it to return home. - The navigation is a list inside
nav. Screen readers announce the number of items, which helps orientation. aria-current="page"marks the link for the page the visitor is on. Screen readers announce it, and you can style it with the attribute selector[aria-current="page"].
If the brand is an image, put the site name in the alt text, for example alt="Acme Bakery home". An empty alt would leave the link with no accessible name.
When header is a landmark
A header that is a direct child of body becomes a banner landmark. Screen reader users can jump straight to it. A header nested inside article, section, or main is not a banner. It only introduces that piece of content, such as a title and byline.
This gives you a simple rule. Use one top-level header per page. You can use many nested ones.
Laying out the header
A header usually puts the brand on the left and the links on the right. Flexbox suits this because it arranges items along one axis.
header {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
align-items: center;
gap: 1rem;
padding: 1rem 2rem;
}
header nav ul {
display: flex;
gap: 1.5rem;
list-style: none;
margin: 0;
padding: 0;
}
header a[aria-current="page"] {
font-weight: 700;
text-decoration: underline;
}justify-content: space-between pushes the brand and the nav to opposite ends. flex-wrap: wrap lets the nav drop below the brand on a narrow screen instead of overflowing. Later chapters cover fuller responsive behavior.
Common errors
- Using
divinstead ofheader. The page looks identical but has no banner landmark. - Several top-level headers. Only one
headershould be a direct child ofbody. - Putting the
h1in the header by habit. Theh1should name the page. On a home page that is usually the hero heading, so the brand stays a plain link. Repeating the site name as anh1on every page gives each page the same title. - An unnamed logo link. An image link with empty
alttext is announced as just "link". - A sticky header that hides content. A
position: stickyheader can cover a focused element when the visitor tabs through the page. Test by pressing Tab and watching for focus rings that disappear behind it.
Check your understanding
Exercise
A page has a top-level header with the site brand and navigation. Inside a blog post's article element, you add another header with the post title and date. How many banner landmarks does the page have, and why?
Show answer
One. Only the header that is a direct child of body is a banner landmark. A header inside an article just introduces that article and has no landmark role.
Exercise
Your brand is an image inside a link to the home page. What should its alt text be, and why?
Show answer
The site name, optionally with the destination, such as "Acme Bakery home". The alt text becomes the link's accessible name. If it is empty, screen reader users hear only "link".