The HTMLImageElement interface provides special properties and methods for manipulating <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;}
Constructor
Image()- The
Image()constructor, taking two optionalunsignedlongs, which are the width and the height of the resource, creates an instance ofHTMLImageElement, not inserted in a DOM tree. - Functionally equivalent to calling
document.createElement("img").
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.decoding- Returns a
DOMStringrepresenting a hint given to the browser on how it should decode the image. 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.
HTMLImageElement.decode()- Returns a
Promisethat resolves when the image is decoded and it is safe to append the image to the DOM. This prevents a decoding delay on the next frame.
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(); // Image 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 |
|---|---|---|
| CSS Object Model (CSSOM) View Module The definition of 'Extensions to HTMLImageElement' in that specification. |
Working Draft | Added the x and y properties. |
| 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. |
Obsolete | 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. |
Obsolete | Initial definition. |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
| Desktop | Mobile | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
HTMLImageElement | Chrome Full support 1 | Edge Full support Yes | Firefox Full support 1 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android ? |
Image() constructor | Chrome Full support 31 | Edge Full support Yes | Firefox Full support Yes | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support Yes | Opera Android ? | Safari iOS Full support Yes | Samsung Internet Android ? |
align | Chrome Full support 1 | Edge Full support 12 | Firefox ? | IE ? | Opera ? | Safari ? | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile ? | Firefox Android ? | Opera Android ? | Safari iOS ? | Samsung Internet Android ? |
alt | Chrome Full support 1 | Edge Full support 12 | Firefox ? | IE ? | Opera ? | Safari ? | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile ? | Firefox Android ? | Opera Android ? | Safari iOS ? | Samsung Internet Android ? |
border | Chrome Full support 1 | Edge Full support 12 | Firefox ? | IE ? | Opera ? | Safari ? | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile ? | Firefox Android ? | Opera Android ? | Safari iOS ? | Samsung Internet Android ? |
complete | Chrome Full support 1 | Edge Full support 12 | Firefox Full support Yes | IE
Full support
5
| Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support Yes | Opera Android ? | Safari iOS Full support Yes | Samsung Internet Android ? |
crossorigin | Chrome Full support 13 | Edge Full support Yes | Firefox Full support Yes | IE Full support 9 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support Yes | Opera Android ? | Safari iOS Full support Yes | Samsung Internet Android ? |
currentSrc | Chrome Full support 45 | Edge Full support 13 | Firefox
Full support
38
| IE No support No | Opera Full support Yes | Safari No support No | WebView Android Full support 45 | Chrome Android Full support 45 | Edge Mobile Full support Yes | Firefox Android
Full support
38
| Opera Android No support No | Safari iOS No support No | Samsung Internet Android ? |
decode() | Chrome Full support 64 | Edge ? | Firefox No support No | IE ? | Opera ? | Safari ? | WebView Android Full support 64 | Chrome Android Full support 64 | Edge Mobile ? | Firefox Android No support No | Opera Android ? | Safari iOS ? | Samsung Internet Android ? |
decoding | Chrome Full support 65 | Edge ? | Firefox Full support 63 | IE No support No | Opera Full support Yes | Safari No support No | WebView Android Full support 65 | Chrome Android Full support 65 | Edge Mobile ? | Firefox Android Full support 63 | Opera Android ? | Safari iOS No support No | Samsung Internet Android ? |
height | Chrome Full support 1 | Edge Full support 12 | Firefox ? | IE ? | Opera ? | Safari ? | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile ? | Firefox Android ? | Opera Android ? | Safari iOS ? | Samsung Internet Android ? |
hspace | Chrome Full support 1 | Edge Full support 12 | Firefox ? | IE ? | Opera ? | Safari ? | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile ? | Firefox Android ? | Opera Android ? | Safari iOS ? | Samsung Internet Android ? |
isMap | Chrome Full support 1 | Edge Full support 12 | Firefox ? | IE ? | Opera ? | Safari ? | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile ? | Firefox Android ? | Opera Android ? | Safari iOS ? | Samsung Internet Android ? |
longDesc | Chrome Full support 1 | Edge Full support 12 | Firefox ? | IE ? | Opera ? | Safari ? | WebView Android Full support Yes | Chrome Android ? | Edge Mobile ? | Firefox Android ? | Opera Android ? | Safari iOS ? | Samsung Internet Android ? |
lowSrc | Chrome Full support 1 | Edge Full support Yes | Firefox Full support Yes | IE ? | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android ? | Opera Android ? | Safari iOS ? | Samsung Internet Android ? |
name | Chrome Full support 1 | Edge Full support 12 | Firefox ? | IE ? | Opera ? | Safari ? | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile ? | Firefox Android ? | Opera Android ? | Safari iOS ? | Samsung Internet Android ? |
naturalHeight | Chrome Full support 1 | Edge Full support 12 | Firefox Full support Yes | IE Full support 9 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android ? | Opera Android ? | Safari iOS Full support Yes | Samsung Internet Android ? |
naturalWidth | Chrome Full support 1 | Edge Full support 12 | Firefox Full support Yes | IE Full support 9 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android ? | Opera Android ? | Safari iOS Full support Yes | Samsung Internet Android ? |
onerror | Chrome No support No | Edge ? | Firefox
Full support
51
| IE No support No | Opera Full support Yes | Safari Full support Yes | WebView Android No support No | Chrome Android No support No | Edge Mobile ? | Firefox Android Full support 51 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android ? |
referrerPolicy | Chrome Full support 51 | Edge No support No | Firefox Full support 50 | IE No support No | Opera Full support 38 | Safari Full support 11.1 | WebView Android Full support 51 | Chrome Android Full support 51 | Edge Mobile No support No | Firefox Android Full support 50 | Opera Android Full support 38 | Safari iOS No support No | Samsung Internet Android ? |
sizes | Chrome Full support 45 | Edge Full support 13 | Firefox
Full support
38
| IE No support No | Opera Full support Yes | Safari No support No | WebView Android Full support 45 | Chrome Android Full support 45 | Edge Mobile Full support Yes | Firefox Android
Full support
38
| Opera Android No support No | Safari iOS No support No | Samsung Internet Android ? |
srcset | Chrome Full support 34 | Edge Full support 12 | Firefox
Full support
38
| IE No support No | Opera Full support 21 | Safari Full support 8 | WebView Android Full support 37 | Chrome Android Full support 34 | Edge Mobile Full support Yes | Firefox Android
Full support
38
| Opera Android No support No | Safari iOS Full support 8 | Samsung Internet Android ? |
src | Chrome Full support 1 | Edge Full support 12 | Firefox ? | IE ? | Opera ? | Safari ? | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile ? | Firefox Android ? | Opera Android ? | Safari iOS ? | Samsung Internet Android ? |
useMap | Chrome Full support 1 | Edge Full support 12 | Firefox ? | IE ? | Opera ? | Safari ? | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile ? | Firefox Android ? | Opera Android ? | Safari iOS ? | Samsung Internet Android ? |
vspace | Chrome Full support 1 | Edge Full support 12 | Firefox ? | IE ? | Opera ? | Safari ? | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile ? | Firefox Android ? | Opera Android ? | Safari iOS ? | Samsung Internet Android ? |
width | Chrome Full support 1 | Edge Full support 12 | Firefox ? | IE ? | Opera ? | Safari ? | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile ? | Firefox Android ? | Opera Android ? | Safari iOS ? | Samsung Internet Android ? |
x | Chrome Full support 1 | Edge Full support 12 | Firefox
Full support
14
| IE No support No | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android
Full support
14
| Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android ? |
y | Chrome Full support 1 | Edge Full support 12 | Firefox
Full support
14
| IE No support No | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android
Full support
14
| Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android ? |
Legend
- Full support
- Full support
- No support
- No support
- Compatibility unknown
- Compatibility unknown
- Experimental. Expect behavior to change in the future.
- Experimental. Expect behavior to change in the future.
- See implementation notes.
- See implementation notes.
- User must explicitly enable this feature.
- User must explicitly enable this feature.
See also
- The HTML element implementing this interface:
<img>
Document Tags and Contributors
Tags:
Contributors to this page:
mdnwebdocs-bot,
jpmedley,
Sheppy,
mfluehr,
sideshowbarker,
chrisdavidmills,
ExE-Boss,
fscholz,
trusktr,
mussali,
Markus Prokott,
erikadoyle,
slimsag,
EmileSonneveld,
ccnokes,
PushpitaPikuDey,
venkatbharadwaj,
teoli,
gilly3,
Sebastianz,
fastest963,
DomiR,
realityking,
Zcorpan,
louuis,
phistuck,
Svl,
jaypeagi,
Brettz9,
kirilloid,
kscarfone,
sethstone,
ethertank,
lcarlson,
trevorh,
jswisher,
JorisW,
Yanmorin
Last updated by:
mdnwebdocs-bot,