Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 744
Markus Kottländer Avatar asked Mar 25 '14 16:03

Markus Kottländer


People also ask

What does an alt attribute must be present on IMG elements?

The required alt attribute specifies an alternate text for an image, if the image cannot be displayed. The alt attribute provides alternative information for an image if a user for some reason cannot view it (because of slow connection, an error in the src attribute, or if the user uses a screen reader).

What happens when you do not provide alt attribute for an IMG tag?

If there is no alt attribute on the <img> element, the screen reader will announce the name of the image file, which may not make any sense to the user.

What alt attribute should be assigned to an image?

Because the image does not convey additional content, alt="" is the most appropriate choice. When an image is used only for decorative purposes, it is best to remove the image from the page content and instead define it as a CSS background image.

Should every image have an alt tag?

Don't add alt text to every image. You should add alt text to most images on a webpage for the sake of SEO, UX, and accessibility — however, there are exceptions. Images that are purely decorative or are described in text nearby, for example, should have an empty alt attribute.


1 Answers

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.)

like image 187
j08691 Avatar answered Sep 28 '22 03:09

j08691