<IMG>NN all   IE all   HTML all
<IMG>End Tag: Forbidden
 

The IMG element displays a graphical image in whatever MIME types the browser is equipped to handle. Common image MIME types are GIF and JPEG. IMG elements are inline elements, appearing anywhere in the document you specify, including in the middle of a line of text. A large number of attributes affecting visual presentation of the element are deprecated in HTML 4.0 in favor of style sheet rules. You will be able to use the attributes safely for many browser generations to come, however, because of the need to be backward compatible with the large collection of image-laden documents already on the Web. Note, too, that if you intend to use style sheets for IMG element borders and margins in Navigator 4, you must wrap the IMG element inside DIV or SPAN elements and assign the style sheets to the surrounding element. This workaround works with Internet Explorer, too, so you can use style sheets in cross-browser deployment.

If you want to make an entire image a clickable link, wrap the IMG element inside an A element. To eliminate the typical link border around the image, set the BORDER attribute to 0. And for image maps (where different segments of an image link to different destinations), the HTML recommendation encourages the use of client-side image maps (via the USEMAP attribute) over the server-side image map (ISMAP).

To be backward compatible with earlier scriptable browsers, it is advisable to include HEIGHT and WIDTH attribute assignments in all IMG element tags. When values are assigned to these attributes, the browser renders pages more quickly because it doesn't have to wait for the image to load in order to determine its size and organize other content on the page.

 
Example
<IMG SRC="mtRushmore.jpg" HEIGHT=90 WIDTH=120>
 
Object Model Reference
NN [window.]document.imageName
[window.]document.images[i]
IE [window.]document.imageName
[window.]document.images[i]
[window.]document.all.elementID
ALIGNNN all   IE all   HTML all
ALIGN="where"Optional
 

Determines how the IMG element is rendered in physical relation to the element's next outermost container and surrounding content. Some settings also let you "float" the image to the left or right margin and let surrounding text wrap around the image (but no wrapping with a centered image).

Most of the rules for alignment constant values cited at the beginning of this chapter apply to the IMG element. Typically, ALIGN attributes are deprecated in HTML 4.0 in favor of the style sheet attributes. But if you require backward compatibility for your document, stick with the ALIGN attribute.

 
Example
<IMG SRC="surferDude.gif" ALIGN="right" HEIGHT=100 WIDTH=200>
 
Value
Each browser defines a different set of values for this attribute. Although the ALIGN attribute has a long heritage, not all values do. The more esoteric values, such as absmiddle and baseline, were added to browser offerings in Navigator 3 and Internet Explorer 4. Select the value(s) from the following table that work for your deployment:
Value NN 4 IE 4 HTML 4.0
absbottom
* * -
absmiddle
* * -
baseline
* * -
bottom
* * *
left
* * *
middle
* * *
right
* * *
texttop
* * -
top
* * *
 
Default bottom
 
Object Model Reference
IE [window.]document.imageName.align
[window.]document.images[i].align
ALTNN all   IE all   HTML all
ALT="textMessage"Required
 

In a world littered with graphical browsers, it is often hard to remember that not everyone can, or chooses to, download images. Aside from those using VT100 terminals with browsers such as Lynx, pocket computers often offer better performance when images don't have to be downloaded and rendered. To replace the image in such a browser, the text assigned to the ALT attribute is displayed where the IMG element appears on the page. The ALT attribute should contain a brief description of what the image is. The HTML recommendation calls the ALT attribute a requirement for the IMG element, but in practice, graphical browsers can get by without it. Still, some browsers display the ALT text initially as the image downloads from the server, providing a temporary alternate display. Be aware that the size of the image area on the page may limit the amount of text you can use for ALT. Make sure the description is readable.

 
Example
<IMG SRC="navbar.gif" USEMAP="#nav" ALT="Navigation Bar" WIDTH=400 HEIGHT=50>
 
Value
Any quoted string of characters.
 
Default None.
 
Object Model Reference
IE [window.]document.imageName.alt
[window.]document.images[i].alt
BORDERNN all   IE all   HTML 3.2
BORDER=pixelsOptional
 

Navigator provides a dedicated attribute to specifying the thickness of a border around an IMG element. Default rendering of the border is in black, but if the IMG element is wrapped inside an A element, the border takes on the document's various link colors (depending on link state). If you want a different color for a plain border, use style sheets (with the appropriate DIV or SPAN wrapper for Navigator 4). When a link surrounds the image, you can eliminate the colored border altogether by setting the BORDER attribute size to zero.

 
Example
<IMG SRC="surferDude.gif" BORDER=3 HEIGHT=100 WIDTH=200>
 
Value
Any integer pixel value.
 
Default 0
 
Object Model Reference
NN [window.]document.imageName.border
[window.]document.images[i].border
IE [window.]document.imageName.border
[window.]document.images[i].border
DATAFLDNN n/a   IE 4   HTML n/a
DATAFLD="columnName"Optional
 

Used with IE 4 data binding to associate a remote data source column name with the SRC attribute URL of an IMG element. The data source column must contain an absolute or relative URL. A DATASRC attribute must also be set for the IMG element.

 
Example
<IMG DATASRC="#DBSRC3" DATAFLD="img3URL" HEIGHT=100 WIDTH=150>
 
Value
Case-sensitive identifier.
 
Default None.
 
Object Model Reference
IE [window.]document.imageName.dataFld
[window.]document.images[i].dataFld
DATASRCNN n/a   IE 4   HTML n/a
DATASRC="dataSourceName"Optional
 

Used with IE 4 data binding to specify the name of the remote ODBC data source (such as an Oracle or SQL Server database) to be associated with the element. Content from the data source is specified via the DATAFLD attribute.

 
Example
<IMG DATASRC="#DBSRC3" DATAFLD="img3URL" HEIGHT=100 WIDTH=150>
 
Value
Case-sensitive identifier.
 
Default None.
 
Object Model Reference
IE [window.]document.imageName.dataSrc
[window.]document.images[i].dataSrc
DYNSRCNN n/a   IE 4   HTML n/a
DYNSRC="URL"Optional
 

Internet Explorer 4 allows video clips (and VRML) to be displayed via the IMG element (as an alternate to the EMBED or OBJECT element). To help the browser differentiate between a dynamic and static image source, you use the DYNSRC attribute in place of the SRC attribute to load the video clip. All other visual aspects of the IMG element are therefore immediately applicable to the rectangular region devoted to playing the video clip. See also the LOOP attribute for controlling the frequency of clip play and the START attribute.

 
Example
<IMG DYNSRC="snowman.avi" LOOP=3 HEIGHT=100 WIDTH=150>
 
Value
Any valid URL, including complete and relative URLs.
 
Default None.
 
Object Model Reference
IE [window.]document.images[i].dynsrc
[window.]document.imageName.dynsrc
HEIGHT, WIDTHNN all   IE all   HTML 3.2
HEIGHT="length"
WIDTH="length"
Optional
 

Define the dimensions for the space on the page reserved for the image, regardless of the actual size of the image. For best performance (and backward script compatibility), you should set these attributes to the actual height and width of the source image. If you supply a different measure, the browser scales the image to fit the space defined by these attributes.

 
Example
<IMG SRC="surferDude.gif" HEIGHT=100 WIDTH=200>
 
Value
Positive integer values (optionally quoted) or percentage values (quoted).
 
Default Actual size of source image.
 
Object Model Reference
NN [window.]document.imageName.height
[window.]document.images[i].height
[window.]document.imageName.width
[window.]document.images[i].width
IE [window.]document.imageName.height
[window.]document.images[i].height
[window.]document.imageName.width
[window.]document.images[i].width
HSPACE, VSPACENN all   IE all   HTML 3.2
HSPACE=pixelCount
VSPACE=pixelCount
Optional
 

Define a margin that acts as whitespace padding around the visual content of the IMG element. The HSPACE establishes a margin on the left and right sides of the image rectangle; the VSPACE establishes a margin on the top and bottom sides of the image rectangle. Use the margin or padding attributes to duplicate the same functionality with style sheets because these attributes are deprecated in HTML 4.0.

 
Example
<IMG SRC="desk3.gif" VSPACE=10 HSPACE=10>
 
Value
Integer representing the number of pixels for the width of the margin on the relevant sides of the IMG element's rectangle.
 
Default 0
 
Object Model Reference
NN [window.]document.imageName.hspace
[window.]document.images[i].hspace
[window.]document.imageName.vspace
[window.]document.images[i].vspace
IE [window.]document.imageName.hspace
[window.]document.images[i].hspace
[window.]document.imageName.vspace
[window.]document.images[i].vspace
IDNN 4   IE 3   HTML 4
ID="elementIdentifier"Optional
 

A unique identifier that distinguishes this element from all the rest in the document. Can be used to associate a single element with a style rule naming this attribute value as an ID selector. An element can have an ID assigned for uniqueness as well as a class for inclusion within a group.

If you assign an ID attribute and not a NAME attribute, the value of the ID attribute cannot be used reliably as the IMG element's name in script reference forms that use the element name. Some browser platforms insist on the NAME attribute being used in an image name reference.

 
Example
<IMG SRC="desk3.gif" ID="desk3" HEIGHT=90 WIDTH=120>
 
Value
Case-sensitive identifier.
 
Default None.
 
Object Model Reference
IE [window.]document.imageName.id
[window.]document.images[i].id
ISMAPNN all   IE all   HTML all
ISMAPOptional
 

The Boolean ISMAP attribute tells the browser that the IMG element is acting as a server-side image map. To turn an image into a server-side image map, wrap the IMG element with an A element whose HREF attribute points to the URL of the CGI program that knows how to interpret the click coordinate information. The browser appends coordinate information about the click to the URL like a GET form method appends form element data to the ACTION attribute URL. In the following example, if a user clicks at the coordinate point 50, 25, the browser sends "http://www.giantco.com/cgi-bin/nav?50,25" to the server. A server CGI program named nav might examine the region in which the coordinate point appears and send the relevant HTML back to the client.

More recent browsers allow client-side image maps (see the USEMAP attribute), which operate more quickly for the user because there is no communication with the server to carry out the examination of the click coordinate point.

 
Example
<A HREF="http://www.giantco.com/cgi-bin/nav" TARGET="main">
<IMG SRC="navbar.gif" ISMAP HEIGHT=90 WIDTH=120>
</A>
 
Value
The presence of the attribute turns the feature on.
 
Default Off.
 
Object Model Reference
IE [window.]document.imageName.isMap
[window.]document.images[i].isMap
LONGDESCNN n/a   IE n/a   HTML 4
LONGDESC="URL"Optional
 

Specifies a URL of a document that contains a longer description of the element than what the content of the ALT or TITLE attributes reveal. One application of this attribute in future browsers is to retrieve an annotated description of the element for users who cannot read the browser screen.

 
Example
<IMG LONGDESC="navDesc.html" ALT="Navigation Bar" SRC="navbar.jpg">
 
Value
Any valid URL, including complete and relative URLs.
 
Default None.
LOOPNN n/a   IE 3   HTML n/a
LOOP="loopCount"Optional
 

If you specify a video clip with the DYNSRC attribute, the LOOP attribute controls how many times the clip should play ("loop") after it loads. If you set the value to zero, the clip loads but does not play initially. Video clips that are not currently running play when the user double-clicks on the image, but you may need to provide instructions for that on the page because there are no other obvious controls.

 
Example
<IMG DYNSRC="snowman.avi" LOOP=3 HEIGHT=100 WIDTH=150>
 
Value
Any positive integer or zero.
 
Default 1
 
Object Model Reference
IE [window.]document.imageName.loop
[window.]document.images[i].loop
LOWSRCNN 3   IE 4   HTML n/a
LOWSRC="URL"Optional
 

Both Navigator and Internet Explorer recognize the fact that not everyone has a fast Internet connection and that high-resolution images can take a long time to download to the client. To fill the void, the LOWSRC attribute lets the author specify a URL of a lower-resolution (or alternate) image to download into the document space first. The LOWSRC image should be the same pixel size as the primary SRC image.

 
Example
<IMG SRC="navbar.jpg" LOWSRC="navbarBW.jpg" HEIGHT=60 WIDTH=300>
 
Value
Any valid URL, including complete and relative URLs.
 
Default None.
 
Object Model Reference
NN [window.]document.imageName.lowsrc
[window.]document.images[i].lowsrc
IE [window.]document.imageName.lowsrc
[window.]document.images[i].lowsrc
NAMENN 3   IE 4   HTML n/a
NAME="elementIdentifier"Optional
 

If you are scripting an image (especially swapping precached images), it is usually more convenient to create a reference to the IMG element by using a unique name you assign to the item. Thus, if you edit the page and move or delete multiple IMG elements on the page, you do not have to worry about adjusting index values to array-style references (document.images[i]).

 
Example
<IMG NAME="mugshot" SRC="janem.jpg" HEIGHT=90 WIDTH=80>
 
Value
Case-sensitive identifier.
 
Default None.
 
Object Model Reference
NN [window.]document.images[i].name
[window.]document.imageName.name
IE [window.]document.images[i].name
[window.]document.imageName.name
SRCNN all   IE all   HTML all
SRC="URL"Required
 

URL to a file containing image data that is displayed through the IMG element. With the exception of specifying a DYNSRC attribute in Internet Explorer for video clips, the SRC attribute is required if you want to see any image in the IMG element space. The browser must be equipped to handle the image MIME type. On the World Wide Web, the most common image formats are GIF and JPEG.

 
Example
<IMG SRC="surferDude.gif" HEIGHT=100 WIDTH=200>
 
Value
A complete or relative URL.
 
Default None.
 
Object Model Reference
NN [window.]document.images[i].src
[window.]document.imageName.src
IE [window.]document.images[i].src
[window.]document.imageName.src
STARTNN n/a   IE 4   HTML n/a
START="fileopen" | "mouseover"Optional
 

Whenever you set the DYNSRC attribute of an IMG to display a video clip, you can direct the element to start playing the video immediately after the video file loads or when the user rolls the cursor over the image. The START attribute lets you decide the best user interface for your page.

 
Example
<IMG DYNSRC="snowman.avi" LOOP=1 START="mouseover" HEIGHT=100 WIDTH=150>
 
Value
One of the two case-insensitive constant values: fileopen | mouseover.
 
Default fileopen
 
Object Model Reference
IE [window.]document.images[i].start
[window.]document.imageName.start
STYLENN 4   IE 3   HTML 4
STYLE="styleSheetProperties"Optional
 

This attribute lets you set one or more style sheet rule property assignments for the current element. You may use the CSS or JavaScript syntax for assigning style attributes. But if you are designing the page for cross-browser deployment, use only the CSS syntax, which both Navigator and Internet Explorer support.

For use in Version 4 browsers, style sheets are recommended over dedicated attributes where applicable, but due to the implementation in Navigator 4, you need to wrap IMG elements with DIV or SPAN elements that contain border and margin-related style sheet rules to make them work with IMG elements. The following example with the embedded STYLE attribute works only in Internet Explorer.

 
Example
<IMG STYLE="padding-left:5px" SRC="surferDude.gif" HEIGHT=150 WIDTH=250>
 
Value
An entire CSS-syntax style sheet rule is enclosed in quotes. Multiple style attribute settings are separated by semicolons. Style sheet attributes are detailed in .
 
Default None.
 
Object Model Reference
IE [window.]document.images[i].style
[window.]document.imageName.style
USEMAPNN all   IE all   HTML 3.2
USEMAP="mapURL"Optional
 

You can define a client-side image map with the help of the MAP and AREA elements. The MAP element is a named container for one or more AREA element. Each AREA element sets a "hot" area on an image and assigns a link destination (and other settings) for a response to the user clicking in that region. The purpose of the USEMAP attribute is to establish a connection between the IMG element and a named MAP element in the same document. In some respects, the MAP element's name is treated like an anchor in that the "address" of the MAP element is the element's name preceded by a # symbol.

 
Example
<IMG SRC="navbar.gif" USEMAP="#navigation" HEIGHT=90 WIDTH=120>
 
Value
A URL to the MAP element in the same document (a hash symbol plus the MAP name).
 
Default None.
 
Object Model Reference
IE [window.]document.imageName.useMap
[window.]document.images[i].useMap
Hosted by uCoz