img (HTML element)
Example
Here’s an
img element for which only the required attributes are
specified (src and alt):
<img src="giant-prawn.jpg" alt="The Giant Prawn at Ballina"/>
- Type
- empty element
- Contains
- nothing
- Contained by
- block-level
elements, inline elements
(except
pre)
Description
The
img element provides a means for embedding an image in
the document, which can be used for as many different purposes as your
imagination allows. With just the couple of required attributes shown in
the example, the img provides a reference to the image
file to display, and a text alternative should the image not be available
for whatever reason. A number of optional, and deprecated, attributes are
covered below in detail.
As it’s an empty element, the
img element requires a trailing slash if it’s to be
XHTML-compliant, but it can be expressed in HTML as
follows:
<img src="giant-prawn.jpg" alt="The Giant Prawn at Ballina">
As an image is an inline element, a break isn’t created before or after it, so the following HTML would render with the text appearing on either side of the image:
<p>Driving along, we spotted a giant prawn <img
src="giant-prawn.jpg" alt="The Giant Prawn at Ballina">,
so we had to stop and take a closer look.</p>
The somewhat unsightly effect of this markup can be seen in Figure 1.
However, with CSS you can achieve great control over the
img element, creating wrapping text with margins (or
gutters, to use the print analogy), border styles, and more.
Some presentational attributes, which control alignment and dimensions, are covered below, but these effects are best controlled using CSS.
Use This For …
This
element is used to place illustrative images—pictures that convey some
important information. It’s not used for purely decorative images that
don’t offer any information, and which could easily be removed from the
page without detriment to its content. Such noncritical, decorative images
may be better implemented using the CSS
background-image property.
This element can be used for photographs, charts and graphs, and maps. Even when it’s used for images that may be difficult to accurately describe in words (for instance, a trend may be seen easily on a graph, but its meaning may be impossible to understand in the absence of the image, for whatever reason), it’s important to ensure that an alternative description is available. See the sections on the alt and longdesc attributes for more information.
Compatibility
| IE | 5.5 | Full |
|---|---|---|
| 6.0 | Full | |
| 7.0 | Full | |
| Firefox | 1.0 | Full |
| 1.5 | Full | |
| 2.0 | Full | |
| Safari | 1.3 | Full |
| 2.0 | Full | |
| 3.0 | Full | |
| Opera | 9.2 | Full |
| 9.5 | Full |
It causes no compatibility issues, and has excellent support across all tested browsers.
In this Section
- align
specifies the alignment or position of theimgrelative to surrounding content - alt
provides alternative text for the image - border
sets the border thickness around the image in pixels - height
specifies the image height - hspace
specifies the amount of space to appear on either side of image - ismap
defines an image as a server-side image map - longdesc
provides a long description of the image - name
specifies the name by which theimgelement will be referenced - src
specifies the location, or source, of the image - usemap
defines a client-side image map for animgelement - vspace
sets the amount of vertical space to appear above and below an image - width
specifies the width of the image
Related Reading
User-contributed notes
Add a note
To post a note on this topic, please log in with your SitePoint username and password. If you don't have an account yet, you can create a new account for free.

