Skip to main content

HTML Image Maps

With HTML image maps, you can make clickable areas on an image.


Image Maps

The <map> tag defines an image map: an image with clickable areas. The areas are defined with one or more <area> tags.

Example

An image map with clickable computer, phone, and coffee areas.

Prompt
A page with an image map over workplace.jpg using a map named workmap and three clickable areas: a rectangle over the computer (computer.htm), a rectangle over the phone (phone.htm), and a circle over the coffee cup (coffee.htm).
Output: complete HTML page

How Does it Work?

The idea of an image map is that you can do different things depending on where in the image you click. To make one you need an image plus some HTML that describes the clickable areas.


The Image

The image is added with the <img> tag. The only difference from a normal image is that you add a usemap attribute. Its value starts with a # followed by the image map's name, which links the image to the map.

Tip: You can use any image as an image map.

Image with usemap

An image linked to a map with the usemap attribute.

index.html
<img src="workplace.jpg" alt="Workplace" usemap="#workmap">

Create Image Map

Next, add a <map> element. It is linked to the image by its required name attribute, which must match the image's usemap value.

Map element

The opening <map> tag with its name.

index.html
<map name="workmap">

The Areas

Then add the clickable areas, each with an <area> element. You set the shape: rect for a rectangle, circle for a circle, poly for a polygon, or default for the whole image. You also set coordinates to place the area on the image.


Shape="rect"

For shape="rect", the coordinates come in pairs of x (from the left) and y (from the top). For example, 34,44 is 34 pixels from the left and 44 from the top, and 270,350 is the opposite corner. Together they make a clickable rectangle.

Example

A rectangular clickable area linking to computer.htm.

Prompt
A page with an image map over workplace.jpg with a single rectangular clickable area, coords 34,44,270,350, linking to computer.htm.
Output: complete HTML page

Shape="circle"

For shape="circle", give the coordinates of the center, then the radius. For example, a center of 337,300 and a radius of 44 pixels make a clickable circle.

Example

A circular clickable area linking to coffee.htm.

Prompt
A page with an image map over workplace.jpg with a single circular clickable area centered at 337,300 with radius 44, linking to coffee.htm.
Output: complete HTML page

Shape="poly"

For shape="poly", give several coordinate pairs to make a shape from straight lines. This can form any shape, such as a croissant, by finding the x and y coordinates for all its edges.

Example

A polygonal clickable area following a croissant's outline.

Prompt
A page with an image map over a croissant image using a polygonal clickable area following the croissant's edges, linking to croissant.htm.
Output: complete HTML page

Image Map and JavaScript

A clickable area can also run a JavaScript function. Add a click event to the <area> element with the onclick attribute.

Example

An area that shows an alert when clicked.

Prompt
A page with an image map where a circular area over the coffee cup runs a JavaScript function on click that alerts "You clicked the coffee cup!"
Output: complete HTML page

Chapter Summary

To recap: use the <map> element to define an image map, the <area> element to define each clickable area, and the usemap attribute on the <img> element to point to the map.


HTML Image Tags

The image-related tags are: <img> (defines an image), <map> (defines an image map), <area> (defines a clickable area inside an image map), and <picture> (defines a container for multiple image resources).