Skip to main content
Mohammad Shehadeh — home (MSH monogram, letter M filled with the Palestinian flag)

How to improve Web Accessibility

Published on
5 min read

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.

1<!DOCTYPE html>
2<html lang="en">
3<head>
4  <meta charset="UTF-8" />
5  <title>WAI-ARIA Attributes to improve Web Accessibility</title>
6</head>
7<body>
8  <header>
9    <nav>
10      <!-- main navigation in here -->
11    </nav>
12  </header>
13
14  <!-- main page content in here -->
15  <main>
16    <!-- article's content -->
17    <h1>WAI-ARIA Attributes to improve Web Accessibility Post</h1>
18    <p>Published on <time datetime="2024-04-08">April 08, 2024</time></p>
19
20    <article>
21      <h2>Introduction</h2>
22      <!-- more content -->
23    </article>
24
25    <aside>
26      <h3>Related</h3>
27
28      <!-- aside content in here -->
29    </aside>
30  </main>
31
32  <!-- footer content in here -->
33  <footer>
34    <p>&copy; 2024 Blog. All rights reserved.</p>
35  </footer>
36
37</body>
38</html>

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.

1<label for="name">Name:</label>
2<input type="text" id="name" name="name" aria-required="true" />

Heading hierarchy

The hierarchical structure shows users how topics fit together, where to find information, and how important it is.

1<h1>Top level heading</h1>
2<h2>Secondary heading</h2>
3<h2>Another secondary heading</h2>
4<h3>Low level heading</h3>

Using clear language

Use meaningful text that gives users information. Write descriptive text instead of generic text.

1<!-- Generic Link Text (Not Accessible) -->
2<a href="https://www.example.com">Click here</a>
3
4<!-- Improved Link Text (Accessible) -->
5<a href="https://www.example.com">Visit the official website of Example Inc.</a>

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.

Note

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.

1<button aria-describedby="trash-desc">Move to trash</button>
2<p id="trash-desc">Items in the trash will be permanently removed after 30 days.</p>

aria-describedby can link to error messages within form fields.

1<label for="email">Email Address:</label>
2<input type="email" id="email" name="email" aria-describedby="email-error" />
3<div id="email-error" role="alert">Please enter a valid email address.</div>

The aria-label provides the interactive element with its accessible name.

1<button aria-label="Close">
2  <svg></svg>
3</button>

Alternative Text for Images

Alternative text provides descriptive content or purpose to screen reader users and search engines for SEO.

1<img
2  src="dinosaur.png"
3  alt="dinosaur standing upright like a human, with small arms, and a large head with lots of sharp teeth."
4/>

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.

1<video controls>
2  <source src="video.mp4" type="video/mp4" />
3  <track kind="captions" label="English" src="captions-en.vtt" srclang="en" default />
4</video>

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.

1<audio controls>
2  <source src="your-audio-file.mp3" type="audio/mpeg" />
3  Your browser does not support the audio element.
4</audio>
5
6<h2>Transcript:</h2>
7<p>This is the transcript of the audio content.</p>
8<p>You should include all the relevant information that is conveyed through the audio.</p>
9<p>
10  Make sure the transcript is synchronized with the audio content to provide an equivalent
11  experience for all users.
12</p>

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

  1. 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
  2. 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
  3. Content Accessibility

    • Write clear, descriptive text
    • Provide captions and transcripts for media
    • Ensure sufficient color contrast
    • Make interactive elements keyboard accessible

References

Related Articles

GET IN TOUCH

Let's work together

I build fast, accessible, and delightful digital experiences for the web. Whether you have a project in mind or just want to connect, I'd love to hear from you.

Get in touch

or reach out directly at hello@mohammadshehadeh.com