Summary

Usage note:
Browsers do not always display the image referenced by the element. This is the case for non-graphical browsers (including those used by people with vision impairments), if the user chooses not to display images, or if the browser is unable to display the image because it is invalid or an unsupported type. In these cases, the browser may replace the image with the text defined in this element's alt attribute.

This attribute defines the alternative text describing the image. Users will see this displayed if the image URL is wrong, the image is not in one of the supported formats, or if the image is not yet downloaded.

Usage note: Omitting this attribute indicates that the image is a key part of the content, but no textual equivalent is available. Setting this attribute to the empty string indicates that this image is not a key part of the content; non-visual browsers may omit it from rendering.

This enumerated attribute indicates if the fetching of the related image must be done using CORS or not. CORS-enabled images can be reused in the <canvas> element without being tainted. The allowed values are:

anonymous

A cross-origin request (i.e., with Origin: HTTP header) is performed. But no credential is sent (i.e., no cookie, no X.509 certificate, and no HTTP Basic authentication is sent). If the server does not give credentials to the origin site (by not setting the Access-Control-Allow-Origin: HTTP header), the image will be tainted and its usage restricted.

use-credentials

A cross-origin request (i.e., with Origin: HTTP header) performed with credential is sent (i.e., a cookie, a certificate, and HTTP Basic authentication is performed). If the server does not give credentials to the origin site (through Access-Control-Allow-Credentials: HTTP header), the image will be tainted and its usage restricted.

When not present, the resource is fetched without a CORS request (i.e., without sending the Origin: HTTP header), preventing its non-tainted usage in <canvas> elements. If invalid, it is handled as if the enumerated keyword anonymous was used. See CORS settings attributes for additional information.

A list of one or more strings separated by commas indicating a set of source sizes. Each source size consists of:

a media condition. This must be omitted for the last item.

a source size value.

Source size values are used to specify the intended size of the image, for the purpose of selecting a source from the list supplied by the srcset attribute when it uses 'w' descriptors. The selected size affects the intrinsic size of the image (images inherent size if no explicit CSS styling is applied). If the srcset attribute is absent, or contains no values with a width descriptor, then the sizes attribute has no effect.

Image URL, this attribute is obligatory for the <img> element. On browsers supporting srcset, src is treated like a candidate image with a pixel density descriptor 1x unless an image with this pixel density descriptor is already defined in srcset or srcset contains 'w' descriptors.

Interaction with CSS

Regarding CSS, an <img> is a replaced element. It has no baseline, meaning, when used in an inline formatting context with vertical-align: baseline, the bottom of the image will be posed on the container's baseline.

Depending of its type, an image may have an intrinsic dimension, but this is not a necessary condition: a SVG image has no intrinsic dimension, a raster image has one.

Example 1

Example 2: Image link

Example 3: Using the srcset attribute

The src attribute is a 1x candidate in user agents supporting srcset.

<img src="mdn-logo-sm.png"
alt="MDN"
srcset="mdn-logo-HD.png 2x">

Example 4: Using the srcset and sizes attributes

The src attribute is ignored in user agents supporting srcset when using 'w' descriptors. When the (min-width: 600px) media condition matches, the image will be 200px wide, otherwise 50vw (50% of the viewport width).