Sub topic 1
Start with semantic HTML: nav, ul, li and a
A navigation bar starts as markup, not styling. This chapter builds the HTML structure that every later chapter styles. You will see which elements to use, why each one is there, and the mistakes that cause trouble later.
Prerequisites
This chapter assumes you know:
- Basic HTML elements and attributes, such as how to write a link with
href. - Basic CSS selectors and properties. This chapter uses almost no CSS.
- The CSS box model (margin, padding, border). It matters in later chapters.
The working model
A navigation bar is a list of links. Browsers, screen readers and search engines understand lists and links without any help from CSS. If you choose the right elements first, the bar works before you style it.
Four elements do the job:
| Element | Role in the bar |
|---|---|
| `<nav>` | Landmark that tells software "this is a block of navigation links" |
| `<ul>` | Unordered list that groups the links as one set |
| `<li>` | One list item per destination |
| `<a>` | The link itself, with an `href` |
The structure nests in that order: nav contains ul, ul contains li, and each li contains one a.
Write the markup
This is the bar you will use for the rest of the tutorial.
<nav aria-label="Main">
<ul class="nav-list" id="nav-list">
<li><a href="/" aria-current="page">Home</a></li>
<li><a href="/products">Products</a></li>
<li><a href="/about">About</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
</nav>Go through it piece by piece.
The nav element
<nav> is a landmark. Screen reader users can list all landmarks on a page and jump straight to the navigation. A <div> has no such meaning.
The aria-label="Main" attribute gives the landmark a name. A page often has more than one nav, for example a main menu and a footer menu. Labels let a user tell them apart. The screen reader announces something like "Main navigation".
Not every group of links needs a nav. Use it for major blocks of navigation, such as the main menu, a table of contents or pagination. A few links inside an article do not need one.
The ul and li elements
A list tells assistive technology how many items there are. A screen reader can say "list, 4 items" and "2 of 4" as the user moves through it. That context helps people orient themselves, and a row of bare links gives none of it.
An unordered list fits because the order of menu items is a design choice and carries no numbered sequence. Every child of a ul must be an li. Putting an a directly inside ul is invalid HTML.
The class and id on the ul are hooks for later chapters. The class is for CSS. The id is what the mobile toggle button will point to.
The a element
Each li holds one link. Use real href values so links work with the keyboard, open in a new tab on middle-click, and can be followed by search engines.
The aria-current="page" attribute marks the link for the page the reader is on. A later chapter covers it in detail. For now, note that it is a normal attribute on the link and that only one link should carry it.
Common errors
- Using
divorspaninstead of list elements. The page looks the same once styled, but the structure is lost for assistive technology. - Using
<a>withouthref. Withouthref, the element is not focusable and is not announced as a link. - Using
onclickon aliinstead of a link. Keyboard users cannot reach it. - Wrapping the whole page header in
nav. Put the logo and page title outside thenavunless they are part of the navigation. - Placing an
adirectly insideul. Always wrap it in anli. - Skipping
aria-labelwhen there are severalnavelements. The landmarks then have identical names.
Check your markup
Open the page in a browser with no stylesheet. You should see a bulleted list of four links. Press Tab. Focus should move through the links in the order they appear on screen. If it does not, the markup has a problem to fix before you write any CSS.
Exercise
A teammate wrote the menu as <nav><a href="/">Home</a><a href="/about">About</a></nav> inside a div with no list. Name two benefits of wrapping the links in ul and li, and say what attribute you would add to nav if the page also had a footer menu.
Show answer
A list lets screen readers announce the number of items and the position within the list, and it groups the links as one set that CSS can style as a unit. The layout work in later chapters also depends on having a list element to target. With two menus on the page, add an aria-label to each nav (for example "Main" and "Footer") so the landmarks have different names.
Exercise
What is wrong with this item: <ul><a href="/contact">Contact</a></ul>?
Show answer
An a cannot be a direct child of ul. Every child of a ul must be an li, so write <ul><li><a href="/contact">Contact</a></li></ul>.