The HTMLImageElement interface provides special properties and methods for manipulating the layout and presentation of <img> elements.
<div id="interfaceDiagram" style="display: inline-block; position: relative; width: 100%; padding-bottom: 20%; vertical-align: middle; overflow: hidden;"><svg style="display: inline-block; position: absolute; top: 0; left: 0;" viewbox="-50 0 600 120" preserveAspectRatio="xMinYMin meet"><a xlink:href="https://developer.mozilla.org/en-US/docs/Web/API/EventTarget" target="_top"><rect x="1" y="1" width="110" height="50" fill="#fff" stroke="#D4DDE4" stroke-width="2px" /><text x="56" y="30" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">EventTarget</text></a><polyline points="111,25 121,20 121,30 111,25" stroke="#D4DDE4" fill="none"/><line x1="121" y1="25" x2="151" y2="25" stroke="#D4DDE4"/><a xlink:href="https://developer.mozilla.org/en-US/docs/Web/API/Node" target="_top"><rect x="151" y="1" width="75" height="50" fill="#fff" stroke="#D4DDE4" stroke-width="2px" /><text x="188.5" y="30" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">Node</text></a><polyline points="226,25 236,20 236,30 226,25" stroke="#D4DDE4" fill="none"/><line x1="236" y1="25" x2="266" y2="25" stroke="#D4DDE4"/><a xlink:href="https://developer.mozilla.org/en-US/docs/Web/API/Element" target="_top"><rect x="266" y="1" width="75" height="50" fill="#fff" stroke="#D4DDE4" stroke-width="2px" /><text x="303.5" y="30" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">Element</text></a><polyline points="341,25 351,20 351,30 341,25" stroke="#D4DDE4" fill="none"/><line x1="351" y1="25" x2="381" y2="25" stroke="#D4DDE4"/><a xlink:href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement" target="_top"><rect x="381" y="1" width="110" height="50" fill="#fff" stroke="#D4DDE4" stroke-width="2px" /><text x="436" y="30" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">HTMLElement</text></a><polyline points="491,25 501,20 501,30 491,25" stroke="#D4DDE4" fill="none"/><line x1="501" y1="25" x2="509" y2="25" stroke="#D4DDE4"/><line x1="509" y1="25" x2="509" y2="90" stroke="#D4DDE4"/><line x1="509" y1="90" x2="492" y2="90" stroke="#D4DDE4"/><a xlink:href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement" target="_top"><rect x="331" y="65" width="160" height="50" fill="#F4F7F8" stroke="#D4DDE4" stroke-width="2px" /><text x="411" y="94" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">HTMLImageElement</text></a></svg></div>
a:hover text { fill: #0095DD; pointer-events: all;}
Properties
Inherits properties from its parent, HTMLElement.
HTMLImageElement.align- Is a
DOMStringindicating the alignment of the image with respect to the surrounding context. The possible values are"left","right","justify", and"center". HTMLImageElement.alt- Is a
DOMStringthat reflects thealtHTML attribute, thus indicating fallback context for the image. HTMLImageElement.border- Is a
DOMStringthat is responsible for the width of the border surrounding the image. This is now deprecated and the CSSborderproperty should be used instead. HTMLImageElement.completeRead only- Returns a
Booleanthat istrueif the browser has finished fetching the image, whether successful or not. It also shows true, if the image has nosrcvalue. HTMLImageElement.crossOrigin- Is a
DOMStringrepresenting the CORS setting for this image element. See CORS settings attributes for further details. HTMLImageElement.currentSrcRead only- Returns a
DOMStringrepresenting the URL to the currently displayed image (which may change, for example in response to media queries). HTMLImageElement.height- Is a
unsigned longthat reflects theheightHTML attribute, indicating the rendered height of the image in CSS pixels. HTMLImageElement.hspace- Is a
longrepresenting the space on either side of the image. HTMLImageElement.isMap- Is a
Booleanthat reflects theismapHTML attribute, indicating that the image is part of a server-side image map. HTMLImageElement.longDesc- Is a
DOMStringrepresenting the URI of a long description of the image. HTMLImageElement.lowSrc- Is a
DOMStringthat refers to a low-quality (but faster to load) copy of the image. HTMLImageElement.name- Is a
DOMStringrepresenting the name of the element. HTMLImageElement.naturalHeightRead only- Returns a
unsigned longrepresenting the intrinsic height of the image in CSS pixels, if it is available; else, it shows0. HTMLImageElement.naturalWidthRead only- Returns a
unsigned longrepresenting the intrinsic width of the image in CSS pixels, if it is available; otherwise, it will show0. HTMLImageElement.referrerPolicy- Is a
DOMStringthat reflects thereferrerpolicyHTML attribute indicating which referrer to use in order to fetch the image. HTMLImageElement.src- Is a
DOMStringthat reflects thesrcHTML attribute, containing the full URL of the image including base URI. HTMLImageElement.sizes- Is a
DOMStringreflecting thesizesHTML attribute. HTMLImageElement.srcset- Is a
DOMStringreflecting thesrcsetHTML attribute, containing a list of candidate images, separated by a comma (',', U+002C COMMA). A candidate image is a URL followed by a'w'with the width of the images, or an'x'followed by the pixel density. HTMLImageElement.useMap- Is a
DOMStringthat reflects theusemapHTML attribute, containing a partial URL of a map element. HTMLImageElement.vspace- Is a
longrepresenting the space above and below the image. HTMLImageElement.width- Is a
unsigned longthat reflects thewidthHTML attribute, indicating the rendered width of the image in CSS pixels. HTMLImageElement.xRead only- Returns a
longrepresenting the horizontal offset from the nearest layer. This property mimics an old Netscape 4 behavior. HTMLImageElement.yRead only- Returns a
longrepresenting the vertical offset from the nearest layer. This property is also similar to behavior of an old Netscape 4.
Methods
Inherits properties from its parent, HTMLElement.
Image()- The
Image()constructor, taking two optionalunsignedlong, which are the width and the height of the resource, creates an instance ofHTMLImageElement, not inserted in a DOM tree.
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
srcattribute is empty ornull. - The specified
srcURL 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.
Example
var img1 = new Image(); // HTML5 Constructor
img1.src = 'image1.png';
img1.alt = 'alt';
document.body.appendChild(img1);
var img2 = document.createElement('img'); // use DOM HTMLImageElement
img2.src = 'image2.jpg';
img2.alt = 'alt text';
document.body.appendChild(img2);
// using first image in the document
alert(document.images[0].src);
Specifications
| Specification | Status | Comment |
|---|---|---|
| Referrer Policy The definition of 'referrer attribute' in that specification. |
Working Draft | Added the referrerPolicy property. |
| CSS Object Model (CSSOM) View Module The definition of 'Extensions to HTMLImageElement' in that specification. |
Working Draft | Added the x and y properties. |
| WHATWG HTML Living Standard The definition of 'HTMLImageElement' in that specification. |
Living Standard | The following properties have been added: srcset, currentSrc and sizes. |
| HTML5 The definition of 'HTMLImageElement' in that specification. |
Recommendation | A constructor (with 2 optional parameters) has been added. The following properties are now obsolete: name, border, align, hspace, vspace, and longdesc.The following properties are now unsigned long, instead of long: height, and width.The following properties have been added: crossorigin, naturalWidth, naturalHeight, and complete. |
| Document Object Model (DOM) Level 2 HTML Specification The definition of 'HTMLImgElement' in that specification. |
Recommendation | The lowSrc property has been removed.The following properties are now long, instead of DOMString: height, width, hspace, and vspace. |
| Document Object Model (DOM) Level 1 Specification The definition of 'HTMLImgElement' in that specification. |
Recommendation | Initial definition. |
Browser compatibility
| Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
|---|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | 1.0 (1.7 or earlier) | (Yes) | (Yes) | (Yes) |
lowSrc |
(Yes) | (Yes) | (Yes) | ? | (Yes) | (Yes) |
naturalWidth, naturalHeight |
(Yes) | (Yes) | (Yes) | 9 | (Yes) | (Yes) |
crossorigin |
(Yes) | (Yes) | (Yes) | 9 | (Yes) | (Yes) |
complete |
(Yes) | (Yes) | (Yes) | 5[4] | (Yes) | (Yes) |
srcset |
34 | (Yes) | 32 (32)[2] | No support | 21 | 7.1 |
currentSrc |
(Yes) | (Yes) | 32 (32)[2] | No support | (Yes) | No support |
sizes |
(Yes) | (Yes) | 33 (33)[3] | No support | (Yes) | No support |
x and y |
(Yes) | (Yes) | 14[1] | No support | (Yes) | (Yes) |
referrerPolicy |
51 | No support | 50 (50) | No support | No support | No support |
onerror event handler |
No support | ? | 51 (51)[5] | No support | (Yes) | (Yes) |
| Feature | Android Webview | Chrome for Android | Edge | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | 1.0 (1.0) | (Yes) | (Yes) | (Yes) |
lowSrc |
(Yes) | (Yes) | (Yes) | ? | ? | ? | ? |
naturalWidth, naturalHeight |
(Yes) | (Yes) | (Yes) | ? | 9 | ? | (Yes) |
crossorigin |
(Yes) | (Yes) | (Yes) | (Yes) | ? | ? | (Yes) |
complete |
(Yes) | (Yes) | (Yes) | (Yes) | ? | ? | (Yes) |
srcset |
34 | 34 | (Yes) | 32.0 (32)[2] | No support | No support | iOS 8 |
currentSrc |
(Yes) | (Yes) | (Yes) | 32.0 (32)[2] | No support | No support | No support |
sizes |
(Yes) | (Yes) | (Yes) | 33.0 (33)[3] | No support | No support | No support |
x and y |
(Yes) | (Yes) | (Yes) | 14[1] | No support | (Yes) | (Yes) |
referrerPolicy |
51 | 51 | No support | 50.0 (50) | No support | No support | No support |
onerror event handler |
No support | No support | ? | 51.0 (51) | No support | (Yes) | (Yes) |
[1] Though, these x and y properties were removed in Gecko 7.0 (Firefox 7.0 / Thunderbird 7.0 / SeaMonkey 2.4) but later, they were restored in Gecko 14.0 (Firefox 14.0 / Thunderbird 14.0 / SeaMonkey 2.11) for compatibility reasons.
[2] This feature is behind the dom.image.srcset.enabled preference, defaulting to false.
[3] This feature is behind the dom.image.picture.enabled preference, defaulting to false.
[4] IE reports false for broken images.
[5] May also be supported in earlier versions.
See also
- The HTML element implementing this interface:
<img>