tadam logo

HTML <img> Tag

HTML <img> Tag

Example

An image is marked up as follows:
<img src="/images/w3html.png" alt="HTML" width="120" height="95" />
The output of the code above will be:

Definition and Usage

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

Notice that 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.

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

Browser Support

The <img> tag is supported in all major browsers

Differences Between HTML and XHTML

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

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

Tips and Notes

Tip: The alt attribute is meant to be used as an alternative text if the image is not available, not as a mouse-over text. To show a mouse-over text on images or image-maps, use the title attribute, like this: <img src="/images/w3html.png" alt="HTML" width="120" height="95" />

Note: When a web page is loaded, it is the browser, at that moment, that actually gets the image from a web server and inserts it into the page. Therefore, make sure that the images actually stay in the same spot in relation to the web page, otherwise your visitors will get a broken link icon. The broken link icon is shown if the browser cannot find the image.

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

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-map. Rarely used. Look at usemap insteadSTF
longdescURLSpecifies the URL to a document that contains a long description of an imageSTF
titletextSpecifies a title text for 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

Standard Attributes

Тег <img> поддерживает следующие стандартные атрибуты:
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
More information about Standard Attributes.

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

More information about Event Attributes.

More examples

Example 1

How to insert images from another folder or from another web site.
<p>Insert an image from another folder:</p>
<img src="/images/w3html.png" alt="HTML" width="120" height="95" />

<p>Insert an image from a web site:</p>
<img src="http://xhtml.co.il/images/logo/logo_jQuery_xhtml.gif" alt="jQuery" width="120" height="95" />
The output of the code above will be:

Example 2

How to align an image within a text.
<p>
An image 
<img src="/images/blue-green-axe.gif" alt="Axe" align="bottom" width="75" height="75" /> 
in the text
An image 
<img src="/images/blue-green-axe.gif" alt="Axe" align="middle" width="75" height="75" /> 
in the text
An image 
<img src="/images/blue-green-axe.gif" alt="Axe" align="top" width="75" height="75" /> 
in the text
</p>
The output of the code above will be:

Example 3

How to let an image float to the left or right of a text.
<p>
<img src="/images/blue-green-axe.gif" alt="Axe" align="left" width="75" height="75" /> 
A paragraph with an image. The align attribute of the image is set to "left". The image will float to the left of this text.
</p>

<p>
<img src="/images/blue-green-axe.gif" alt="Axe" align="right" width="75" height="75" />
A paragraph with an image. The align attribute of the image is set to "right". The image will float to the right of this text.
</p>
The output of the code above will be:

Example 4

How to add a hyperlink to an image.
An image that is a link:
<a href="http://Xhtml.co.il/en/">
<img src="/images/w3html.png" alt="HTML" width="120" height="95" border="0" />
</a>
The output of the code above will be:

Example 5

An image-map, with clickable areas, and a target attribute:
<p>Click on one of the planets to watch it closer:</p>

<img src="/images/353915main_image_1377_428-321.gif" alt="Planets" width="350" height="263" usemap="#planetmap">
<map id="planetmap" name="planetmap">
<area shape="rect" coords="203,104,343,241" alt="Sun" title="sun" href="http://xhtml.co.il/tryit/sun.html" target="_blank" >
<area shape="circle" coords="92,98,56" alt="Mercury" title="Mercury" href="http://xhtml.co.il/tryit/mercury.html">
</map>
The output of the code above will be:
Was this information helpful?
   

Comments