Semantic HTML & Accessibility

Difficulty: Beginner

Question

What is semantic HTML? Why does it matter for accessibility and SEO, and when do you still need ARIA?

Answer

Semantic HTML means choosing elements for what the content is rather than how it looks: <nav> for navigation, <button> for an action, <article> for a self-contained post, <h1>-<h6> for headings. Browsers turn these elements into roles in the accessibility tree, so screen readers, keyboards, search engines and reader modes understand the page without extra work. A page built from <div> and <span> can look identical but tells these tools nothing.

How the browser uses your markup

When the browser parses HTML it builds the DOM, and from it an accessibility tree. Each node gets a role, a name and a state. A <button>Pay ₹499</button> becomes role button, name "Pay ₹499", and it is focusable and fires on both Enter and Space for free. A <div onclick> becomes a generic node with no role, no keyboard support and no focus.

Landmark elements map to ARIA landmark roles that screen reader users jump between:

ElementImplicit roleNotes
<header>bannerOnly when not nested inside <article>, <section>, <main> etc.
<nav>navigationUse for major navigation blocks, not every group of links
<main>mainOne visible <main> per page
<aside>complementaryRelated but secondary content
<footer>contentinfoSame nesting rule as <header>
<section>regionOnly becomes a landmark when it has an accessible name

Why it matters for SEO

Search engines read the same structure. Google uses headings, link text, <main> content and lists to understand what a page is about and which parts are boilerplate. Semantic markup is not a magic ranking boost, but it removes ambiguity: a clear <h1>, a logical heading outline and real <a href> links (not divs with click handlers) make content easier to crawl and extract for featured snippets.

When you actually need ARIA

The first rule of ARIA is: if a native element does the job, use it. ARIA only changes what assistive technology hears; it adds no behaviour. Reach for it when HTML has no equivalent:

  • Custom widgets like tabs, comboboxes and tree views (role="tablist", aria-selected, aria-controls)
  • Dynamic updates that must be announced (aria-live="polite" for "Item added to cart")
  • Naming icon-only controls (aria-label="Close")
  • Linking help or error text to inputs (aria-describedby, aria-invalid)

Wrong ARIA is worse than none. role="button" on a div still needs tabindex="0", Enter and Space handlers and focus styles, which is why a plain <button> is almost always the better answer.

Common mistakes interviewers look for

  • Skipping heading levels or picking headings by font size (<h4> because it looked right)
  • Using <section> as a styling wrapper; use <div> when there is no meaning
  • Clickable divs and spans instead of <button> or <a>
  • Missing alt on images, or writing "image of" in alt text; decorative images need alt=""
  • Inputs without a <label>; placeholder is not a label
  • Removing focus outlines with outline: none and nothing to replace them

On a real product, such as a checkout flow on Flipkart or Swiggy, these details decide whether a keyboard or screen reader user can finish paying. Teams catch them with axe or Lighthouse audits in CI plus manual testing with NVDA, VoiceOver or TalkBack.

From markup to assistive technology. Semantic elements carry roles into the accessibility tree that screen readers and crawlers rely on.
  1. Semantic HTML: Elements like nav, main, button and h1.
  2. DOM tree: The browser parses markup into nodes.
  3. Accessibility tree: Each node gets a role, name and state.
  4. Screen reader or crawler: Reads landmarks, headings and links.

Code examples

Div soup vs semantic layout

<!-- BAD: everything is a div, nothing has a role -->
<div class="header">
  <div class="nav"><div onclick="go('/')">Home</div></div>
</div>
<div class="content">
  <div class="title">Order summary</div>
  <div class="btn" onclick="pay()">Pay ₹499</div>
</div>

<!-- GOOD: landmarks, headings and native controls -->
<header>
  <nav aria-label="Primary">
    <a href="/">Home</a>
  </nav>
</header>
<main>
  <article>
    <h1>Order summary</h1>
    <p>2 items, delivery in 30 minutes</p>
    <button type="button">Pay ₹499</button>
  </article>
</main>
<footer>
  <p>&copy; 2026 Example Foods</p>
</footer>

The semantic version exposes banner, navigation, main and contentinfo landmarks, a real heading and a focusable button that works with Enter and Space without any JavaScript.

ARIA only where HTML falls short

<!-- Icon-only button needs an accessible name -->
<button type="button" aria-label="Close dialog">
  <svg aria-hidden="true" focusable="false"><!-- x icon --></svg>
</button>

<!-- Announce cart updates without moving focus -->
<p aria-live="polite" id="cart-status">2 items in cart</p>

<!-- Connect hint and error text to the input -->
<label for="upi">UPI ID</label>
<input id="upi" name="upi" required
       aria-describedby="upi-hint upi-error"
       aria-invalid="true">
<p id="upi-hint">Example: name@okbank</p>
<p id="upi-error">Enter a valid UPI ID</p>

Each ARIA attribute here fills a gap native HTML cannot: naming an icon, announcing a live change and associating extra text with a field.

Key points

  • Semantic elements map to roles in the accessibility tree; div and span map to generic nodes with no meaning.
  • Landmarks (header, nav, main, aside, footer) let screen reader users jump around the page.
  • Native controls like button and a come with focus and keyboard support; ARIA adds none of that.
  • First rule of ARIA: use a native element if one exists. Bad ARIA is worse than no ARIA.
  • Keep one h1 per page and a heading outline without skipped levels.
  • Semantic markup helps SEO by making structure and main content unambiguous, not by acting as a ranking trick.

Common follow-up questions

  • What is the difference between section, article and div?
  • How would you make a custom dropdown accessible?
  • What does aria-live do and when would you use assertive instead of polite?
  • How do you test a page for accessibility?
  • Why is a div with an onclick handler a problem for keyboard users?

Interview tips

  • Mention the accessibility tree; it shows you know why semantics matter, not just that they do.
  • Quote the first rule of ARIA and give one example where ARIA is genuinely needed.
  • Bring up keyboard users, not only screen readers. Many candidates forget them.
  • Name a real tool you have used, such as Lighthouse, axe DevTools or VoiceOver.

Concepts covered

Semantic HTML, Landmark elements, Accessibility tree, ARIA roles, Screen readers, SEO

More HTML & CSS Interview questions