On this page12 sections
Lecture 031: Accessibility & Focus
| Category | 5: Code, Characters & Standards |
| Lecture | 031 of 048 |
| Estimated video length | 14 min |
| Target | HTML5 · any modern browser |
| Level | Beginner |
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
alttext, 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.
<!-- 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
<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.
4. Labels and Link Text
Example
<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
<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:
<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
<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.
<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>| Attribute | Use |
|---|---|
aria-label | A name for an element with no visible text (an icon button) |
aria-live="polite" | Announce changes to this area (status messages) |
aria-expanded | Says 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
- What does "a11y" mean?
- Why is
<button>better than<div onclick>? - Write good
alttext for a photo of a red bicycle leaning on a wall. - Which keys move focus forwards and backwards?
- How do you give an icon-only button an accessible name?
- What is the first rule of ARIA?
Practice Answers
- Accessibility (a, 11 letters, y).
- It works with the keyboard and is announced as a button automatically.
alt="Red bicycle leaning against a brick wall"- Tab moves forwards; Shift+Tab moves backwards.
aria-label, for example<button aria-label="Search">🔍</button>.- 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.