Tag <img> HTML Reference



Example

How to insert an image:

<img src="image.png" alt="The image" height="64" width="64" />

Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

1. Definition and Usage

The <img> tag defines an image in an HTML page.

The <img> tag has two required attributes: src and alt.

Note: Images are not technically inserted into an HTML page, images are linked to HTML pages. The <img> tag creates a holding space for the referenced image.

Tip: To link an image to another document, simply nest the <img> tag inside <a> tags.


2. Differences Between HTML and XHTML

In HTML the <img> tag has no end tag.

In XHTML the <img> tag must be properly closed.


3. Required Attributes

DTD indicates in which HTML 4.01/XHTML 1.0 DTD the attribute is allowed. S=Strict, T=Transitional, and F=Frameset.

AttributeValueDescriptionDTD
alttextSpecifies an alternate text for an imageSTF
srcURLSpecifies the URL of an imageSTF

4. Optional Attributes

AttributeValueDescriptionDTD
aligntop
bottom
middle
left
right
Deprecated. Use styles instead.
Specifies the alignment of an image according to surrounding elements
TF
borderpixelsDeprecated. Use styles instead.
Specifies the width of the border around an image
TF
heightpixels
%
Specifies the height of an imageSTF
hspacepixelsDeprecated. Use styles instead.
Specifies the whitespace on left and right side of an image
TF
ismapismapSpecifies an image as a server-side image-mapSTF
longdescURLSpecifies the URL to a document that contains a long description of an imageSTF
usemap#mapnameSpecifies an image as a client-side image-mapSTF
vspacepixelsDeprecated. Use styles instead.
Specifies the whitespace on top and bottom of an image
TF
widthpixels
%
Specifies the width of an imageSTF

5. Standard Attributes

The <img> tag supports the following standard attributes:

AttributeValueDescriptionDTD
classclassnameSpecifies a classname for an elementSTF
dirrtl
ltr
Specifies the text direction for the content in an elementSTF
ididSpecifies a unique id for an elementSTF
langlanguage_codeSpecifies a language code for the content in an elementSTF
stylestyle_definitionSpecifies an inline style for an elementSTF
titletextSpecifies extra information about an elementSTF
xml:langlanguage_codeSpecifies a language code for the content in an element, in XHTML documentsSTF

6. Event Attributes

The <img> tag supports the following event attributes:

AttributeValueDescriptionDTD
onabortscriptScript to be run when loading of an image is interruptedSTF
onclickscriptScript to be run on a mouse clickSTF
ondblclickscriptScript to be run on a mouse double-clickSTF
onmousedownscriptScript to be run when mouse button is pressedSTF
onmousemovescriptScript to be run when mouse pointer movesSTF
onmouseoutscriptScript to be run when mouse pointer moves out of an elementSTF
onmouseoverscriptScript to be run when mouse pointer moves over an elementSTF
onmouseupscriptScript to be run when mouse button is releasedSTF
onkeydownscriptScript to be run when a key is pressedSTF
onkeypressscriptScript to be run when a key is pressed and releasedSTF
onkeyupscriptScript to be run when a key is releasedSTF

Relative articles