How to improve Web Accessibility
Build a website everyone can use, no matter their abilities. Many sites and tools have accessibility barriers that make them hard or impossible for some people to use. Web accessibility helps screen readers and other tools understand content better.
Semantic HTML
A semantic tag describes its meaning to both the browser and the developer. It also improves SEO, making your site easier to find.
Semantic markup has other benefits beyond accessibility: - Easier to develop with; they have built-in keyboard accessibility — users navigate between buttons with the Tab key and activate their selection with Space, Return or Enter. - Search engines favor semantic elements like headings and links for SEO, making your content easier to find than non-semantic elements like <div>.
ARIA roles
By default, many semantic elements in HTML have a role; for example, <main> has the "main" role.
Use ARIA roles to describe elements that don't natively exist in HTML, or exist but don't yet have full browser support.
For instance, aria-required="true" marks required fields.
Heading hierarchy
The hierarchical structure shows users how topics fit together, where to find information, and how important it is.
Using clear language
Use meaningful text that gives users information. Write descriptive text instead of generic text.
Using tabindex
When users press the tab key to browse a webpage, only interactive elements (like links, buttons, form controls) get focused. tabindex makes other elements focusable too.
Set to 0, the element becomes focusable by keyboard and script. Set to -1, the element becomes focusable by script but stays out of the keyboard focus order.
Use element.focus() to set focus
Touch targets must be large enough to interact with. The recommended size is 48x48 px.
State and Property Attributes
ARIA attributes convey the state and properties of elements. For example, aria-hidden indicates whether the element is exposed to an accessibility API.
Accessible Names and Descriptions
ARIA attributes make widgets easier to understand for screen reader users.
The aria-describedby attribute lists the ids of the elements that describe the object. It links widgets or groups to the text that describes them.
aria-describedby can link to error messages within form fields.
The aria-label provides the interactive element with its accessible name.
Alternative Text for Images
Alternative text provides descriptive content or purpose to screen reader users and search engines for SEO.
Captions and Transcripts
Captions and transcripts are necessary for web accessibility, especially for people with hearing impairments. They make audio and video content reach a wider audience.
Captions provide a text representation of a video's content.
In this example:
<video>: Embeds the video element.<source>: Specifies the video source file.<track>: Defines a caption track.kind="captions":Indicates that this track contains captions.label="English": Specifies the label for the captions.src="captions-en.vtt": Points to the WebVTT (Web Video Text Tracks) file containing captions.srclang="en": Specifies the language of the captions.default: Indicates that these captions should be displayed by default.
Transcripts provide a written version of the audio's content. Here's an example of an audio transcript.
Web accessibility matters. As a frontend developer, you help make the internet inclusive for everyone. Use practices like semantic HTML, alt text for images, and keyboard navigation. Every accessible website you build counts. Keep going!
Quick Recap
-
Semantic HTML Best Practices
- Use appropriate HTML elements for their intended purpose
- Maintain proper heading hierarchy
- Include meaningful alt text for images
- Use semantic elements for better SEO and accessibility
-
ARIA Implementation
- Use ARIA roles and attributes when native HTML isn't sufficient
- Provide clear accessible names and descriptions
- Maintain proper state management
- Don't use ARIA when native HTML elements suffice
-
Content Accessibility
- Write clear, descriptive text
- Provide captions and transcripts for media
- Ensure sufficient color contrast
- Make interactive elements keyboard accessible
References
- MDN - Accessibility
- MDN - ARIA states and properties
- MDN - ARIA Roles
- W3C - Web Accessibility Initiative