Sub topic 1
What 'semantic' means in HTML
Prerequisites
This chapter assumes you know basic HTML syntax: tags, attributes and nesting. You should be able to open your browser's developer tools and inspect an element. Knowing what CSS does helps, but you don't need to write any.
Meaning versus appearance
A semantic element describes what its content is. It does not describe how the content looks. A <button> is a control that triggers an action. A <nav> is a block of navigation links. Both have default styles, but those styles are a side effect, and CSS can replace them.
This gives you a working rule. HTML answers "what is this?" and CSS answers "how should it look?" If you pick an element because of its default look, you are mixing the two jobs.
Browsers, screen readers, search engines and other developers all read the same markup. Each one uses the element names to make decisions:
- Browsers add behavior, such as focus and keyboard activation for buttons.
- Assistive technology announces roles and offers shortcuts, such as jumping between headings.
- Search engines and reader modes find the main content and the title structure.
- Developers can read the structure of a page without decoding class names.
The same page, two ways
Both snippets below look identical once styled. Only the second tells software what each part is.
<!-- Generic: every part is an anonymous box -->
<div class="top">
<div class="title">Latest articles</div>
<div class="links">
<div class="link">Articles</div>
<div class="link">About</div>
</div>
</div><!-- Semantic: every part has a name and a role -->
<header>
<h1>Latest articles</h1>
<nav aria-label="Primary">
<ul>
<li><a href="/articles">Articles</a></li>
<li><a href="/about">About</a></li>
</ul>
</nav>
</header>In the first version, a screen reader user hears plain text with no structure. The "links" cannot be tabbed to or followed. In the second version, the heading, the navigation region and the links are all exposed, and the links work from the keyboard.
Semantics are not styling
A common error is to choose an element for its look. Headings are the usual case. An <h4> is small by default, so some authors use it for small text. Others skip from <h1> to <h4> to get a particular size. The element then claims a structure that does not exist.
The fix is to choose the element for its meaning and set the size in CSS:
<h2 class="small-title">Related reading</h2>
<style>
.small-title {
font-size: 1rem;
}
</style>The reverse also happens. A <blockquote> indents text, so it gets used for spacing. Use it only for actual quotations.
What you get for free
Native elements carry behavior that you would otherwise write by hand. Compare these two:
<!-- Avoid: no role, no keyboard support, not focusable -->
<div class="btn" onclick="save()">Save</div>
<!-- Prefer: focusable, keyboard operable, announced as a button -->
<button type="button" onclick="save()">Save</button>The <button> receives focus with Tab, activates with Enter and Space, and is announced as a button. The <div> has none of this. You would need tabindex, a role, and key handlers to match it.
Check it in devtools
Open your browser's developer tools and inspect the semantic header above. The accessibility pane shows the role the browser assigned to each element. A <nav> shows as a navigation landmark, and an <h1> shows as a heading of level 1. A <div> shows as generic. Later chapters use this view often.
Exercise
A teammate uses <h3> for a caption under a photo because it has the right font size. What is wrong with this, and what would you do instead?
Show answer
The <h3> tells browsers and assistive technology that a third-level section heading starts there, which is false and pollutes the heading outline. Use <figure> with <figcaption> for the caption, and set the font size with CSS.
Exercise
Name two things a native <button> does that a <div onclick="save()"> does not.
Show answer
Any two of: it can receive keyboard focus, it activates with Enter and Space, and it is announced as a button by assistive technology.