Master of the universe

Top 5 ARIA Roles and Attributes for Building Accessible Websites

Introduction

Web accessibility is a crucial aspect of modern web development, as it ensures that websites and applications can be used by a diverse audience, including people with disabilities. Accessible Rich Internet Applications (ARIA) is a set of roles and attributes designed to improve the accessibility of dynamic web content. By using ARIA roles and attributes, developers can create more accessible websites that cater to the needs of all users.

In this article, we will discuss the top 5 ARIA roles and attributes that you should consider incorporating into your website to enhance its accessibility. These roles and attributes will help you provide a better user experience for everyone, including users who rely on screen readers and other assistive technologies.

1. Role: button

The button role is used to indicate that an element is a clickable button. This role is particularly useful for elements that do not have the native semantics of a button, such as a div or span element. By using the button role, you can ensure that screen readers and other assistive technologies understand the purpose of the element and can interact with it accordingly.

Use cases for the button role

Some common use cases for the button role include:

  • Custom buttons that perform actions, such as opening a modal or submitting a form
  • Interactive elements that toggle the visibility of other elements, such as dropdown menus or accordions

How to implement the button role

To implement the button role, simply add the role="button" attribute to the appropriate element in your HTML markup. Here's an example code snippet:

<div role="button" tabindex="0" onclick="handleButtonClick()">Click me!</div>

Common mistakes and best practices

When using the button role, keep the following best practices in mind:

  • Use native HTML button elements (<button> or <input type="button">) whenever possible, as they have built-in accessibility features and better browser support
  • Ensure that your custom button elements are keyboard-accessible by adding a tabindex="0" attribute
  • Provide appropriate keyboard event handlers (e.g., onclick and onkeydown) to make sure the button is functional for both mouse and keyboard users
https://www.youtube.com/watch?v=0hqhAIjE_8I

2. Role: navigation

The navigation role is used to indicate that a section of the page contains navigation elements, such as links, menus, or breadcrumbs. This role helps screen reader users understand the structure of your website and navigate it more efficiently.

Purpose of the navigation role

The main purpose of the navigation role is to provide context and semantic meaning to the navigation elements within your website. By using this role, you can ensure that assistive technologies can identify and interact with your site's navigation elements correctly.

Implementing the navigation role in your website

To implement the navigation role, add the role="navigation" attribute to the container element that holds your navigation elements. Here's an example code snippet:

<nav role="navigation">
  <ul>
    <li><a href="#home">Home</a></li>
    <li><a href="#about">About</a></li>
    <li><a href="#contact">Contact</a></li>
  </ul>
</nav>

Tips for organizing and labeling navigation elements

When using the navigation role, consider the following best practices:

  • Use the HTML5 <nav> element, which has built-in semantics for navigation, and add the role="navigation" attribute for better compatibility with older screen readers
  • Organize your navigation elements in a logical order, such as in a list (<ul> or <ol>)
  • Provide a clear and concise label for each navigation element, as this will be announced by screen readers
  • Use the aria-labelledby or aria-label attributes to provide additional context and information for your navigation elements if needed

3. Role: main

The main role is used to indicate the primary content area of a web page. This role is essential for accessibility, as it helps users with screen readers understand the main purpose of the page and allows them to navigate directly to the primary content.

Why the main role is vital for accessibility

The main role provides a clear indication of where the main content of a web page begins, making it easier for users with screen readers to locate and focus on the primary information. Without the main role, these users might have to navigate through a lot of irrelevant content, such as headers, sidebars, and footers, before reaching the main content.

Adding the main role to your site structure

To implement the main role in your website, add the role="main" attribute to the container element that holds your primary content. Here's an example code snippet:

<div role="main">
  <h1>Welcome to our website</h1>
  <p>Here you'll find the latest news and updates about our company.</p>
  <!-- more main content -->
</div>

Alternatively, you can use the HTML5 <main> element, which has built-in semantics for the main content area, and add the role="main" attribute for better compatibility with older screen readers.

Ensuring proper focus management with the main role

When using the main role, keep the following best practices in mind:

  • There should be only one main role per page to avoid confusion
  • Make sure that the main role is not nested inside another landmark role (e.g., navigation, header, or footer)
  • Use the tabindex="-1" attribute on the main content container to allow users with screen readers to navigate directly to the main content using a "skip to main content" link

4. Attribute: aria-labelledby

The aria-labelledby attribute is used to associate a label with an element. This attribute is particularly useful when you need to provide a more descriptive label for an element than the one provided by the native HTML label element.

Importance of the aria-labelledby attribute

The aria-labelledby attribute allows you to create more accessible and descriptive labels for elements, ensuring that users with screen readers can understand the purpose of each element and interact with it appropriately.

Use cases for aria-labelledby

Some common use cases for the aria-labelledby attribute include:

  • Form labels: Associate a more descriptive label with form inputs, especially when the native HTML label is not enough
  • Headings and sections: Label different sections of your page or application, providing additional context to users with screen readers

How to use aria-labelledby effectively

To use the aria-labelledby attribute, first create an element that contains the descriptive label you want to associate with the target element. Then, add the aria-labelledby attribute to the target element and set its value to the ID of the label element. Here's an example code snippet:

<h2 id="usernameLabel">Username (6-12 characters, including letters and numbers)</h2>
<input type="text" aria-labelledby="usernameLabel">

In this example, the input element is labeled by the more descriptive text within the <h2> element, providing additional context for users with screen readers.

5. Attribute: aria-describedby

The aria-describedby attribute is used to associate a description with an element, providing additional information or instructions to assist users with interacting with the element. This attribute is particularly helpful when you need to give more context or explanation than is typically available through the element's label.

Benefits of the aria-describedby attribute

Using the aria-describedby attribute improves the accessibility of your website by providing supplementary information to users with screen readers. This extra context can help users better understand the purpose and functionality of a given element, leading to a more inclusive and user-friendly experience.

When to use aria-describedby

Some common scenarios where the aria-describedby attribute can be helpful include:

  • Form input descriptions: Provide extra instructions or guidelines for filling out a form input, such as password requirements or character limits
  • Additional information for complex elements: Offer explanations for elements that may be confusing or difficult to understand, such as custom interactive components or data visualizations

Implementing aria-describedby in your project

To use the aria-describedby attribute, first create an element that contains the descriptive text you want to associate with the target element. Then, add the aria-describedby attribute to the target element and set its value to the ID of the description element. Here's an example code snippet:

<label for="password">Password</label>
<input type="password" id="password" aria-describedby="passwordDescription">
<p id="passwordDescription">Your password must be at least 8 characters long and include a combination of uppercase and lowercase letters, numbers, and special characters.</p>

In this example, the password input element is associated with the descriptive text within the <p> element, providing additional guidance for users with screen readers.

Conclusion

In this article, we have explored the top 5 ARIA roles and attributes that can significantly improve the accessibility of your website:

  1. Role: button
  2. Role: navigation
  3. Role: main
  4. Attribute: aria-labelledby
  5. Attribute: aria-describedby

By incorporating these roles and attributes into your project, you can create a more inclusive and user-friendly experience for all visitors, including those who rely on screen readers and other assistive technologies. We encourage you to continue exploring ARIA roles and attributes and implementing them in your work to further enhance web accessibility.

Frequently Asked Questions

What is ARIA?

ARIA stands for Accessible Rich Internet Applications. ARIA is a set of roles, states, and properties that help developers enhance the accessibility of their web content and applications, particularly for users with disabilities who rely on assistive technologies like screen readers.

Why is web accessibility important?

Web accessibility is essential because it ensures that everyone, regardless of their abilities or disabilities, can access, understand, and interact with the content and services available on the internet. By making your website accessible, you are promoting inclusivity, meeting legal requirements in many countries, and potentially reaching a broader audience.

How do I check if my website is accessible?

There are several tools available to help you evaluate the accessibility of your website. Some popular options include:

These tools can assist in identifying potential accessibility issues, but manual testing and user feedback should also be considered for a more comprehensive evaluation.

What is the difference between aria-labelledby and aria-describedby?

The aria-labelledby attribute is used to associate a label with an element, providing a name or title for that element. The aria-describedby attribute, on the other hand, is used to associate a description with an element, offering additional information or context beyond the label. While both attributes serve to enhance the accessibility of an element, aria-labelledby focuses on naming the element, while aria-describedby supplies supplementary details.

Can I use ARIA roles and attributes with any HTML element?

While ARIA roles and attributes can be added to most HTML elements, it is essential to use them appropriately and according to the ARIA specification. Some roles and attributes have specific requirements or are only applicable to certain types of elements. Always refer to the ARIA documentation and best practices when implementing ARIA in your project.

Sign up for the Artisan Beta

Help us reimagine WordPress.

Whether you’re a smaller site seeking to optimize performance, or mid-market/enterprise buildinging out a secure WordPress architecture – we’ve got you covered. 

We care about the protection of your data. Read our Privacy Policy.