DEV Community

Carlos Viteri
Carlos Viteri

Posted on • Edited on • Originally published at c4rlosviteri.dev

3 1

Accessible components: Alerts

Alerts are a very common pattern to provide feedback messages for user actions such as error, warning or information. Do you know how to create a truly accessible alert? In this tutorial, we will learn how to create one.

There are two ways of creating WAI-ARIA compliant alerts. We will take a look at both.

The easiest and recommended one is to add an aria-role="alert". This role is used to communicate an important message to the user.

<div role="alert">
  <p>Alert example: Here should be the alert content</p>
</div>

On the other hand, we can create an alert by adding two aria attributes:

  • aria-live="assertive" whatever text change in the children nodes of this attribute will interrupt the current process in the assistive technologies to notify immediately this change to the user.
  • aria-atomic="true" this attribute tells the assistive technologies to read the entire content of the alert event if just a portion of it has changed.
<div aria-atomic="true" aria-live="assertive">
  <p>Alert example: Here should be the alert content</p>
</div>

Sometimes you will want the user to close the alert. To do it, you have to add a close labeled button by using an aria-label, only if the text inside the button is not descriptive enough.

<div role="alert">
  <button aria-label="Close alert">×</button>
  <p>Alert example: Here should be the alert content</p>
</div>

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay