Lesson 2 · Web development foundations

Semantic HTML and accessibility

Write HTML that works for everyone, including people using screen readers and keyboards.

About 25 minutes

Tab indents. Press Escape, then Tab, to leave the editor.

HTML isn't a way to make things look a certain way; CSS does that. HTML says what each part of a page is: a heading, a list, a navigation menu, a button, a form field. That meaning is called semantics, and it's what browsers, search engines and assistive technology read. Get it right and your page works with a mouse, a keyboard, a screen reader and a voice assistant with little extra effort. Get it wrong and you'll spend days bolting on fixes. Accessibility is also a legal requirement for many organizations and a standard part of code review, so it's a skill employers look for.

Who you're building for

Accessibility (often shortened to a11y, an "a", 11 letters, a "y") means people can use what you build regardless of disability or circumstance. That includes:

  • people who are blind or have low vision, using a screen reader (software that reads the page aloud or sends it to a braille display, such as VoiceOver on Apple devices, TalkBack on Android, or NVDA and JAWS on Windows) or screen magnification;
  • people who can't use a mouse and navigate with a keyboard, a switch device or voice control;
  • people who are deaf or hard of hearing, who need captions for audio and video;
  • people with cognitive or attention differences, who benefit from clear structure and plain language.

It also includes everyone in a temporary situation: a broken arm, bright sunlight on a phone, a noisy room. The international standard is the W3C's Web Content Accessibility Guidelines (WCAG), currently version 2.2. Most organizations aim for its AA level, and laws such as the Americans with Disabilities Act in the US and the European Accessibility Act in the EU are applied using it.

How a screen reader sees your page

Browsers build an accessibility tree alongside the DOM. For each meaningful element it records a role (what it is: button, link, heading, textbox), an accessible name (what it's called: "Send message"), and a state (checked, expanded, disabled). Screen readers read that tree, not your pixels.

This is why semantics matter so much. A <button> gets the role "button" automatically, can be reached with the Tab key, and fires its click handler on Enter and Space. A <div> styled to look like a button gets none of that: it has no role, can't be focused, and ignores the keyboard. You could add all of those back with attributes and JavaScript, but you'd be rebuilding, badly, what the browser gives you for free.

Question

Quick check

A teammate made a "Buy now" control with <div class="btn" onclick="buy()">. It looks right and works with a mouse. What's the problem?
Pick an answer.

Landmarks: the page's table of contents

Screen reader users rarely listen to a page from top to bottom. They jump between headings and landmarks, regions with a defined purpose. HTML gives you elements for them:

<header>Site name, logo, maybe the main navigation</header>
<nav>Links to other pages</nav>
<main>The one main content area of this page</main>
<aside>Related but secondary content</aside>
<footer>Contact details, copyright, secondary links</footer>

Use exactly one <main> per page. Inside it, use headings in order: one <h1> for the page's topic, <h2> for its sections, <h3> inside those. Never pick a heading level for its font size; that's CSS's job. A sighted reader skims by headings too, so a good outline helps everyone.

Forms: the place accessibility breaks most

Forms are where users give you information, so a broken form means a lost signup, sale or support request. The rules:

  1. Every field needs a <label>. Connect it either by wrapping the input, or with for matching the input's id:

    <label for="email">Email address</label>
    <input id="email" name="email" type="email" autocomplete="email">
    

    The label becomes the field's accessible name, and clicking it focuses the field, a bigger target for everyone. A placeholder is not a label: it disappears as soon as someone types, often has low contrast, and isn't reliably announced.

  2. Use the right input type. type="email", tel, number, date and url get built-in validation and, on phones, the right keyboard. autocomplete lets browsers fill in known details.

  3. Buttons inside forms need a type. A <button> in a <form> defaults to type="submit". Say so explicitly, and use type="button" for buttons that shouldn't submit, such as "Show password".

  4. Group related choices with <fieldset> and a <legend>, so "Yes" and "No" radio buttons are announced with their question.

Images and alt text

Every <img> needs an alt attribute. What goes in it depends on the image's job:

  • Informative images (a chart, a photo that matters, a logo) get a short description of what they convey: alt="OfflineOwl" for a logo, not alt="logo image".
  • Decorative images that add nothing a reader would miss get an empty alt="", which tells screen readers to skip them.
  • Functional images, such as an icon inside a link or button, describe the action: alt="Search".

A missing alt is the worst case: many screen readers fall back to reading the file name.

Fix an inaccessible page

Challenge

Make the contact page accessible

This page looks fine but is hard to use without a mouse or a screen. Fix it with semantic HTML:

  1. Wrap the top in <header>, the form in <main>, and the last line in <footer>.
  2. Make the title a real <h1>.
  3. Give the logo meaningful alt text.
  4. Give every field a <label>, and make the email field type="email".
  5. Replace the fake button with a real <button type="submit">.

The checks below tell you what's still missing.

  • The page has header, main and footer landmarks
  • The title is an h1
  • The logo has meaningful alt text
  • Every field has a label
  • The email field uses type="email"
  • The form has a real submit button

ARIA: powerful, and a last resort

ARIA (Accessible Rich Internet Applications) is a set of attributes such as role, aria-label and aria-expanded that let you add or change information in the accessibility tree. It's essential for custom widgets that HTML has no element for, like a tabbed panel. But ARIA only changes what assistive technology is told; it adds no behavior. role="button" on a <div> makes a screen reader say "button", yet the div still can't be focused or pressed with the keyboard.

The W3C's own guidance puts it first: if a native HTML element already does what you need, use it instead of adding ARIA.

Question

Quick check

You need a control that opens and closes a menu. What's the best starting point?
Pick an answer.

Recap

  • Semantic HTML describes what content is; the browser turns it into an accessibility tree of roles, names and states.
  • Native elements like <button>, <a> and form fields come with keyboard support and correct announcements built in.
  • Landmarks (header, nav, main, footer) and an ordered heading outline let people jump around a page.
  • Every field needs a real <label>; placeholders are hints, not labels. Every image needs alt, empty only if it's decorative.
  • Reach for ARIA only when HTML has no element for what you're building.

Finished this lesson?

Mark it complete to track your progress on this device.