DEV Community

Marco
Marco

Posted on • Originally published at blog.disane.dev

ARIA attributes in HTML: A comprehensive guide 🌐

Learn how ARIA attributes improve accessibility in HTML and how you can use them effectively in your projects 🥸


What are ARIA attributes? 🤔

ARIA stands for "Accessible Rich Internet Applications". It is a set of attributes that have been developed to make web content and applications more accessible for people with disabilities. In particular, ARIA attributes help to improve accessibility for users who rely on assistive technologies such as screen readers.

These attributes extend the semantic meaning of HTML elements and provide additional information that is necessary for interpretation by assistive technologies. For example, they can be used to define the role of an element, specify its states and properties or establish relationships between elements.

Why are ARIA attributes so important? 🚀

Imagine:

You are a blind person who wants to be active in the digital world. You want to use the latest technologies, be part of digitalization and not be excluded from the benefits of modern web applications. Your favorite website offers exciting content and services that you would like to use - but every time you try to access them, you come up against insurmountable barriers.

Without ARIA attributes, interactive components such as modal dialogs, sliders or dynamic content areas remain invisible and incomprehensible to you. You will only hear confusing instructions or receive no information at all about how to use the page.

This is not only frustrating, but effectively excludes you from important online offers. ARIA attributes are critical because they ensure that you, as someone who relies on assistive technologies, can experience the full functionality of the website. They enable developers to design complex user interfaces so that they are accessible and understandable for everyone, regardless of individual ability.

Who-are-aria-attributes-specifically-aimed-at? 🧑‍🦯

ARIA attributes are aimed in particular at people who rely on assistive technologies to use the web. This includes users of screen readers that read out information on a website and people with motor impairments who use special input devices.

ARIA attributes enable these users to better understand and interact with complex web applications. In addition, developers and designers also benefit from using ARIA attributes as they can ensure that their applications are accessible to a wider audience.

Why should software be inclusive? 🌍

Inclusive software is not only ethically desirable, but also commercially beneficial. By designing software to be accessible to as many people as possible, you can reach a broader user base while meeting legal requirements in many countries.

Inclusion means that no one is excluded due to disabilities or limitations.

In addition, inclusive software helps to promote a positive brand image by showing that the company cares about all its users. Ultimately, this strengthens users' trust and loyalty towards the brand.

Equality Awareness GIF by INTO ACTION

The basic ARIA roles and attributes 📜

ARIA offers a variety of attributes that can be divided into three main categories: Roles, Properties and States.

ARIA roles 🎭

Roles define what an element on the page represents or what function it has. Some of the most commonly used roles are:

  • role="button": Assigns the role of a button to an element. Useful if you want to use a div or span as a button.
  • role="navigation": Identifies a section of the page as a navigation area.
  • role="dialog": Defines a modal dialog box that requires the user's attention.
  • role="alert": Marks an element as an alert or important message that is immediately announced by assistive technologies.

ARIA properties 🏷️

Properties provide additional information about an element and its relationships to other elements. Important ARIA properties are:

  • aria-labelledby: Refers to another element that labels the current element. Particularly useful in form elements.
  • aria-describedby: Similar to aria-labelledby, but gives a more detailed description of the element.
  • aria-controls: Indicates that the current element controls the interaction with another element, e.g. a switch that opens a menu.For example, a switch that shows or hides a menu.

ARIA states 🔄

States define the current status of an interactive element. Examples are:

  • aria-expanded: Indicates whether an area is expanded (true) or collapsed (false).
  • aria-checked: Indicates whether a checkbox or switch is activated (true), not activated (false) or has a mixed state (mixed).
  • aria-hidden: Determines whether an element is visible (false) or hidden (true) for assistive technologies.

Practical examples of using ARIA attributes 🛠️

To better understand the use of ARIA attributes, let's look at some practical examples.

A custom button

Developers often use div or span elements to create custom buttons that are different from standard HTML buttons. To ensure that these custom buttons are also accessible to screen readers, ARIA attributes should be used:

<div role="button" aria-pressed="false" tabindex="0" onclick="toggleButton(this)">
  Click me
</div>

Enter fullscreen mode Exit fullscreen mode

In this example, the role="button" attribute assigns the div element the role of a button. The aria-pressed attribute indicates whether the button is currently pressed or not, and tabindex="0" makes the element focusable via the keyboard.

Accordion menu

Accordion menus consist of several collapsible sections. You can use ARIA attributes to ensure that they are understandable for screen reader users:

<div id="accordion">
  <h3 id="section1-heading">
    <button aria-expanded="false" aria-controls="section1">
      Section 1
    </button>
  </h3>
  <div id="section1" role="region" aria-labelledby="section1-heading">
    <p>Content of the first section </p>
  </div>

  <h3 id="section2-heading">
    <button aria-expanded="false" aria-controls="section2">
      Section 2
    </button>
  </h3>
  <div id="section2" role="region" aria-labelledby="section2-heading">
    <p>Content of the second section </p>
  </div>
</div>

Enter fullscreen mode Exit fullscreen mode

Here, each button element uses the aria-expanded attribute to indicate whether the associated section is open or closed. The aria-controls attribute indicates the relationship between the button and the content area, while role="region" identifies the content as an area of interest to the user.

Modal dialogs

Modal dialogs are frequently used UI components that can be made accessible with ARIA attributes:

<div role="dialog" aria-labelledby="dialog-title" aria-describedby="dialog-description">
  <h2 id="dialog-title">confirmation required</h2>
  <p id="dialog-description">Do you really want to perform this action?</p>
  <button onclick="closeDialog()">Close</button>
</div>

Enter fullscreen mode Exit fullscreen mode

In this example, role="dialog" identifies the element as a modal dialog box. The aria-labelledby attribute links the dialog box to the title, and aria-describedby refers to a description of the dialog content.

Common mistakes when using ARIA attributes 🚨

Despite their benefits, there are some common mistakes that developers make when implementing ARIA attributes. It is important to avoid these so as not to unintentionally compromise accessibility.

Excessive use of ARIA

ARIA should only be used when necessary. If an HTML element is inherently semantically meaningful (e.g. button, nav, header), it should be used without ARIA.

Missing synchronization of states

If the state of an element (e.g. aria-exp, nav, header) is missing, it should be used without ARIA.If the state of an element (e.g. aria-expanded) is changed by user input, it must be ensured that the ARIA attribute is updated accordingly.

Unnecessary role assignments

Not every element requires a role assignment. For example, a button element does not require an additional role="button" assignment.

Best practices for using ARIA 🌟

To get the most out of ARIA attributes and maximize accessibility, some best practices should be followed:

Use native HTML elements when possible

These already have built-in accessibility features that ARIA may not be able to provide.

Test accessibility regularly

Use tools such as screen readers or specialized software to make sure your ARIA implementation is working.

Document your ARIA implementations

If you use ARIA attributes in your code, document why and how they are used so that other developers can better understand the code.

What you shouldn't use ARIA for (with examples) 🚫

Although ARIA attributes are powerful, they should not be used as a replacement for native HTML elements that already have inherent semantic meanings and accessibility properties. For example, you should not use a div element with role="button" instead of a regular button element unless there is a good reason to do so.

Native HTML elements such as button, input, label or select already provide excellent accessibility support. Overuse of ARIA attributes can cause code to become more complex and difficult to maintain, and in some cases it can even worsen accessibility if the attributes are not implemented correctly.

How to self-test ARIA 🔍

Testing ARIA attributes is a crucial step in ensuring that a web application is truly accessible. There are various methods for testing ARIA attributes.

One of the most effective methods is to use a screen reader such as NVDA (NonVisual Desktop Access) or VoiceOver (on Mac). These tools read out the content of the page and indicate whether the ARIA attributes have been implemented correctly. In addition, developers can use tools such as the Accessibility Inspector in the developer tools of browsers or specialized accessibility tools such as Axe or WAVE to identify and fix potential problems.

Regular testing with these tools helps to continuously improve accessibility.

Companies that take accessibility seriously 🌟

Some companies have made it their mission to create truly inclusive digital experiences that are accessible to all. One outstanding example is Microsoft. The company has made a strong commitment to accessibility and is continuously developing tools and technologies that support people with disabilities. With initiatives such as the "AI for Accessibility" program, Microsoft promotes innovations that aim to improve the lives of people with disabilities. Microsoft's operating systems and Office applications also offer comprehensive accessibility features that enable people with visual, hearing or mobility impairments to work productively.

Preview image

Another example is Apple, which always pays attention to accessibility in its product development. Functions such as VoiceOver, which enables blind and visually impaired users to operate the device completely voice-controlled, or the extensive customization options for users with motor impairments set standards in the industry. Apple has managed to develop technologies that are inclusive from the start and ensure that no one is excluded.

Preview image

Google is also one of the companies prioritizing accessibility. With the development of products such as Google Assistant, which has been optimized for people with disabilities, and the embedding of accessibility tools in Android and Chrome, Google shows that inclusion is a central component of its product philosophy.

Preview image

These companies are inspiring examples of how accessibility can be integrated into a company's DNA. They prove that it is not only possible, but also economically viable to create digital products and services that are accessible to all people.

Conclusion 🎯

ARIA attributes are a powerful tool to improve the accessibility of web applications. They enable developers to make interactive and dynamic content accessible to all users, especially those who rely on assistive technologies. By carefully implementing ARIA attributes, you can help make the web a more inclusive place.

If you have any questions or need help implementing ARIA attributes, feel free to use the comments section below. Together we can make the web more accessible for everyone!


If you like my posts, it would be nice if you follow my Blog for more tech stuff.

Top comments (0)