Choosing Elements by Meaning, Not Looks
Decide between the elements that look similar — link or button, list or table, figure or plain image, disclosure or dialog — by asking what the content is, not how it should look.
Contentsand lesson path
On this page
- Why it matters
- Action or destination: link or button
- Lists, definitions and real tables
- Content that travels together
- Check your understanding
- Apply it
- Review prompt
Lessons of Semantic HTML
- Anatomy of an HTML Document Concept · 15 min
- Headings, Landmarks and the Page Outline Worked example · 20 min
- Choosing Elements by Meaning, Not Looks Concept · 20 min
- Alternatives and Labels for Images, Media and Inputs Concept · 20 min
- Lab: Mark Up an Article Page and Inspect Its Accessibility Tree Lab · 30 min
- Check: Spot the Semantic Errors Knowledge check · 8 questions
What you will learn
- Choose the semantic element for each piece of content (links vs buttons, lists, data tables with headers, figures, dialog, details) instead of generic div and span.
01. Why it matters
HTML has one element for "a generic container" (div) and one for "generic inline text" (span). They are the last resort, not the default. Every other element is a claim about what the content is: this is a link, this is a quotation, this is tabular data, this is a list of steps. Browsers pay those claims out in behaviour — keyboard support, screen-reader roles, default styling, error recovery — and you inherit all of it the moment you pick the right element.
Pick a generic element instead and you owe that behaviour back in JavaScript and ARIA attributes, hand-written and easy to get subtly wrong. The honest test for any element choice is not "what should this look like?" but "what is this, and what would a person using only a keyboard or a screen reader need it to do?"
The reward for getting this right is that the meaning survives restyling. A redesign changes every class name; the elements stay.
02. Action or destination: link or button
The most common everyday decision. A link (a with an href) goes somewhere: another page, another section of this page, a file. A button does something on this page: submit a form, open a panel, play audio, confirm a choice. Users act on the difference — links open in new tabs, can be bookmarked, and are announced with "link"; buttons respond to Space as well as Enter, and are announced with "button".
<p>
<button type="button" id="reserve">Reserve a place</button>
</p>
<p>
Read the <a href="/trips/faq.html">booking FAQ</a> before you pay.
</p>Two habits to keep. Always give buttons an explicit type — button, submit or reset — because the default inside a form is submit, and a stray "Add to basket" button can send the whole form. And never fake either half of the pair: an a with href="#" and a click handler announces a destination it never reaches, while a clickable div announces nothing at all and swallows Enter, Space and focus until you rebuild them by hand.
03. Lists, definitions and real tables
Three list elements cover most structured content. ul for items where order does not matter, ol for sequences where it does, and dl for pairs of terms and descriptions. A run of paragraphs separated by line breaks is usually a list in disguise; a list is what a screen reader announces as "list, three items", letting users skip the rest.
A table is for data with a two-dimensional relationship: each cell is meaningful along both its row and its column. Real header cells — th with scope — are what make that relationship available to software; bold td cells make it available only to eyes. A caption names what the table shows.
<table>
<caption>Group size against ticket price</caption>
<thead>
<tr>
<th scope="col">Group size</th>
<th scope="col">Price</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Up to 15</th>
<td>£45</td>
</tr>
<tr>
<th scope="row">16 to 30</th>
<td>£80</td>
</tr>
</tbody>
</table>With real header cells, a screen reader on the £80 cell can announce "16 to 30, Price, £80" — the cell plus its two headers. Without them (bold td instead of th), the user hears only "£80" and has to count columns by hand. scope makes the direction of each header explicit: screen readers can usually work it out in a simple table like this one, but not in irregular ones, so state it. The same duty runs in reverse: if the content is really a sequence of events, use ol and do not force it into a grid; if it is page layout, use CSS, not table markup.
04. Content that travels together
Some elements bundle content with its explanation. A figure wraps an image, chart or code listing together with its figcaption; the caption belongs to the figure, so it moves with it and is announced with it. This is not the same job as alt text, which replaces the image with a description; the caption reads alongside it.
A details element with a summary child is a disclosure widget the browser gives you for free: closed until clicked, keyboard-operable, no JavaScript. Use it for optional content that most readers will skip. And dialog, opened from script with showModal(), is the platform's modal: it renders in the top layer above the rest of the page, makes the page behind it unclickable, closes on Escape, and hands focus back where it came from when it closes.
<details>
<summary>What if it rains?</summary>
<p>Trips run in light rain; we refund if the harbour closes.</p>
</details>
<figure>
<img src="fossil-wall.jpg"
alt="A wall of ammonite fossils in the museum atrium">
<figcaption>The fossil wall on level 1, photographed in spring.</figcaption>
</figure>
<dialog id="confirm">
<form method="dialog">
<p>Reserve your place on the fossil walk?</p>
<button type="submit" value="cancel">Cancel</button>
<button type="submit" value="ok">Reserve</button>
</form>
</dialog>The form method="dialog" inside the dialog is a small platform feature: submitting it closes the dialog instead of sending a request, and the value of the button that was pressed becomes dialog.returnValue, so your script can tell Cancel from Reserve. The script below opens it from the Reserve button shown earlier:
const dialog = document.getElementById('confirm');
document.getElementById('reserve').addEventListener('click', () => {
dialog.showModal();
});Checked in Chromium 156 (driven by Playwright 1.64.1): after showModal(), focus moves into the dialog, a forced click on a control behind it does nothing, and after Escape (or dialog.close()), focus returns to the button that opened it. dialog.show() is the non-modal twin — the dialog opens without any of that blocking — so it is the wrong choice for confirmations. Building the same behaviour on a div means reimplementing focus trapping, Escape handling and focus restoration yourself, which is exactly the kind of code that quietly breaks.
const all = (s) => [...document.querySelectorAll(s)];
console.log('links:', all('a[href]').length);
console.log('buttons:', all('button').length);
console.log('button types:', all('button').map((b) => b.type).join(', '));
const details = document.querySelector('details');
console.log('summary text:', details.querySelector('summary').textContent);
console.log('details closed by default:', !details.open);
console.log('table caption:', document.querySelector('caption').textContent);
const scoped = all('th[scope]').map((th) => `${th.textContent}=${th.scope}`);
console.log('header cells with scope:', scoped.join(', '));
console.log('figure caption:', document.querySelector('figcaption').textContent);
console.log('img alt:', document.querySelector('figure img').alt);links: 1
buttons: 3
button types: button, submit, submit
summary text: What if it rains?
details closed by default: true
table caption: Group size against ticket price
header cells with scope: Group size=col, Price=col, Up to 15=row, 16 to 30=row
figure caption: The fossil wall on level 1, photographed in spring.
img alt: A wall of ammonite fossils in the museum atriumPut the three HTML blocks of this lesson on one page, open it, and paste the snippet into the console. It reads back the facts the elements carry: one link, the Reserve button plus the dialog's two submit buttons (each with an explicit type), the disclosure closed by default, the caption and scoped headers on the table, the figure caption and the alt text on the image. Nothing here depends on styling — which is the point. The meaning lives in the elements, so any tool can read it.
05. Check your understanding
06. Apply it
Sweep a page you have written for generic elements and ask, for each one, what it is pretending to be. Replace clickable div elements with buttons, a href="#" fakes with buttons, paragraphs-in-a-row lists with ul, ol or dl, styled tables of div elements with a real table, and images with captions with figure. Keep the div and span that remain — they are legitimate where no meaning applies. Then validate with npx html-validate as in the lab, and reread the page through its elements alone: it should still make sense.
07. Review prompt
Without looking back: state the link-versus-button test in one sentence each, name the three list elements and when each applies, explain what scope on a th adds for a screen-reader user, and list what showModal() gives you that toggling a div does not.
References
Your progress
Reading state
Sign in to save that you finished reading this lesson.
Prove this skill
8 questions · about 6 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.