Explain it simply
Explain it simply
In a book, readers tell headings, chapters, paragraphs and lists apart by how they look. Browsers and screen readers do not look; they read tag names. <h2> means a heading, <ul> a list, and <button> something you can press.
<div> says nothing except "a box". Build a whole page out of divs and it may look identical to sighted users, but the meaning is gone for blind users, keyboard users, search engines and the next developer who reads the code.
Problem → Why → Solution
The problem: a "button" made from a div
To match the design, a developer styles a <div> and adds onclick to make a button. It works with a mouse, but a Tab key user never reaches it, and pressing Enter or Space does nothing. A screen reader reads only the text "Place order" and never says it can be pressed. Adding role="button" changes what is announced, yet the keyboard problem remains.
<div class="btn" onclick="placeOrder()">Place order</div>
<!-- "Fixed" with ARIA, but still not reachable or usable by keyboard -->
<div class="btn" role="button" onclick="placeOrder()">Place order</div>Why does this happen?
<button> comes with focusability, activation by Enter and Space, a disabled state, form submission and the "button" role built in. A <div> has none of these. An ARIA role is only a label for assistive technology; it adds no behaviour, so you would have to write tabindex and keyboard handlers yourself. A <button> gives you all of that for free.
Core concept: an element carries a role, behaviour and structure
When a browser parses HTML it builds not only the DOM but also an accessibility tree, which assistive technology such as a screen reader reads. Each node in that tree has a role (heading, list, button, link), a name ("Place order") and states (checked, disabled, level 2). None of this comes from CSS; it comes from which element you chose. A class name like "title" or "btn" is invisible to that tree.
A semantic element gives you three things. A role: <h2> is a "level 2 heading", so screen reader users can jump between headings and scan the page like a table of contents. Behaviour: <button> works with the keyboard, and <a href> can be opened in a new tab. Structure: the <li> items inside a <ul> communicate relationships such as "item 1 of 3".
<div> and <span> are generic elements: having no meaning is their job. Use them for layout grouping or as CSS hooks when no fitting element exists. The rule is simple: ask what the content is first; if a matching element exists, use it, and only fall back to a div when none does.
Keep meaning and appearance apart. If the design wants a section title in small grey text, still use <h2> and shrink it with CSS. If a button should look like a link, style a <button>. This is also the first rule of ARIA: when a native element already has the meaning and behaviour you need, use that element instead of adding ARIA. ARIA exists to fill real gaps in HTML, not to rebuild elements the browser already provides.
- Ask what the content is
- Find the element that means it
- Fall back to div/span if none
- Style the look with CSS
The element says what it is; CSS says how it looks.
General pattern
<!-- generic: no meaning, only a styling hook -->
<div class="hook">...</div>
<!-- semantic: the element itself says what the content is -->
<h2>Section title</h2>
<ul><li>Item</li></ul>
<button type="button">Action</button>
<a href="/page">Destination</a>Reach for a meaningful element first: a heading for a title, a list for items, a button for an action, a link for navigation. Use a div only when nothing fits.
Example: a div version and a semantic version
This example writes the same content twice: Version A uses generic div boxes, and Version B uses elements that say what they are, a heading, a list and a button. Open it and press Tab: notice which "Order now" receives focus.
Neither version has any CSS yet, so Version B's bold heading and bullets come from the browser's default styles. You could add CSS to make both versions look identical, but the difference in roles and keyboard support would never go away.
<!-- Version A: generic boxes -->
<div class="title">Today's specials</div>
<div class="item">Mohinga - 2,500 Ks</div>
<div class="item">Shan noodles - 3,000 Ks</div>
<div class="btn" onclick="this.textContent = 'Ordered!'">Order now</div>
<!-- Version B: elements that say what they are -->
<h2>Today's specials</h2>
<ul>
<li>Mohinga - 2,500 Ks</li>
<li>Shan noodles - 3,000 Ks</li>
</ul>
<button type="button" onclick="this.textContent = 'Ordered!'">Order now</button>Code breakdown
- Generic title
<div class="title">Today's specials</div>The class is called "title", but class names mean nothing to the browser. In the accessibility tree it is plain text and never appears in a heading list.
- Clickable div
<div class="btn" onclick="this.textContent = 'Ordered!'">Order now</div>It reacts to mouse clicks only: no Tab focus, no Enter activation, and screen readers never call it a button.
- Real heading
<h2>Today's specials</h2>Its role is heading, level 2, so screen reader users can jump straight here with a heading shortcut.
- List
<ul>Because of <ul> and its <li> items, a screen reader announces "list, 2 items" so the user knows how much is coming.
- Real button
<button type="button" onclick="this.textContent = 'Ordered!'">Order now</button>The handler is identical, but as a <button> it takes Tab focus, fires click on Enter or Space, and is announced as "Order now, button". type="button" stops it from submitting if it ever ends up inside a form.
Version A shows four plain lines of unstyled text; Version B shows a large bold heading, a bulleted list and a real button. Pressing Tab focuses only Version B's button, and Enter changes it to "Ordered!".Common div replacements
When cleaning up an older codebase, the same swaps come up again and again. A class name that describes meaning, such as title, btn, nav or list, is usually a sign that a semantic element belongs there. Elements for the large page regions, such as header, nav, main and footer, get their own lesson next.
| Markup to replace | Semantic element |
|---|---|
| <div class="title"> | <h1> to <h6> (by outline level) |
| Lines separated by <br> | <ul> / <ol> with <li> |
| <div class="btn" onclick> | <button type="button"> |
| <span onclick="location.href=..."> | <a href="..."> |
| Important text marked with <b> | <strong> |
In real projects
Where this shows up in real projects
Semantic HTML is not only for screen readers. A browser's reader mode uses <article> and headings to extract the text, and translation tools, browser extensions and automated testing tools rely on roles too. Tools like Playwright and Testing Library recommend finding elements by role, such as getByRole("button", { name: "Order now" }), so correct semantics also make tests sturdier.
Search engines also use headings, links and structure to understand what a page is about, but adding a semantic tag is no guaranteed ranking boost; content quality is what matters most. In team projects, review comments like "this div should be a button" and "this bold text should be a heading" are among the most common ones you will see.
Common mistakes
Common mistakes
1. Choosing elements by their default look
Using <h1> because you want big text, or <blockquote> because you want an indent, sends the wrong meaning. Heading levels get skipped and screen reader users lose their bearings. Pick the element by meaning and set the size with CSS.
2. Using div plus ARIA instead of a native element
<div role="button"> changes only the announcement. You must add tabindex="0", Enter and Space handlers and a disabled state yourself, and something usually gets missed. <button> provides all of it at once.
3. Mixing up links and buttons
Use <a href> when going to another page or URL, and <button> when performing an action on the page, such as opening a modal or adding to a cart. A link with href="#" used as a button changes the URL and often jumps to the top of the page.
Best practices
Best practices
- Use the page once with only Tab, Shift+Tab, Enter and Space, without a mouse.
- Check roles in the DevTools Accessibility pane or its full accessibility tree view.
- When a class name describes meaning, look for an element with that meaning first.
Mini challenge
Mini challenge: make a div recipe card semantic
Rewrite the div-built recipe card in the starter with elements that match what each piece of content is. Keep the text the same.
- The recipe name becomes a level 2 heading
- Ingredients (order does not matter) and steps (order matters) use the two correct list types
- "Save recipe" becomes a real button that works with the keyboard
- "Full recipe" becomes a link to /recipes/mohinga
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Recipe card</title>
</head>
<body>
<!-- TODO: replace every div below with an element that matches its meaning -->
<div class="name">Mohinga</div>
<div class="label">Ingredients</div>
<div>Rice noodles</div>
<div>Fish broth</div>
<div class="label">Steps</div>
<div>Simmer the broth.</div>
<div>Pour it over the noodles.</div>
<div class="btn" onclick="this.textContent = 'Saved'">Save recipe</div>
<div class="link" onclick="location.href = '/recipes/mohinga'">Full recipe</div>
</body>
</html>Expected: The card shows a "Mohinga" heading, a bulleted ingredients list, steps numbered 1 and 2, a Save recipe button and an underlined Full recipe link. The Tab key focuses the button and then the link.
Show hint
A list where order does not matter is <ul>; one where it does is <ol>. Consider making the "Ingredients" and "Steps" labels level 3 headings.
Show solution
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Recipe card</title>
</head>
<body>
<h2>Mohinga</h2>
<h3>Ingredients</h3>
<ul>
<li>Rice noodles</li>
<li>Fish broth</li>
</ul>
<h3>Steps</h3>
<ol>
<li>Simmer the broth.</li>
<li>Pour it over the noodles.</li>
</ol>
<button type="button" onclick="this.textContent = 'Saved'">Save recipe</button>
<a href="/recipes/mohinga">Full recipe</a>
</body>
</html>Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- Semantic HTML chooses elements by meaning, and CSS handles the look.
- An element supplies a role, name, states and built-in behaviour in the accessibility tree.
- <div> and <span> are generic styling hooks for when no fitting element exists.
- Prefer native elements over ARIA: never replace a <button> with a div plus a role.