facebook google plus twitter
Webucator's Free HTML Tutorial

Lesson: HTML Images

Welcome to our free HTML tutorial. This tutorial is based on Webucator's Introduction to HTML Training course.

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.

Lesson Goals

  • Add images to a website.
  • Create image links.
  • Make images accessible.

Inserting Images

Inserting images in web pages is done by placing an img tag in the HTML code. The <img> tag is an empty tag. Its src attribute is used to reference an image file using a relative or absolute path. The syntax is shown below:


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

The following page shows how to use the <img> tag:

Code Sample:

<html lang="en">
<meta charset="UTF-8">
<p><img src="http://www.google.com/images/logo.gif" alt="Logo"></p>
<p><img src="Images/Pooh.jpg" alt="Pooh Bear"></p>
<p><img src="Images/RunnersHome.gif" alt="Runners Home"></p>

Code Explanation

The page will render as follows:Google Image Page Example

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 (in some browsers).

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:

<html lang="en">
<meta charset="UTF-8">
<title>Long Description</title>
<h1>Long Description</h1>
<p><img src="Images/BigMac.jpg" alt="Big Mac Nutrition Information" longdesc="BigMac.html"></p>

Code Explanation

The browsers don't do anything with the longdesc value, but modern screen readers make use of it. One example is JAWS, a most popular screen reader from Freedom Scientific.

Height and Width Attributes

The img element also takes height and width attributes that set the dimensions (in pixels) to use to display the image on the page. It is not good practice to "resize" the image using these attributes. If you use these attributes, you should set the values to the actual height and width of the image. Using the height and width attributes lets the browser know how much space to allocate for the image without having to wait for the image itself to download. Use of these attributes may promote faster rendering of the Web page.

<img src="logo.gif" alt="Logo" height="100" width="300">

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.

Adding Images to the Page

Duration: 5 to 10 minutes.

In this exercise, you will add an image to index.html. The resulting page is shown below.Welcome to Runners Home Page with Image

  1. Open Images/Exercises/index.html for editing.
  2. Add the image called RunnersHome.gif in the Images/Exercises/Images directory, so that the page appears as shown in the screenshot above.
    • The alternative text should be "Runners Home Logo".
    • There is no need for a long description.


<html lang="en">
<meta charset="UTF-8">
<title>Runners Home&trade;</title>
	<a href="index.html">Home</a> |
	<a href="Races.html">Races</a> |
	<a href="Resources.html">Resources</a> |
	<a href="Calculator.html">Calculator</a> |
	<a href="RunningLog.html">Running Log</a> |
	<a href="MyAccount.html">My Account</a> |
	<a href="Logout.html">Log out</a>
<p>Hello, Stranger!</p>
<h1>Welcome to Runners Home&trade;</h1>
	<img src="Images/RunnersHome.gif" alt="Logo"><br>
	Runners Home&trade; is dedicated to providing you with:<br>
	&#8226; <a href="Races.html">the most up-to-date information on running races</a>.<br>
	&#8226; <a href="Resources.html">the best resources for runners</a>.
<p>&copy; 2018 Runners Home. All rights reserved. For questions, send email to <a href="mailto:info@runnershome.com">info@runnershome.com</a>.</p>