← Back tothis vs that

alt vs aria-label

Written byPhuoc Nguyen
Created
16 Sep, 2023
Category
HTML
Tags
Accessibility
When making web content accessible, it's crucial to provide alternative text for non-text elements, like images and icons. Two commonly used attributes for this are `aria-label` and `alt`. Although both serve the same purpose, there are some differences between them.

The alt attribute

The `alt` attribute is a text description used for images and other non-text content. The purpose of this attribute is to provide a way for screen readers and assistive technologies to understand what the image is about. The `img` element requires the `alt` attribute, but it's a good idea to include it for other non-text content too.
Here's an example of how you can use the `alt` attribute to describe an image:
html
<img src="image.jpg" alt="A red apple on a white background">

The aria-label attribute

The `aria-label` attribute helps to label the current element. It's handy when you have an element that doesn't have a visible label. This attribute can be used on any element, but it's especially useful for accessibility because it gives a textual representation of the element that can be understood by screen readers and other assistive technologies.
Here's an example of how to use the `aria-label` attribute to describe a button:
html
<button aria-label="Close">X</button>

Differences between alt and aria-label

Although both `aria-label` and `alt` serve the same purpose of providing alternative text for non-text content, there are some differences between them.
`alt` is specifically designed for images and other non-text content, and it's a required attribute for the `img` element. In contrast, `aria-label` can be used on any element and is optional.
Another difference is that `alt` provides a textual representation of the image, while `aria-label` provides a textual label for an element that doesn't have a visible label.

Using alt and aria-label together

To make sure that non-text content is accessible to everyone, it's a good idea to use both the `alt` and `aria-label` attributes. This way, everyone can understand the content, regardless of their abilities.
For instance, let's say you use an image as a link. In this case, both the `alt` attribute and the `aria-label` attribute can be used to provide alternative text for the image and a label for the link. This makes it easier for everyone to use and understand your website.
html
<a href="/page.html">
<img
src="image.jpg"
alt="A red apple on a white background"
aria-label="Link to page about apples"
>
</a>
In this example, we're making our web content more accessible by using the `alt` attribute to provide alternative text for images and the `aria-label` attribute to describe links for screen readers and other assistive technologies. By using both attributes together, we're ensuring that everyone can easily understand and interact with our web content.

Best practices for writing effective alt and aria-label attributes

When writing descriptions for images and other non-text content, it's important to follow some best practices. Here are a few tips for creating effective `alt` and `aria-label` attributes:
  • Be descriptive but concise
Alternative text should describe an image or element well enough to convey its meaning, but it should also be concise enough to avoid overwhelming the user with too much information. Aim for a balance between providing enough detail and keeping the text short and to the point.
  • Provide alternative text for icons and images
Icons and images can add context and visual appeal to forms. But to ensure they are accessible to everyone, it's crucial to provide alternative text using the `alt` or `aria-label` attribute. This allows screen readers and other assistive technologies to understand and convey the meaning of these elements.
  • Don't repeat surrounding content
To avoid redundancy, don't duplicate information that's already in the surrounding content. For instance, if you have an image of a button labeled Click me there's no need to include Click me in the alternative text since it's already on the button.
  • Use keywords
Including relevant keywords in your alternative text can help users understand the context of the image or element. For instance, if your image shows a red apple on a white background, including the word apple in your alternative text can help users who are searching for information about apples. This simple step can make a big difference in improving the accessibility and searchability of your content.
  • Test with screen readers
It's important to test your alternative text using screen readers and other assistive technologies to ensure that they accurately convey the meaning of the image or element. Doing so will help you identify any issues or areas where your alternative text could be improved.
By following these best practices, you can make your web content more accessible to all users by creating effective `alt` and `aria-label` attributes.

Conclusion

To sum it up, the `alt` attribute gives alternative text for non-text content, while the `aria-label` attribute gives a textual label for an element without a visible label. Both attributes make web content more accessible, but they are used in different contexts.
If you found this post helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

Questions? 🙋

Do you have any questions about front-end development? If so, feel free to create a new issue on GitHub using the button below. I'm happy to help with any topic you'd like to learn more about, even beyond what's covered in this post.
While I have a long list of upcoming topics, I'm always eager to prioritize your questions and ideas for future content. Let's learn and grow together! Sharing knowledge is the best way to elevate ourselves 🥷.
Ask me questions

Recent posts ⚡

Newsletter 🔔

If you're into front-end technologies and you want to see more of the content I'm creating, then you might want to consider subscribing to my newsletter.
By subscribing, you'll be the first to know about new articles, products, and exclusive promotions.
Don't worry, I won't spam you. And if you ever change your mind, you can unsubscribe at any time.
Phước Nguyễn