On this page12 sections
  1. Learning Objectives
  2. 1. Semantic HTML Is Most of It
  3. 2. Headings in Order
  4. 3. Text Alternatives
  5. 4. Labels and Link Text
  6. 5. Keyboard and Focus
  7. 6. Colour and Contrast
  8. 7. ARIA: Only When HTML Can't Do It
  9. Common Beginner Mistakes
  10. Practice Questions
  11. Practice Answers
  12. Key Takeaways

Lecture 031: Accessibility & Focus

Category5: Code, Characters & Standards
Lecture031 of 048
Estimated video length14 min
TargetHTML5 · any modern browser
LevelBeginner

Accessibility (often written a11y) means everyone can use your page: people who are blind and use a screen reader, people who can't use a mouse, people with low vision or colour blindness. Good HTML does most of the work for free.


Learning Objectives

  • Use semantic elements so assistive technology understands the page
  • Write good alt text, labels and link text
  • Make everything work with the keyboard, and keep focus visible
  • Know when (and when not) to use ARIA attributes

1. Semantic HTML Is Most of It

Screen readers announce elements by their meaning: "heading level 2", "navigation", "button", "list, 4 items". Use the right element and you get that for free.

html
<!-- Accessible -->
<button type="button">Save</button>

<!-- Not accessible: no keyboard access, no "button" announcement -->
<div onclick="save()">Save</div>

Landmarks (<header>, <nav>, <main>, <footer>) let screen-reader users jump straight to a section (Lecture 023).


2. Headings in Order

Screen-reader users often navigate by headings. Use one <h1>, then <h2>, <h3>… without skipping levels (Lecture 005).


3. Text Alternatives

html
<img src="chart.png" alt="Sales doubled from 2024 to 2026">
<img src="divider.png" alt="">

Describe meaningful images; use an empty alt="" for decoration, so it's skipped.


Example

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

<p>Read our <a href="#">refund policy</a>.</p>
  • Every input needs a <label>.
  • Link text should make sense on its own. Avoid "click here".

5. Keyboard and Focus

Try this example using only the Tab, Shift+Tab, Enter and Space keys:

Example

html
<style>
  a, button, input { margin: 4px; }
  :focus-visible { outline: 3px solid #f59f00; outline-offset: 2px; }
</style>

<a href="#">A link</a>
<button type="button" onclick="this.textContent = 'Pressed!'">A button</button>
<input placeholder="An input">
<div tabindex="0" style="display: inline-block; padding: 6px; border: 1px solid #aaa;">A focusable div</div>
  • Links, buttons and form fields can be focused automatically.
  • Never remove the focus outline without replacing it. Keyboard users need to see where they are.
  • A "skip link" at the very top lets keyboard users jump past the menu:
html
<a href="#main" class="skip-link">Skip to content</a>

6. Colour and Contrast

  • Text needs strong contrast with its background (at least 4.5 : 1 for normal text).
  • Don't use colour alone to show meaning. Add a word or an icon too.

Example

html
<p style="color: #c92a2a;">✖ Error: the email is missing.</p>
<p style="color: #2b8a3e;">✔ Saved.</p>

7. ARIA: Only When HTML Can't Do It

ARIA attributes add extra information for assistive technology.

html
<button type="button" aria-label="Close">✕</button>
<p id="status" aria-live="polite">Loading…</p>
<button type="button" aria-expanded="false" aria-controls="menu">Menu</button>
AttributeUse
aria-labelA name for an element with no visible text (an icon button)
aria-live="polite"Announce changes to this area (status messages)
aria-expandedSays whether a menu or panel is open

The first rule of ARIA: if a native HTML element does the job, use it instead.


Common Beginner Mistakes

1. Clickable <div>s and <span>s. Use <button> or <a>.

2. outline: none with no replacement. Keyboard users get lost.

3. Placeholders instead of labels. They vanish as you type, and screen readers may ignore them.

4. Too much ARIA. Wrong ARIA is worse than none. Prefer semantic HTML.


Practice Questions

  1. What does "a11y" mean?
  2. Why is <button> better than <div onclick>?
  3. Write good alt text for a photo of a red bicycle leaning on a wall.
  4. Which keys move focus forwards and backwards?
  5. How do you give an icon-only button an accessible name?
  6. What is the first rule of ARIA?

Practice Answers

  1. Accessibility (a, 11 letters, y).
  2. It works with the keyboard and is announced as a button automatically.
  3. alt="Red bicycle leaning against a brick wall"
  4. Tab moves forwards; Shift+Tab moves backwards.
  5. aria-label, for example <button aria-label="Search">🔍</button>.
  6. If a native HTML element can do the job, use it instead of ARIA.

Key Takeaways

  • Semantic HTML gives most accessibility for free.
  • Every image needs alt, every input a <label>, every link meaningful text.
  • Everything must work with the keyboard, with a visible focus outline.
  • Use ARIA only when HTML can't express something.