Lesson 11: HTML Accessibility
HTML Accessibility
Lesson 11 · Building websites everyone can use.
What is web accessibility?
Websites are used by millions of people, and not everyone interacts with them in the same way.
Some visitors may have difficulty seeing, hearing, moving a mouse, reading small text, or understanding complicated layouts.
Web accessibility means designing and building websites so that people with different abilities can use them.
Some examples of accessibility needs include:
- Screen readers that read webpage content aloud.
- Keyboard navigation instead of using a mouse.
- Captions for people who cannot hear audio.
- Clear layouts and readable text.
The alt attribute
In earlier lessons, you learned how to add images using the <img> element.
But what happens when someone uses a screen reader and encounters an image? They might not know what the image shows.
The alt attribute provides alternative text describing an image.
A screen reader can use this description to communicate the image's meaning.
A blue ocean with waves reaching the shore.
Making forms accessible
Forms can become confusing when users don't know what information each input field requires.
The <label> element gives an input a clear name. The label's for attribute should match the input's id.
This connects the label to the input. Clicking the label can also move focus to the input field.
Using a keyboard to navigate
Not everyone uses a mouse. Some people navigate websites using only a keyboard.
The Tab key moves between interactive elements such as links, buttons, and form fields. Pressing Enter or Space can activate certain controls.
These are naturally keyboard-accessible HTML elements.
You should also make sure the keyboard focus is visible. Users need to know which element is currently selected.
Why semantic elements matter
In Lesson 06, you learned about semantic HTML. These elements describe the purpose of different sections of a webpage.
Semantic elements help browsers, assistive technologies, and developers understand how a webpage is organized.
What is ARIA?
ARIA stands for Accessible Rich Internet Applications. It provides extra information to assistive technologies when standard HTML alone doesn't communicate enough.
One common ARIA attribute is aria-label. It provides an accessible name for an element.
This gives the button an accessible name even though its visible symbol is just an X.
Another useful attribute is aria-expanded:
It communicates whether an expandable section is currently open or closed. When the menu changes state, the value should also be updated.
Captions and transcripts
Not everyone can hear audio. Captions provide text versions of spoken dialogue and important sounds in a video.
HTML supports captions through the <track> element.
The captions file is usually written in WebVTT format and uses the .vtt file extension.
Transcripts can also help people who prefer reading the audio content instead.
Making text easier to read
Text should be easy to distinguish from its background. Poor contrast can make content difficult to read.
This text is difficult to read.
This text is much easier to read.
The Web Content Accessibility Guidelines (WCAG) recommend a contrast ratio of at least:
- 4.5:1 for normal-sized text at the AA level.
- 3:1 for large text at the AA level.
Using the right elements
A common mistake is using a generic div or span when a real button or link would work better.
Less appropriate for an action:
Better:
A real button already supports keyboard interaction and communicates its purpose to assistive technologies.
Your turn: Improve an accessible webpage!
Imagine you are reviewing a website that has several accessibility problems.
Your job is to improve the following code:
Try to make these improvements:
- Add meaningful alt text to the image.
- Use semantic HTML elements where appropriate.
- Replace the clickable div with a proper button or link.
- Add a label connected to the email input.
- Consider adding captions to the video.
- Check that your text has sufficient color contrast.
Test your knowledge!
Choose the correct answer for each question.