Sub topic 14
A process for choosing the right element
This chapter assumes you know basic HTML syntax (tags, attributes, nesting), have used a browser's developer tools, and have read the earlier chapters on landmarks, headings, sectioning, text-level elements, tables, forms and interactive elements. Here you will combine them into one repeatable decision process.
Why a process helps
Most semantic mistakes come from choosing an element by how it looks. A bold line becomes a <b>, a clickable box becomes a <div>, and an indented block becomes a <blockquote>. Appearance is CSS's job. An element's job is to say what the content is and what it does.
A process makes you ask the same questions in the same order. It also gives a team shared reasons to cite in code review, such as "this is a list of links, so it needs ul," instead of "I prefer it this way."
The decision process
Work through these steps for each piece of content. Stop at the first step that gives a clear answer.
- Does the content have a native role? Ask what the content is: navigation, a heading, a list, a table, a quotation, a form control or a button.
- Is it interactive? If the user can activate it, type into it or toggle it, start from interactive elements.
- Is it structural? If it organizes the page or a part of it, consider landmarks and sectioning elements.
- Is it text with special meaning? If it is a phrase inside a paragraph, consider text-level elements.
- Is it only a styling or scripting hook? Only then use
divorspan. - Does native HTML fall short? Add ARIA only for a gap that no element can fill.
The order matters. Generic containers come last because they carry no meaning, and ARIA comes after them because it only adjusts what the browser already exposes.
Step 1: Name the content
Describe the content in one plain phrase before you write any tag. The phrase usually points to the element.
| What you would say | Element |
|---|---|
| "A group of links to other pages on this site" | `nav` containing a `ul` of `a` elements |
| "The title of this part of the page" | `h1` to `h6` |
| "Steps to follow in order" | `ol` |
| "Terms and their definitions" | `dl` with `dt` and `dd` |
| "A picture with a caption" | `figure` and `figcaption` |
| "Words quoted from another source" | `blockquote` or `q` |
| "A date or time" | `time` with `datetime` |
| "Rows and columns of related values" | `table` |
| "Something I press to run an action" | `button` |
| "Something I follow to go elsewhere" | `a` with `href` |
If your phrase is only visual, such as "a grey box with rounded corners," you have not named the content yet. Ask what the box holds.
Step 2: Separate actions from navigation
The most common fork in the process is between button and a. Use the question: what happens when the user activates it?
- If it moves to another URL or another place in the page, use
awith anhref. - If it runs code, submits a form or changes something on the current page, use
button.
<!-- Navigates: a link -->
<a href="/pricing">See pricing</a>
<!-- Acts on the current page: a button -->
<button type="button" id="theme-toggle" aria-pressed="false">Dark theme</button>
<!-- Submits a form: a button inside the form -->
<form action="/search" method="get">
<label for="q">Search</label>
<input id="q" name="q" type="search">
<button type="submit">Search</button>
</form>The first two differ in more than looks. A link can be opened in a new tab, bookmarked and copied. A button cannot. Screen reader users also hear "link" or "button" and expect each to behave that way.
Step 3: Pick structure from the page outward
For structure, work from the outside in. First assign the page landmarks, then the headings, then the sectioning elements inside main.
<body>
<header>
<a href="/">Example Site</a>
<nav aria-label="Primary">
<ul>
<li><a href="/articles">Articles</a></li>
<li><a href="/about">About</a></li>
</ul>
</nav>
</header>
<main>
<h1>Semantic markup guide</h1>
<article>
<h2>Choosing elements</h2>
<p>Describe the content first, then pick the tag.</p>
</article>
<section aria-labelledby="related-heading">
<h2 id="related-heading">Related reading</h2>
<ul>
<li><a href="/aria">ARIA basics</a></li>
</ul>
</section>
</main>
<footer>
<p>© Example Site</p>
</footer>
</body>Use these tests as you go:
article: Could this block be syndicated or reused on its own and still make sense? A blog post, comment or product card passes.section: Is it a themed group that has its own heading? If there is no natural heading, you probably want adiv.aside: Is it tangentially related to the content around it? A pull quote or related links pass.nav: Is it a major block of navigation? Not every group of links needsnav. A short list of links in a paragraph does not.
Step 4: Mark meaning inside text
Inside a sentence, choose by the reason for the emphasis, not by the visual result.
| Reason | Element | Not this |
|---|---|---|
| Serious importance or urgency | `strong` | `b` for the bold look |
| Stress that changes the sentence meaning | `em` | `i` for the italic look |
| Highlight relevant to the reader's current task | `mark` | A yellow `span` |
| Computer code or a file name | `code` | A monospace `span` |
| Abbreviation with an expansion | `abbr` with `title` | Plain text |
| A date or time | `time` | Plain text |
b and i are still valid when no stronger meaning applies. Examples are a product name in a style guide or a foreign phrase. Use them deliberately, and not as shortcuts for strong and em.
Step 5: Keep generic containers honest
Use div and span when no element describes the content and you need a hook for layout or script. A wrapper that centers a column is a good use of div. A div that acts as a button, heading or list is a missed element.
<!-- Avoid -->
<div class="title">Latest articles</div>
<div class="item">First article</div>
<div class="item">Second article</div>
<!-- Prefer -->
<h2>Latest articles</h2>
<ul>
<li>First article</li>
<li>Second article</li>
</ul>Step 6: Reach for ARIA last
If you have worked through the steps and a native element still cannot do the job, add ARIA. Typical cases are a live region that announces updates, a custom widget with no native equivalent, or labeling for a landmark that appears twice. Follow the native-first rule:
- Use a native element if one exists.
- If you must adapt an element, add the minimum ARIA needed.
- If you add a role, you also take on its keyboard behavior and state attributes.
Worked example
Suppose you receive this markup and need to improve it.
<div class="top">
<div class="logo">Recipes</div>
<div class="links">
<span onclick="go('/home')">Home</span>
<span onclick="go('/saved')">Saved</span>
</div>
</div>
<div class="content">
<div class="big">Pancakes</div>
<div>Serves <b>4</b>. Ready in <span>20 minutes</span>.</div>
<div class="step">Mix the flour and milk.</div>
<div class="step">Fry in a hot pan.</div>
<div class="btn" onclick="save()">Save recipe</div>
</div>Apply the questions in turn:
- The top block is a page header with site navigation. Its links go to other URLs, so they are
aelements inside anav. - "Pancakes" is the title of the page, so it is an
h1. - The steps are in order, so they form an
ol. - "20 minutes" is a duration, so it can use
timewith a machine-readable value. - "Save recipe" runs code, so it is a
button.
<header>
<a href="/">Recipes</a>
<nav aria-label="Primary">
<ul>
<li><a href="/home">Home</a></li>
<li><a href="/saved">Saved</a></li>
</ul>
</nav>
</header>
<main>
<h1>Pancakes</h1>
<p>Serves 4. Ready in <time datetime="PT20M">20 minutes</time>.</p>
<ol>
<li>Mix the flour and milk.</li>
<li>Fry in a hot pan.</li>
</ol>
<button type="button" onclick="save()">Save recipe</button>
</main>The new version needs no ARIA. It is keyboard operable and exposes landmarks, a heading, a list and a button. The onclick handlers on the old links also failed in other ways: they could not be focused by keyboard, and they gave no link semantics.
Edge cases
- A link that looks like a button. Keep it an
aif it navigates, and style it with CSS. - A button that looks like a link. Keep it a
buttonif it acts, and remove the default button styling. - Content that fits two elements. A product card is both an
articleand a list item. Nest them:licontainingarticle. - No suitable element. A tab widget has no native element. Use the ARIA tabs pattern, and implement the arrow-key behavior it requires.
- Styling changes the semantics. CSS such as
display: contentsorlist-style: nonecan alter how some browsers expose elements. Check the result in the accessibility tree instead of assuming.
Exercise
A "Delete account" control opens a confirmation dialog on the same page and changes nothing in the URL. A developer wrote <a href="#" onclick="openConfirm()">Delete account</a>. Which element should replace the a, and why?
Show answer
Use <button type="button">Delete account</button>. The control runs code on the current page and does not navigate, so it is an action and not a link. A button is focusable, works with Enter and Space, and is announced as a button. The href="#" link also adds a pointless URL fragment and invites open-in-new-tab behavior that makes no sense.
Exercise
Choose an element for each of these: (a) a group of five links to the main sections of a site, (b) "Total: 42" shown beside a date in a receipt, (c) a wrapper whose only purpose is to apply a flexbox layout.
Show answer
(a) nav containing a ul of a elements, because it is a major block of site navigation. (b) The date uses time with a datetime value, and the total is plain text in a p, or data with a value attribute if a machine-readable number is useful. (c) div, because no element describes the content and the wrapper exists only for layout.