The HTML <img>
element represents an image in 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 |
This element includes the global attributes.
align
Deprecated since HTML4.01, Obsolete since HTML5 float
and/or vertical-align
CSS properties instead.alt
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
border
CSS property instead.crossorigin
HTML5
<canvas>
element without being "tainted." The allowed values are:anonymous
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
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.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.height
hspace
Deprecated since HTML4.01, Obsolete since HTML5
margin
CSS property instead.ismap
longdesc
name
Deprecated since HTML4.01, Obsolete since HTML5
id
attribute instead.referrerpolicy
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.sizes
HTML5
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
<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.srcset
HTML5
w
'. The width descriptor is divided by the source size given in the sizes
attribute to calculate the effective pixel density.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
usemap
vspace
Deprecated since HTML4.01, Obsolete since HTML5
margin
CSS property instead.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:
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, have no intrinsic dimensions.
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:
src
attribute is empty or null
.src
URL is the same as the URL of the page the user is currently on.<img>
element's attributes.<img src="mdn-logo-sm.png" alt="MDN">
<a href="/">MDN<img src="mdn-logo-sm.png" alt="MDN"></a>
srcset
attributeThe 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">
srcset
and sizes
attributesThe src
attribute is ignored in user agents that support srcset
when using 'w
' descriptors. When the (min-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="(min-width: 600px) 200px, 50vw">
Specification | Status | Comment |
---|---|---|
Referrer Policy The definition of 'referrer attribute' in that specification. | Working Draft | Added the referrerpolicy attribute. |
WHATWG HTML Living Standard The definition of '<img>' in that specification. | Living Standard | |
HTML5 The definition of '<img>' in that specification. | Recommendation | |
HTML 4.01 Specification The definition of '<img>' in that specification. | Recommendation |
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
srcset | 34.0 | (Yes) | 32.0 (32.0)[2] | No support | 21 | 7.1 |
referrerpolicy | 46.0[1] | ? | 50.0 (50.0) | ? | ? | ? |
onerror event handler | (Yes) | ? | 51 (51) | ? | (Yes) | (Yes) |
Feature | Android | Android Webview | Edge | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
srcset | ? | 34.0 | (Yes) | No support | No support | 21 | iOS 8 | 34.0 |
referrerpolicy | ? | 46.0 [1] | No support | 50.0 (50.0) | ? | ? | ? | 46.0[1] |
onerror event handler | ? | (Yes) | ? | 51.0 (51) | ? | ? | (Yes) | (Yes) |
[1] Implemented as referrerpolicy
and behind a flag.
[2] Implemented behind a preference.
© 2005–2017 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