"An img element must have an alt attribute, except under certain conditions." What conditions?

StackOverflow https://stackoverflow.com/questions/22640095

  •  21-06-2023
  •  | 
  •  

Question

This is what the W3C Validator prints for an img tag without an alt attribute:

"An img element must have an alt attribute, except under certain conditions."

I can't find anything about it... at least nothing more than what the alt tag is about.

Does anybody know what conditions?

Was it helpful?

Solution

The W3 outlines the reason:

In some cases an image is included in a published document, but the author is unable to provide an appropriate text alternative. In such cases the minimum requirement is to provide a caption for the image using the figure and figcaption elements under the following conditions:

  • The img element is in a figure element
  • The figure element contains a figcaption element
  • The figcaption element contains content other than inter-element whitespace
  • Ignoring the figcaption element and its descendants, the figure element has no Text node descendants other than inter-element whitespace, and no embedded content descendant other than the img element.

In other words, the only content of the figure is an img element and a figcaption element, and the figcaption element must include (caption) content.

Such cases are to be kept to an absolute minimum. If there is even the slightest possibility of the author having the ability to provide real alternative text, then it would not be acceptable to omit the alt attribute.

Code example of an allowable case for no alt attribute:

<figure>
<img src="clara.jpg">
<figcaption>clara.jpg, taken on 12/11/2010.</figcaption>
</figure>

WHATWG.org adds:

A conformance checker must report the lack of an alt attribute as an error unless one of the conditions listed below applies:

  • The img element is in a figure element that satisfies the conditions described above.

  • The img element has a title attribute with a value that is not the empty string (also as described above).

  • The conformance checker has been configured to assume that the document is an e-mail or document intended for a specific person who is known to be able to view images.

  • The img element has a (non-conforming) generator-unable-to-provide-required-alt attribute whose value is the empty string. A conformance checker that is not reporting the lack of an alt attribute as an error must also not report the presence of the empty generator-unable-to-provide-required-alt attribute as an error. (This case does not represent a case where the document is conforming, only that the generator could not determine appropriate alternative text — validators are not required to show an error in this case, because such an error might encourage markup generators to include bogus alternative text purely in an attempt to silence validators. Naturally, conformance checkers may report the lack of an alt attribute as an error even in the presence of the generator-unable-to-provide-required-alt attribute; for example, there could be a user option to report all conformance errors even those that might be the more or less inevitable result of using a markup generator.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top