Go to main content
Bypass navigation bar HTML Reference HTML Tags HTML Tutorials
Bypass language selection
Bypass location bar

Location: Home > HTML Reference > HTML Tags > HTML img tag

Bypass main content

HTML img tag

Note: If you don't know what an element/tag is and/or how you must use it we recommend you read our HTML tags and attributes tutorial, which you can find in our HTML tutorials section.

Bypass main content

Description

The HTML img element inserts an image in the document, which location is set in the "src" attribute.

Images are great for displaying information that sometimes is hard to explain with text. But you shouldn't forget that people with visual disabilities may not be able to receive its content. This is the main reason for the existence of the "alt" attribute (and its mandatory nature in XHTML), which should be used by authors to provide an alternative textual version for the information displayed in the image.

Some attributes for this element have a strong presentational nature, reason for which they have been deprecated in HTML 4.01 in favor of style sheets. These attributes are: "align", "border", "hspace" and "vspace". Besides, the "name" attribute has been deprecated in XHTML 1.0. Because of this, all these attributes shouldn't be used anymore.

Examples

Code
<img src="http://www.htmlquick.com/img/p/link-to-us/button.jpg" alt="HTMLQuick.com logo" width="88" height="31" />
View
<abbr title=HTMLQuick.com logo" width="88" height="31" />

Attributes

id (name)

The "id" attribute assigns an identifier to the associated element. This identifier must be unique in the document and can be used to refer to that element in other instances (e.g., from client-side scripts).

<p id="paragraph1">This is the first paragraph, named "paragraph1". To dynamically change its properties use this identifier.</p>

class (cdata)

The "class" attribute assigns a class name (or a list of class names separated by spaces) to the container element. It is used together with style sheets and tells the browser the class (or classes) to which the element is associated with.

A class gives presentational attributes to elements (read more at the Cascading Style Sheets tutorial).

<p class="references">This article is based on the book "Wind in the trees" by Jhon L. Brooks</p>
<p class="references important">This article is based on the book "Wind in the trees" by Jhon L. Brooks... and is more important than the one before.</p>

style (style)

This attribute is used to define presentational attributes for the containing element, and its value should be composed by style sheets properties. Although in some cases it can become useful, a better practice is to place presentational attributes in external files, relating them to elements with the "class" attribute. This way you keep the semantic and presentational parts of your document separated.

You can find more information about presentational attributes at the Cascading Style Sheets tutorial.

<p style="color: #0000FF; font-size: 12pt">This is a paragraph with a defined style.</p>
<p>And this is another text without style.</p>

title (text)

The purpose of this attribute is to provide a title for the element. Its value must be a short and accurate description of the element. Browsers usually render it as a "tool tip" when the user puts the mouse pointer over the element for a short period of time.

Code
<a title="HTMLQuick.com" href="http://www.htmlquick.com">HTML code</a>
View

lang (langcode)

Specifies the language of an element's content. The default value is "unknown".

When writing XHTML 1.0 documents, the attribute used to specify the language of an elements is "xml:lang". For forward and backward compatibility both attributes can be used simultaneously as in the example below. Note that in XHTML 1.1 the "lang" attribute has been completely replaced by "xml:lang" and its use is no longer valid.

<p lang="en" xml:lang="en">This is a paragraph in English.</p>
<p lang="es" xml:lang="es">Este es un párrafo en español.</p>

dir

This attribute indicates the direction in which the texts of the element must be read. This includes content, attribute values and tables. It has two possible values that are case-insensitive:

  • RTL: Right to left.
  • LTR: Left to right.
<q lang="he" dir="rtl">...an Hebrew quotation...</q>

src (uri)

This attribute contains a URI that is supposed to point to the location of the image resource. This is the element's most important attribute, and it is mandatory, because without it there would be no image to display.

<img src="img/treasure-map.jpg" />

alt (text)

The value of this attribute provides a textual alternative for the image's content, and therefore must describe exactly what the image intends to show. This is vital when the development of the website needs to reach visually impaired audiences.

In XHTML 1.0, this attribute became mandatory for the element and therefore shouldn't be omitted.

<img src="img/treasure-map.jpg" alt="This map displays the route to the treasure. You should turn right twice and follow straight until you find it." />

longdesc (uri)

This attribute's content points to a resource with extended alternative information for the image's content. Therefore, the destination document, should expand and complement the information set in the "alt" attribute.

<img src="img/treasure-map.jpg" alt="This map displays the route to the treasure. You should turn right twice and follow straight until you find it." longdesc="maps-explanation.html" />

width (length)

Specifies the width of the image. This can be different to the real width of the image, and browsers should resize it as needed. However, specifiyng a different width may cause pixelated images (when bigger) or longer load periods (when smaller).

<img src="img/treasure-map.jpg" width="100" />

height (length)

Specifies the height of the image. This can be different to the real height of the image, and browsers should resize it as needed. However, specifiyng a different height may cause a pixelated images (when bigger) or longer load periods (when smaller).

<img src="img/treasure-map.jpg" height="50" />

ismap

When this boolean attribute is present, the image is identified as a server-side image map.

When using the HTML img element, a server-side image map must be composed by an image with the "ismap" attribute present, inserted into a link's content. This way, when the image is clicked, the link is followed and the coordinates where the click occurred are sent to the destination document.

These coordinates will define the number of pixels from the top-left corner of the image, and will be named using the value of the "name" attribute of the image: "elem_name.x" for the horizontal coordinate and "elem_name.y" for the vertical.

Remember that in XHTML, boolean attributes must take their own names as value (e.g., attr_name="attr_name").

<a href="menu-post.php" /><img src="img/menu.png" ismap="ismap" /></a>

usemap (uri)

The value of this attribute points to an image map (HTML map element) that will be associated to this element. Therefore, this value must match the value of the "name" attribute in the image map.

<map name="mainmenu">
...Map info...
</map>
<img src="../img/mainmenu.png" alt="Main menu" usemap="mainmenu" />

name (cdata)

This attribute assigns a name to the element, that can be used to refer to it in scripts or style sheets.

In XHTML 1.0 the "name" attribute for this element has been deprecated in favor of the "id" attribute and in XHTML 1.1 it's simply invalid. Therefore, its use is no longer recommended.

<img name="photo1" src="img/photo1.png" alt="Mary and me" />

align

This attribute has been deprecated in HTML 4.01. Therefore its use is no longer recommended.

It specifies the position of the image with respect to the surrounding content. It can take the following case-insentive values:

  • top: The top side of the image is vertically aligned with the baseline.
  • middle: The image is vertically centered with respect to the baseline.
  • bottom: The bottom side of the image is vertically aligned with the baseline. This is the default value.
  • left: The image is floated to the left margin.
  • right: the image is floated to the right margin.
<img src="img/picture.png" align="center" />

border (pixels)

This attribute has been deprecated in HTML 4.01. Therefore its use is no longer recommended.

This attribute sets the border width in number of pixels.

<img src="img/picture.png" border="2" />

hspace (pixels)

This attribute has been deprecated in HTML 4.01. Therefore its use is no longer recommended.

Sets the amount of white space (in pixels) to be inserted at the left and right margins of the image.

<img src="img/picture.png" hspace="20" />

vspace (pixels)

This attribute has been deprecated in HTML 4.01. Therefore its use is no longer recommended.

Sets the amount of white space (in pixels) to be inserted at the top and bottom margins of the image.

<img src="img/picture.png" vspace="10" />

Events

  • onclick
  • ondblclick
  • onmousedown
  • onmouseup
  • onmouseover
  • onmousemove
  • onmouseout
  • onkeypress
  • onkeydown
  • onkeyup

See a complete list and information about events in HTML

Diseño y desarrollo: Latitud29.com

Links and logos|Contact|Beyond HTML|Tools and resources|Sitemap|Webmaster|Donate