Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

HTMLImageElement

BaselineWidely available *

TheHTMLImageElement interface represents an HTML<img> element, providing the properties and methods used to manipulate image elements.

EventTarget Node Element HTMLElement HTMLImageElement

Constructor

Image()

TheImage() constructor creates and returns a newHTMLImageElement object representing an HTML<img> element which is not attached to any DOM tree. It accepts optional width and height parameters. When called without parameters,new Image() is equivalent to callingdocument.createElement('img').

Instance properties

Inherits properties from its parent,HTMLElement.

HTMLImageElement.alt

A string that reflects thealt HTML attribute, thus indicating the alternate fallback content to be displayed if the image has not been loaded.

HTMLImageElement.attributionSrcSecure contextExperimental

Gets and sets theattributionsrc attribute on an<img> element programmatically, reflecting the value of that attribute.attributionsrc specifies that you want the browser to send anAttribution-Reporting-Eligible header along with the image request. On the server-side this is used to trigger sending anAttribution-Reporting-Register-Source orAttribution-Reporting-Register-Trigger header in the response, to register an image-basedattribution source orattribution trigger, respectively.

HTMLImageElement.completeRead only

Returns a boolean value that istrue if the browser has finished fetching the image, whether successful or not. That means this value is alsotrue if the image has nosrc value indicating an image to load.

HTMLImageElement.crossOrigin

A string specifying the CORS setting for this image element. SeeCORS settings attributes for further details. This may benull if CORS is not used.

HTMLImageElement.currentSrcRead only

Returns a string representing the URL from which the currently displayed image was loaded. This may change as the image is adjusted due to changing conditions, as directed by anymedia queries which are in place.

HTMLImageElement.decoding

An optional string representing a hint given to the browser on how it should decode the image. If this value is provided, it must be one of the possible permitted values:sync to decode the image synchronously,async to decode it asynchronously, orauto to indicate no preference (which is the default). Read thedecoding page for details on the implications of this property's values.

HTMLImageElement.fetchPriority

An optional string representing a hint given to the browser on how it should prioritize fetching of the image relative to other images. If this value is provided, it must be one of the possible permitted values:high to fetch at a high priority,low to fetch at a low priority, orauto to indicate no preference (which is the default).

HTMLImageElement.height

An integer value that reflects theheight HTML attribute, indicating the rendered height of the image in CSS pixels.

HTMLImageElement.isMap

A boolean value that reflects theismap HTML attribute, indicating that the image is part of a server-side image map. This is different from a client-side image map, specified using an<img> element and a corresponding<map> which contains<area> elements indicating the clickable areas in the image. The imagemust be contained within an<a> element; see theismap page for details.

HTMLImageElement.loading

A string providing a hint to the browser used to optimize loading the document by determining whether to load the image immediately (eager) or on an as-needed basis (lazy).

HTMLImageElement.naturalHeightRead only

Returns an integer value representing the intrinsic height of the image in CSS pixels, if it is available; else, it shows0. This is the height the image would be if it were rendered at its natural full size.

HTMLImageElement.naturalWidthRead only

An integer value representing the intrinsic width of the image in CSS pixels, if it is available; otherwise, it will show0. This is the width the image would be if it were rendered at its natural full size.

HTMLImageElement.referrerPolicy

A string that reflects thereferrerpolicy HTML attribute, which tells theuser agent how to decide which referrer to use in order to fetch the image. Read this article for details on the possible values of this string.

HTMLImageElement.sizes

A string reflecting thesizes HTML attribute. This string specifies a list of comma-separated conditional sizes for the image; that is, for a given viewport size, a particular image size is to be used. Read the documentation on thesizes page for details on the format of this string.

HTMLImageElement.src

A string that reflects thesrc HTML attribute, which contains the full URL of the image including base URI. You can load a different image into the element by changing the URL in thesrc attribute.

HTMLImageElement.srcset

A string reflecting thesrcset HTML attribute. This specifies a list of candidate images, separated by commas (',', U+002C COMMA). Each candidate image is a URL followed by a space, followed by a specially-formatted string indicating the size of the image. The size may be specified either the width or a size multiple. Read thesrcset page for specifics on the format of the size substring.

HTMLImageElement.useMap

A string reflecting theusemap HTML attribute, containing the page-local URL of the<map> element describing the image map to use. The page-local URL is a pound (hash) symbol (#) followed by the ID of the<map> element, such as#my-map-element. The<map> in turn contains<area> elements indicating the clickable areas in the image.

HTMLImageElement.width

An integer value that reflects thewidth HTML attribute, indicating the rendered width of the image in CSS pixels.

HTMLImageElement.xRead only

An integer indicating the horizontal offset of the left border edge of the image's CSS layout box relative to the origin of the<html> element's containing block.

HTMLImageElement.yRead only

The integer vertical offset of the top border edge of the image's CSS layout box relative to the origin of the<html> element's containing block.

Obsolete properties

HTMLImageElement.alignDeprecated

A string indicating the alignment of the image with respect to the surrounding context. The possible values are"left","right","justify", and"center". This is obsolete; you should instead use CSS (such astext-align, which works with images despite its name) to specify the alignment.

HTMLImageElement.borderDeprecated

A string which defines the width of the border surrounding the image. This is deprecated; use the CSSborder property instead.

HTMLImageElement.hspaceDeprecated

An integer value which specifies the amount of space (in pixels) to leave empty on the left and right sides of the image.

HTMLImageElement.longDescDeprecated

A string specifying the URL at which a long description of the image's contents may be found. This is used to turn the image into a hyperlink automatically. Modern HTML should instead place an<img> inside an<a> element defining the hyperlink.

HTMLImageElement.nameDeprecated

A string representing the name of the element.

HTMLImageElement.vspaceDeprecated

An integer value specifying the amount of empty space, in pixels, to leave above and below the image.

Instance methods

Inherits methods from its parent,HTMLElement.

HTMLImageElement.decode()

Returns aPromise that resolves when the image is decoded and it's safe to append the image to the DOM. This prevents rendering of the next frame from having to pause to decode the image, as would happen if an undecoded image were added to the DOM.

Errors

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

  • Thesrc attribute is empty ornull.
  • The specifiedsrc 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 theuser agent.

Example

js
const img1 = new Image(); // Image constructorimg1.src = "image1.png";img1.alt = "alt";document.body.appendChild(img1);const img2 = document.createElement("img"); // Use DOM HTMLImageElementimg2.src = "image2.jpg";img2.alt = "alt text";document.body.appendChild(img2);// using first image in the documentalert(document.images[0].src);

Specifications

Specification
HTML
# htmlimageelement

Browser compatibility

See also

  • The HTML element implementing this interface:<img>

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp