Displaying an Image (<img>)
As in HTML, the <img> element is an empty element that you use to insert images into Web pages. This element is supported in XHTML 1.0 Strict, XHTML 1.0 Transitional, XHTML 1.0 Frameset, and XHTML 1.1. Here are its attributes (which apply to XHTML 1.0 Strict, XHTML 1.0 Transitional, XHTML 1.0 Frameset, and XHTML 1.1, unless otherwise noted):
Attribute | Description |
---|
align | Sets the alignment of text relative to the image on the screen. Possible settings are left, right, top, texttop, middle, absmiddle, baseline, bottom, and absbottom. (XHTML 1.0 Transitional, XHTML 1.0 Frameset.) |
alt | Is required. This attribute holds the text that should be displayed instead of an image for browsers that cannot handle graphics or have graphics disabled. |
border | Specifies whether the image has a border and, if so, how thick the border is. Set this to 0 for no border, or a positive integer pixel value. (XHTML 1.0 Transitional, XHTML 1.0 Frameset.) |
class | Gives the style class of the element. |
height | Specifies the height of the image, in pixels. |
hspace | Sets the horizontal spacing (both left and right sides) around the image. Set this to pixel measurements. (XHTML 1.0 Transitional, XHTML 1.0 Frameset.) |
id | Use the ID to refer to the element; set this attribute to a unique identifier. |
ismap | Specifies that this image is to be used as an image map along with a map file. |
lang | Specifies the base language used in the element. Applies only when the document is interpreted as HTML. |
longdesc | Contains a longer description of the image. Allows descriptions that can contain markup. Set this to a URI. |
src | Is required. Specifies the URI of the image to display. |
style | Gives the inline style indicating how to render the element. |
title | Contains the title of the body (which might be displayed in ToolTips). |
usemap | Specifies the URI—usually inside the current document—of a client-side image map. |
vspace | Sets the vertical spacing around the image. Set this to pixel measurements. (XHTML 1.0 Transitional, XHTML 1.0 Frameset.) |
width | Indicates the width of the image. Set this to pixel measurements. |
xml:lang | Specifies the base language for the element when the document is interpreted as an XML document. |
This element supports these XHTML events: onclick, ondblclick, onmousedown, onmouseup, onmouseover, onmousemove, onmouseout, onkeypress, onkeydown, and onkeyup.
You use the XHTML <img> element to insert images into a Web page as well as image maps. When you use this element, you supply the URI of the image in the src attribute. Besides src, the alt attribute, which specifies alternate text to display in case the image can't be displayed, is required.
Interestingly, the align attribute is not deprecated in the <img> element as it is for virtually every other XHTML element that supports it.
Here's a simple example using the <img> element:
<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/tr/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>
Using the <img> Element
</title>
</head>
<body>
<center>
<h1>
Using the <img> Element
</h1>
<img src="image.jpg"
width="428" height="86" alt="an image" />
</center>
</body>
</html>
You can see the result in Figure 17.1.