HTML Images

In this lesson of the HTML tutorial, you will learn...
  1. To add images to a website.
  2. To create image links.
  3. To make images accesible.

Modern browsers generally support three types of images: GIFs, JPEGs, and PNGs. The PNG and GIF are generally used for simple images such as drawing; whereas the JPEG format is used for more complicated images such as photographs.

Inserting Images

Inserting images in web pages is done by placing an img tag in the HTML code. The img tag's src attribute is used to reference an image file using a relative or absolute path. The syntax is as follows.

Syntax
<img src="path_to_image_file" alt="alternative text"/>

The <img /> tag is an empty tag and should be closed with a shortcut close. The following page shows how to use the <img /> tag:

Code Sample: Images/Demos/Images.html

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Images</title>
</head>
<body>
<h1>Images</h1>
<p><img src="http://www.google.com/images/logo.gif" alt="Logo" /></p>
<hr />
<p><img src="Images/Pooh.jpg" alt="Pooh Bear" /></p>
<hr />
<p><img src="Images/RunnersHome.gif" alt="Runners Home" /></p>
</body>
</html>
Code Explanation

The page will render as follows:

Note that img elements may not be direct children of the body element.

Making Images Accessible

Alternative Text

To add alternate text for an image, use the alt attribute.

<img src="logo.gif" alt="Logo" />

Alternate text is displayed...

  • When the user's browser does not support images.
  • As the image is downloading.
  • When the user hovers over the image with the mouse.

Alternate text can also be used by screenreaders to describe an image for the visually impaired.

Long Descriptions

If an image depicts something complicated, such as a graph or chart, a long description of the image can be provided on a separate page. The longdesc attribute of the <img /> tag is used to point to that description. For example:

Code Sample: Images/Demos/LongDesc.html

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Long Description</title>
</head>
<body>
<h1>Long Description</h1>
<p><img src="Images/BigMac.jpg" alt="Big Mac Nutrition Information" longdesc="BigMac.html" /></p>
</body>
</html>
Code Explanation

The browsers don't do anything with the longdesc value, but modern screenreaders make use of it. Freedom Scientific, the makers of Jaws, one of the most popular screenreaders, say this about their support for longdesc:

JAWS now supports the "Longdesc" attribute in HTML within Internet Explorer 5.x and 6. The "Longdesc" attribute allows a long description of graphics to be provided on a separate page. The "Longdesc" attribute contains the address of the descriptive page. After reading any Alt text for the graphic, JAWS announces there is a long description and the address of the page. Just press ENTER to open the page containing the long description in a new window.

Image Links

Creating image links is easy. Simply put an <a> tag around your image, like so...

<a href="index.html" title="Link to Home Page"><img src="images/logo.gif" /></a>

By default, most browsers will place a one-pixel border around a linked image.

For examples of linked images, see Images/Demos/ImageLinks.html.

HTML Images Conclusion

In this lesson of the HTML tutorial you have learned to add images to a web page, to make those images accessible, and to create image links .

To continue to learn HTML go to the top of this page and click on the next lesson in this HTML Tutorial's Table of Contents.

Use of this website implies agreement to the following:

Copyright Information

All pages and graphics on this Web site are the property of Webucator, Inc. unless otherwise specified.

None of the content on this website may be redistributed or reproduced in any way, shape, or form without written permission from Webucator, Inc.

No Printing or saving of web pages

This content may not be printed or saved. It is for online use only.


Linking to this website

You may link to any of the pages on this website; however, you may not include the content in a frame or iframe without written permission from Webucator, Inc.


Warranties

This website is provided without warranty of any kind. There are no guarantees that use of the site will not be subject to interruptions. All direct or indirect risk related to use of the site is borne entirely by the user. All code and explanations provided on this site are provided without warranties to correctness, performance, fitness, merchantability, and/or any other warranty (whether expressed or implied).

For individual private use only

You agree not to use this online manual to deliver or receive training. If you are delivering or attending a class that is making use of this online manual, you are in violation of our terms of service. Please report any abuse to courseware@webucator.com. If you would like to deliver or receive training using this manual, please fill out the form at http://www.webucator.com/Contact.cfm.