W3cubDocs

/HTML

<img>

The <img> embeds an image into the document.

Content categories Flow content, phrasing content, embedded content, palpable content. If the element has a usemap attribute, it also is a part of the interactive content category.
Permitted content None, it is an empty element.
Tag omission Must have a start tag and must not have an end tag.
Permitted parents Any element that accepts embedded content.
Permitted ARIA roles Any
DOM interface HTMLImageElement

Attributes

This element includes the global attributes.

align Deprecated since HTML4.01, Obsolete since HTML5
The alignment of the image with respect to its surrounding context. In HTML5, use the float and/or vertical-align CSS properties instead.
alt
This attribute defines the alternative text describing the image. Users will see this text 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.

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 cannot 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. You should, for these reasons and others, provide a useful value for alt whenever possible.

Omitting this attribute altogether indicates that the image is a key part of the content, and no textual equivalent is available. Setting this attribute to an empty string (alt="") indicates that this image is not a key part of the content, and that non-visual browsers may omit it from rendering.

border Deprecated since HTML4.01, Obsolete since HTML5
The width of a border around the image. In HTML5, use the border CSS property instead.
crossorigin HTML5
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.
decoding

Provides an image decoding hint to the browser. The allowed values are:

sync
Decode the image synchronously for atomic presentation with other content.
async
Decode the image asynchronously to reduce delay in presenting other content.
auto
Default mode, which indicates no preference for the decoding mode. The browser decides what is best for the user.
height
The intrinsic height of the image in pixels. In HTML 4, the height could be defined pixels or as a percentage. In HTML5, however, the value must be in pixels.
hspace Deprecated since HTML4.01, Obsolete since HTML5
The number of pixels of white space to insert to the left and right of the image. In HTML5, use the margin CSS property instead.
ismap
This Boolean attribute indicates that the image is part of a server-side map. If so, the precise coordinates of a click are sent to the server.

This attribute is allowed only if the <img> element is a descendant of an <a> element with a valid href attribute.

longdesc
A link to a more detailed description of the image. Possible values are a URL or an element id.
name Deprecated since HTML4.01, Obsolete since HTML5
A name for the element. It is supported in HTML 4 only for backward compatibility. Use the id attribute instead.
referrerpolicy
A string indicating which referrer to use when fetching the resource:
  • no-referrer: the Referer header will not be sent.
  • no-referrer-when-downgrade: no Referer header will be sent when navigating to an origin without TLS (HTTPS). This is a user agent’s default behavior if no policy is otherwise specified.
  • origin: the Referer header will include the page of origin's scheme, the host and the port.
  • origin-when-cross-origin: navigating to other origins will limit the included referral data to the scheme, the host and the port, while navigating from the same origin will include the referrer's full path.
  • unsafe-url: the Referer header will include the origin and the path, but not the fragment, password or username. This case is unsafe because it can leak origins and paths from TLS-protected resources to insecure origins.
sizesHTML5
A list of one or more strings separated by commas indicating a set of source sizes. Each source size consists of:
  1. a media condition. This must be omitted for the last item.
  2. a source size value.

Source size values specify the intended display size of the image. User agents use the current source size to select one of the sources supplied by the srcset attribute, when those sources are described using width ('w') descriptors. The selected source size affects the intrinsic size of the image (the image’s display size if no CSS styling is applied). If the srcset attribute is absent, or contains no values with a width (w) descriptor, then the sizes attribute has no effect.

src
The image URL. This attribute is mandatory 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 unless srcset contains 'w' descriptors.
srcsetHTML5
A list of one or more strings separated by commas indicating a set of possible image sources for the user agent to use. Each string is composed of:
  1. a URL to an image,
  2. optionally, whitespace followed by one of:
    • a width descriptor, or a positive integer directly followed by 'w'. The width descriptor is divided by the source size given in the sizes attribute to calculate the effective pixel density.
    • a pixel density descriptor, which is a positive floating point number directly followed by 'x'.

If no descriptor is specified, the source is assigned the default descriptor: 1x.

It is incorrect to mix width descriptors and pixel density descriptors in the same srcset attribute. Duplicate descriptors (for instance, two sources in the same srcset which are both described with '2x') are invalid, too.

The user agent selects any one of the available sources at its discretion. This provides them with significant leeway to tailor their selection based on things like user preferences or bandwidth conditions.

width
The intrinsic width of the image in pixels. In HTML 4, either a percentage or pixels were acceptable values. In HTML5, however, only pixels are acceptable.
usemap
The partial URL (starting with '#') of an image map associated with the element.

Usage note: You cannot use this attribute if the <img> element is a descendant of an <a> or <button> element.

vspace Deprecated since HTML4.01, Obsolete since HTML5
The number of pixels of white space to insert above and below the image. In HTML5, use the margin CSS property instead.

Supported image formats

The HTML standard doesn't give a list of image formats that must be supported, so each user agent supports a different set of formats. Gecko supports:

Interaction with CSS

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

Depending on its type, an image may have an intrinsic width and height. For some image types, however, intrinsic dimensions are not necessary. SVG images, for instance, may have no intrinsic dimensions if their root <svg> element doesn't have width or height set.

Errors

If an error occurs while trying to load or render the image, and an onerror event handler has been configured to handle the error event, that event handler will get called. This can happen in a number of situations, including:

  • The src attribute is empty or null.
  • The specified src URL is the same as the URL of the page the user is currently on.
  • The specified image is corrupted in some way that prevents it from being loaded.
  • The specified image's metadata is corrupted in such a way that it's impossible to retrieve its dimensions, and no dimensions were specified in the <img> element's attributes.
  • The specified image is in a format not supported by the user agent.

Examples

Example 1: Alternative text

<img src="mdn-logo-sm.png" alt="MDN">

MDN

<a href="/">MDN<img src="mdn-logo-sm.png" alt="MDN"></a>

MDN

Example 3: Using the srcset attribute

The src attribute is a 1x candidate in user agents that support 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 that support srcset when using 'w' descriptors. When the (max-width: 600px) media condition matches, the image will be 200px wide, otherwise it will be 50vw wide (50% of the viewport width).

 <img src="clock-demo-thumb-200.png" 
      alt="Clock" 
      srcset="clock-demo-thumb-200.png 200w,
          clock-demo-thumb-400.png 400w"
      sizes="(max-width: 600px) 200px, 50vw">

Specifications

Browser compatibility

Feature Chrome Edge Firefox Internet Explorer Opera Safari
Basic support Yes Yes Yes Yes Yes Yes
align Yes Yes Yes Yes Yes Yes
alt Yes Yes Yes Yes Yes Yes
border Yes Yes Yes Yes Yes Yes
crossorigin Yes Yes Yes Yes Yes Yes
height Yes Yes Yes Yes Yes Yes
hspace Yes Yes Yes Yes Yes Yes
ismap Yes Yes Yes Yes Yes Yes
longdesc Yes Yes Yes Yes Yes Yes
name Yes Yes Yes Yes Yes Yes
onerror Yes ? 51 ? Yes Yes
referrerpolicy 51 No 50 No 38 ?
sizes Yes Yes Yes Yes Yes Yes
src Yes Yes Yes Yes Yes Yes
srcset 34 Yes

38

32 — 521

No 21 8
usemap Yes Yes Yes Yes Yes Yes
vspace Yes Yes Yes Yes Yes Yes
width Yes Yes Yes Yes Yes Yes
Feature Android webview Chrome for Android Edge mobile Firefox for Android IE mobile Opera Android iOS Safari
Basic support Yes Yes Yes Yes Yes Yes Yes
align Yes Yes Yes Yes Yes Yes Yes
alt Yes Yes Yes Yes Yes Yes Yes
border Yes Yes Yes Yes Yes Yes Yes
crossorigin Yes Yes Yes Yes Yes Yes Yes
height Yes Yes Yes Yes Yes Yes Yes
hspace Yes Yes Yes Yes Yes Yes Yes
ismap Yes Yes Yes Yes Yes Yes Yes
longdesc Yes Yes Yes Yes Yes Yes Yes
name Yes Yes Yes Yes Yes Yes Yes
onerror Yes Yes ? 51 ? Yes Yes
referrerpolicy 51 51 No 50 No 38 ?
sizes Yes Yes Yes Yes Yes Yes Yes
src Yes Yes Yes Yes Yes Yes Yes
srcset 34 34 Yes

38

32 — 521

No 21 8
usemap Yes Yes Yes Yes Yes Yes Yes
vspace Yes Yes Yes Yes Yes Yes Yes
width Yes Yes Yes Yes Yes Yes Yes

1. From version 32 until version 52 (exclusive): this feature is behind the dom.image.srcset.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.

See also

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img