The HTML <img> tag represents an image in an HTML document. It is used for embedding the image into the document.The basic tag is written like this <img src="" alt=""> with the URL of the image inserted between the opening/closing quotes of the src attribute. You should also provide an alt attribute. This attribute is used to specify some text that will be displayed in the even that the image can't be displayed.

Note that, although the image is embedded into the page, it still exists independently of the document. The <img> element simply finds the image, and displays it within the content. If one were to delete or move the image at a later date, the image will no longer be able to be loaded by the HTML document. Similarly, if one were to change the image to a new image (but use the same name/URL of the old image), the new image will now be displayed whenever the HTML document is loaded.

Example

The following example shows the <img> element in action. You can modify the code (on the left) and click "Refresh" to see your changes take effect (on the right).
Source CodeResult

Attributes
The <img> tag accepts the following attributes. Try adding some to the above example to see how it affects the display/behavior of the element.

Attributes Specific to the <img> Element
AttributeDescription
altAlternate text. This specifies text to be used in case the browser/user agent can't render the image. The text should describe the image clearly. For example, it's better to use "Photo of Mt Cook on a sunny day" than to simply use "Mt Cook" or "Pic" or similar.
srcThis specifies the URL of the image to be embedded into the image.
crossoriginThis attribute is a CORS settings attribute. CORS stands for Cross-Origin Resource Sharing. The purpose of the crossorigin attribute is to allow you to configure the CORS requests for the element's fetched data. The values for the crossorigin attribute are enumerated.
Possible values:
ValueDescription
anonymousCross-origin CORS requests for the element will not have the credentials flag set. In other words, there will be no exchange of user credentials via cookies, client-side SSL certificates or HTTP authentication.
use-credentialsCross-origin CORS requests for the element will have the credentials flag set.
If this attribute is not specified, CORS is not used at all.
An invalid keyword and an empty string will be handled as the anonymous value.
ismapFor image maps. For more about image maps, see the <map> tag.
usemapFor image maps. For more about image maps, see the <map> tag.
widthSpecifies the width of the image.
heightSpecifies the height of the image.

Global Attributes

The <img> tag accepts the following global attributes. These attributes are standard across all HTML 5 tags.

AttributeDescription
accesskeySpecifies a shortcut key that can be used to access this <img> element.
Possible values.
[Any string of characters. This string of characters specifies the key/s the user needs to use in order to access the element.]
classThis is a document wide identifier. It is used to refer to a class that is specified in the style sheet. The value should match the name of the class you wish to use.
contenteditableThis attribute specifies whether the user can edit the content or not.
Possible values:
  • true
  • false
contextmenuThe contextmenu attribute sets a context menu for an element. The value must be the ID of a menu element in the DOM.
dirSpecifies the direction of the text.
Possible values:
ValueDescription
ltrSpecifies that the text should read left to right.
rtlThe text should read right to left.
autoThe text direction should be determined programatically using the contents of the element.
draggableSpecifies whether the user is allowed to drag this <img> element or not.
Possible values:
  • true
  • false
  • auto
ValueDescription
trueThis value specifies that the element is draggable.
falsefalse value specifies that the element is not draggable.
autoUses the default behavior of the user agent/browser. This is the default value.
dropzoneThe dropzone attribute specifies what should happen when the user "drops" an element (i.e. after dragging it) onto this <img> element.
Must be an unordered set of unique space-separated tokens that are ASCII case-insensitive.
Possible values:
ValueDescription
copyResults in a copy of the dragged data. Default value.
moveResults in the data being moved to the new location.
linkResults in a link to the original data.
Any keyword with eight characters or more, beginning with the an ASCII case-insensitive match for the string "string:"Specifies that items with the drag data item kind Plain Unicode string and the drag data item type string set to a value that matches the remainder of the keyword are accepted.
Any keyword with six characters or more, beginning with an ASCII case-insensitive match for the string "file:"Allows you to specify which file types can be processed (i.e. copied, moved or linked) in this dropzone. Example: dropzone="copy file:image/png file:image/gif file:image/jpeg"
Note that this attribute must not have more than one of the three feedback values (copy, move, and link) specified. If none are specified, the copy value is implied.
hiddenIndicates that this particular <img> element is not yet, or is no longer, relevant. The browser/user agent does not display elements that have the hidden attribute present.
This is a boolean attribute. If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace (i.e. either hidden or hidden="hidden").
Possible values:
  • [Empty string]
  • hidden
idThe id attribute is a document wide identifier, which is used in conjunction with CSS and JavaScript. The value must match the name of the id you wish to use.
itemidThe itemid provides a global identifier for an "item". This attribute is optional, however if it is provided, it must have a value that is a valid URL potentially surrounded by spaces.
The itemid attribute can only be present in elements that include both the itemscopeand the itemtype attributes, as long as the itemtype attribute specifies a vocabulary that supports global identifiers for items, as defined by that vocabulary's specification.
itempropThis attribute provides one or more properties to one or more "items".
Although this attribute is optional, if used it must have a value that is an unordered set of unique space-separated tokens that are case-sensitive, representing the names of the name-value pairs that it adds. The attribute's value must have at least one token. Each token must be one of the following:
  • A valid URL that is an absolute URL, or
  • If the item is a typed item: a "defined property name" allowed in this situation according to the specification that defines the relevant types for the item, or
  • If the item is not a typed item: a string that contains no U+002E FULL STOP characters (.) and no U+003A COLON characters (:).
Also, Specifications that introduce defined property names that are not absolute URLs must ensure all such property names contain no U+002E FULL STOP characters (.), no U+003A COLON characters (:), and no space characters.
itemrefThis attribute is used in conjunction with the itemscope attribute, the itemref attribute provides a list of additional elements to crawl to find the name-value pairs of the "item". Although the itemref attribute is optional, if specified, it must have a value that is an unordered set of unique space-separated tokens that are case-sensitive, consisting of IDs of elements in the same home subtree. Also, the itemref can only be used on elements that also have the itemscope attribute present.
itemscopeHTML5 elements that have the itemscope attribute create a name-value pair called an "item". Elements with an itemscope attribute may also have an itemtype attribute specified, to give the item types of the item.
This is a boolean attribute. If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace (i.e. either itemscope or itemscope="itemscope").
Possible values:
  • [Empty string]
  • itemscope
itemtypeThis attribute provides an item type for elements containing the itemscope attribute. The attribute is optional but if it is specified, it must have a value that is an unordered set of unique space-separated tokens that are case-sensitive, each of which is a valid URL that is an absolute URL, and all of which are defined to use the same vocabulary. The attribute's value must have at least one token.
The itemtype attribute must only be present in elements that include the itemscopeattribute.
langSets the language code to be used.
Possible values:
[Must be a valid RFC 3066 language code, or an empty string.]
spellcheckSpecifies whether the element should have its spelling checked.
ValueDescription
[Empty string]The element should have its spelling checked.
trueThe element should have its spelling checked.
falseThe element should not have its spelling checked.
If this attribute is missing, the element will use the default behavior, possibly based on the parent's own spellcheck state.
styleSpecifies inline styles for this <img> element. This allows you to define the styles within the page, and within this <img> tag, as opposed to referring to styles defined elsewhere (such as an external style sheet). Although this can be useful for over-riding external styles, it is usually preferrable to use external styles in conjunction with the classattribute and/or the id attribute.
tabindexHelps determine the tabbing order for this <img> element (for when the user uses the "tab" key on their keyboard to "tab" through the elements on the page in order to select an element).
Possible values:
[Any valid integer. For example, 0, 1, 2, 3, ...etc]
titleSpecifies a title to associate with this particular <img> element. Many browsers will display this when the cursor hovers over the element (similar to a "tool tip").
Possible values:
[Any text to be displayed as a "tool tip".]
translateDetermines whether the element's attribute values and the values of its Text node children are to be translated when the page is localized, or whether to leave them unchanged.
The translate attribute is an enumerated attribute and may contain the following possible values:
  • [Empty String]
  • yes
  • no
If the translate attribute is provided, but its value is missing or is invalid, the element will inherit its value from its parent element.

 

Event Handler Content Attributes

The <img> tag also accepts the event handler content attributes listed below.
An event handler content attribute is an attribute for a specific event handler. The name of the content attribute is the same as the name of the event handler.
Event handlers are commonly used to extend the functionality of an HTML element. By using any of the event handler content attributes below, you can tell the browser to run a specific script and when to run it. For example, by using onclick="", you tell the browser to run a piece of JavaScript whenever someone clicks on the element. The JavaScript needs to be inserted between the double quotes. This is typically a small piece of JavaScript that simply calls a JavaScript function that has been defined elsewhere.
Here are the event handler content attributes that can be used with the <img> element.
AttributeDescription
onabortInvoked when an event has been aborted. For example, the browser stops fetching media data before it is completely downloaded.
onblurUser has left the focus of the element.
oncancelcancel event handler.
oncanplayInvoked when the browser/user agent can start playing media, but hasn't yet, due to buffering. In other words, if playback were to begin now, it wouldn't play right to the end (due to the current playback rate) - it would need to pause during playback in order to load the rest of the media.
oncanplaythroughThe browser/user agent estimates that if playback were to begin now, the media resource could be rendered at the current playback rate all the way to its end without having to stop for further buffering.
onchangeUser has changed the object, then attempts to leave that field (i.e. clicks elsewhere).
onclickInvoked when the user clicked on the object.
oncloseclose event handler.
oncontextmenuInvoked when a context menu has been triggered.
oncuechangecuechange event handler.
ondblclickInvoked when the user clicked twice on the object.
ondragInvoked when an element is being dragged.
ondragendInvoked when an element has stopped being dragged.
ondragenterInvoked when an element has been dragged to a drop target.
ondragexitdragexit event handler.
ondragleaveInvoked when an element leaves a valid drop target.
ondragoverInvoked when an element is being dragged over a valid drop target.
ondragstartInvoked when a drag operation has started.
ondropInvoked when an element is being dropped.
ondurationchangeInvoked when the length of the media is changed (i.e. the duration attribute has just been updated).
onemptiedInvoked when a media resource element suddenly becomes empty (for example, due to a network error).
onendedInvoked when the media has reached the end.
onerrorInvoked when an error occurs while the element is being loaded. Also handler for script error notifications.
onfocusInvoked when the focus is on the element.
oninputinput event handler.
oninvalidinvalid event handler.
onkeydownInvoked when a key was pressed over an element.
onkeypressInvoked when a key was pressed over an element then released.
onkeyupInvoked when a key was released over an element.
onloadThe element has loaded.
onloadeddataInvoked when the browser/user agent can render the media data at the current playback position for the first time.
onloadedmetadataInvoked when the browser/user agent has just determined the duration and dimensions of the media resource.
onloadstartInvoked when the browser/user agent has started loading the media resource.
onmousedownThe cursor moved over the object and mouse/pointing device was pressed down.
onmousemoveThe cursor moved while hovering over an object.
onmouseoutThe cursor moved off the object
onmouseoverThe cursor moved over the object (i.e. user hovers the mouse over the object).
onmouseupThe mouse/pointing device was released after being pressed down.
onmousewheelInvoked when the mouse wheel is being rotated.
onpauseInvoked when the media resource has been paused.
onplayInvoked when the media resource starts playback.
onplayingPlayback has begun.
onprogressThe browser/user agent is fetching media data the.
onratechangeInvoked when the playback rate has changed (i.e. either the defaultPlaybackRate or the playbackRate has just been updated).
onresetreset event handler.
onresizeresize event handler.
onscrollscroll event handler. Invoked when the element's scrollbar is being scrolled.
onseekedInvoked when the seeking IDL attribute changed to false (i.e. the seeking attribute is no longer true)
onseekingInvoked when the seeking IDL attribute changed to true and the seek operation is taking long enough that the user agent has time to fire the event.
onselectInvoked when some or all of the contents of an object is selected. For example, the user selected some text within a text field.
onshowshow event handler.
onstalledInvoked when the browser/user agent is trying to fetch media data but the data has stalled (i.e. the data has stopped coming).
onsubmitUser submitted a form.
onsuspendThe browser/user agent is (intentionally) not currently fetching media data, but has not yet downloaded the entire media resource (i.e. it has suspended the download).
ontimeupdateInvoked when the media's current playback position changed.
onvolumechangeInvoked when either the volume attribute or the muted attribute has changed.
onwaitingThe next frame of the media is not yet available (but the browser/user agent expects it to become available).

More Information About the <img> Element

Content Categories
  • Flow content.
  • Phrasing content.
  • Embedded content.
  • Form-associated element.
  • If the element has a usemap attribute: Interactive content.
  • Palpable content.
Can be usedWhere embedded content is expected.
Content modelEmpty.
End Tag Required?No. You can only use a start tag when using the <img> element, it must not have a closing tag.
  • Right: <img>
  • Wrong: <img></img>
DOM InterfaceHTMLImageElement
 
Top