alt
is not a required attribute. While the url mentioned in another reply (html.spec.whatwg.org) might be a good resource, it is not an official spec. See the <img>
tag for the official spec. There's even a section called "If the src attribute is set and the alt attribute is not", implying that you don't need the alt
attribute.
Now, would I ever recommend not setting the alt
attribute? No. I think it should always be set, but that doesn't mean it's required. But one of the valid values is the null string, "" (quote-quote). If an image is decorative and doesn't provide additional context, then you can have alt=""
.
If your anchor tag does not have any embedded text and only relies on a nested image, such as:
<a href="...">
<img src="...">
</a>
then you can provide the appropriate context for screen readers by using either the aria-label
attribute on the <a>
or an appropriate alt
attribute on the image.
Option 1
<a href="..." aria-label="purpose of link">
<img src="..." alt="">
</a>
Option 2
<a href="...">
<img src="..." alt="purpose of link">
</a>
(Notice that option 1 still has an alt attribute on the image but it's the null string.)