The alt, aria-label and aria-labelledby attributes provide a textual alternative to an image.
It is used whenever the actual image cannot be rendered.
Common reasons for that include:
It is also very important not to set an alternative text attribute to a non-informative value. For example, <img ... alt="logo"> is useless as it doesn’t give any information to the user. In this case, as for any other decorative image, it is better to use a CSS background image instead of an <img> tag. If using CSS background-image is not possible, an empty alt="" is tolerated. See Exceptions below.
This rule raises an issue when:
<img> element has no alt attribute.<input> element has no alt, aria-label or aria-labelledby attribute or they hold an empty string.<area> element within an image map has no alt, aria-label or aria-labelledby attribute.<object> element has no inner text, title, aria-label or aria-labelledby attribute.<img> elements with an empty string alt="" attribute won’t raise any issue. However, this way should be used in two cases only:
When the image is decorative and it is not possible to use a CSS background image. For example, when the decorative <img> is generated via javascript with a source image coming from a database, it is better to use an <img alt=""> tag rather than generate CSS code.
<li *ngFor="let image of images">
<img [src]="image" alt="">
</li>
When the image is not decorative but its alt text would repeat a nearby text. For example, images contained in links should not duplicate the link’s text in their alt attribute, as it would make the screen reader repeat the text twice.
<a href="flowers.html">
<img src="tulip.gif" alt="" />
A blooming tulip
</a>
In all other cases you should use CSS background images.
Add an alternative text to the HTML element.