CHAPTER 22 · HTML

Image maps: give different parts of one picture different links

An image can contain more than one destination. Build a small map with named regions, learn to read coordinates, and keep ordinary text links available so nobody has to guess where to click.

Download example files (ZIP)

When one image link is not enough

The previous image-link example made the whole picture one link. Imagine instead a college plan showing a reading room, a help desk and a meeting spot. Each part may need its own destination. A client-side image map describes those clickable regions in HTML; the browser uses the pointer position to choose the matching link.

This is useful for some diagrams, but ordinary labelled links are often simpler. Do not put your only navigation in an unexplained picture. People using keyboards, small screens or a missing-image view still need a clear route.

The three connected pieces

PartJob
img usemap="#campus"Connect the displayed image to a map named campus
map name="campus"Group the region definitions
areaDescribe a region's shape, coordinates and destination

Usemap contains a # followed by the map's name. The map name itself has no #. If you also give the map an id, make its value match its name. Keep map names unique within the document. Area is a void element and has no closing area tag.

Coordinates are positions in the image

A coordinate is a number locating a point. In these fixed-size examples, x measures rightwards from the left edge and y measures downwards from the top. The top-left corner is 0,0.

The supplied images/campus-map.svg is 360 by 220. Its reading-room rectangle extends from 30,40 to 130,130. Its round meeting spot has centre 250,90 and radius 40. A radius is the distance from the centre to the circle's edge.

ShapeCoords patternExample
rectLeft x, top y, right x, bottom y30,40,130,130
circleCentre x, centre y, radius250,90,40
polyAt least three x,y point pairs120,190,160,150,200,190
defaultNo coords needed; covers the remaining regionOmit coords

Polygon means a closed shape with straight edges. Its last point connects back to the first. Rect needs four numbers, circle needs three, and poly needs an even number of values forming at least three points. Use plain numbers, not px units or percentages.

Connect the picture to its link regions
  1. 1Img usemap: #campus
  2. 2Map name: campus
  3. 3Area shape and coords: the region
  4. 4Area href and alt: its named destination

Keep equivalent text links and test the measured display size.

Complete program: campus-regions.html

campus-regions.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Practice Campus Regions</title>
</head>
<body>
  <h1>Choose a practice campus place</h1>
  <p>Select the rectangle for reading or the circle for meeting.</p>
  <img src="images/campus-map.svg" alt="Practice campus plan: reading room rectangle and meeting circle" width="360" height="220" usemap="#campus">
  <map name="campus">
    <area shape="rect" coords="30,40,130,130" href="#reading" alt="Reading room details">
    <area shape="circle" coords="250,90,40" href="#meeting" alt="Meeting spot details">
  </map>
  <p><a href="#reading">Reading room details</a> or <a href="#meeting">Meeting spot details</a>.</p>
  <h2 id="reading">Reading room</h2>
  <p>Bring a book and keep your phone silent.</p>
  <h2 id="meeting">Meeting spot</h2>
  <p>Meet your practice partner before the activity.</p>
</body>
</html>
Browser previewDocument title: Practice Campus Regions

This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.

Expected result: the drawn rectangle and circle link to different sections. The image's own alt describes the overall plan; each linked area has alt naming its destination. Check both the image regions and the equivalent visible text links. A short page may not move far when the target is already visible.

Complete program: polygon-region.html

polygon-region.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>A Polygon on the Campus Plan</title>
</head>
<body>
  <h1>Find the practice help desk</h1>
  <p>The triangular mark points to the help desk section.</p>
  <img src="images/campus-map.svg" alt="Practice campus plan with a triangular help desk mark" width="360" height="220" usemap="#help-map">
  <map name="help-map">
    <area shape="poly" coords="120,190,160,150,200,190" href="#help" alt="Help desk details">
  </map>
  <p><a href="#help">Read the help desk details</a></p>
  <h2 id="help">Help desk</h2>
  <p>Bring the saved page and explain what you expected to see.</p>
</body>
</html>
Browser previewDocument title: A Polygon on the Campus Plan

This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.

The three points trace the supplied triangle. Move them deliberately and test how the clickable region changes. The picture itself does not change when coords changes: you are editing the invisible link region, not redrawing the image.

Overlapping areas and the default shape

If more than one area contains the chosen point, the first matching linked region in map order wins. Avoid unnecessary overlap. Put a default region last if you deliberately want a remaining-background destination. Label it truthfully and do not make an empty-looking background trigger a surprising action.

Image size and mobile limits

Coords are fixed numeric positions. Simply shrinking a picture with CSS does not automatically rewrite its coordinates to follow the resized features. Keep these demonstrations at their stated 360 by 220 size. If you need a responsive map, plan a reliable scaling approach or choose a more suitable design, such as labelled HTML links or an accessible SVG with links.

Do not wrap an img with usemap inside an ordinary anchor. That combines two competing link mechanisms. Also avoid attaching JavaScript actions just to make a navigation region work: href already supplies navigation.

Test more than the mouse

Try Tab and Enter on linked areas in your target browser. Check that their destination names make sense. Keyboard behaviour and focus visibility can vary, so keep and test the equivalent text links too. Do not assume that a pointer changing shape proves the map is usable for everyone.

Problem and solution: the clickable place is beside the drawing

Problem: a map was measured at 360 by 220, then its image was displayed at half that size. Clicking the visible room no longer finds the intended region.

Solution: restore the measured display dimensions and retest. For a truly resizing design, do not guess new values once and hope they fit every viewport. Use a planned responsive approach or simpler links that do not depend on coordinates.

Common mistakes and useful tips

  • Matching usemap to an unrelated id instead of the map name.
  • Putting # inside the map's name value.
  • Omitting alt on linked area elements.
  • Reversing rectangle corners or giving a circle four values.
  • Changing coords and expecting the image drawing to change.
  • Scaling the picture without checking the regions.
  • Offering no visible text alternatives for important navigation.

Practice questions

  1. How does usemap="#places" connect to a map?
  2. What are the four values for a rectangle?
  3. What does the third value of circle coords mean?
  4. What should a linked area's alt describe?
  5. Will fixed coordinates automatically follow an image resized with CSS?
  6. Save the campus example, test both regions and text alternatives, then change one coordinate. Explain what changed and what stayed the same.
Check your practice answers
  1. It refers to map name="places" in the document.
  2. The top-left x,y pair followed by the bottom-right x,y pair.
  3. The radius, measured from the centre to the edge.
  4. The destination or purpose of that region's link.
  5. No. A responsive design needs an appropriate scaling solution or another navigation approach.
  6. Use the supplied image at its stated dimensions. A coordinate edit changes the invisible region; it does not alter the image file. Restore sensible values and check keyboard access too.

Interview questions

How does an image map differ from an ordinary image link?

An ordinary linked image has one anchor destination. An image map defines multiple shaped link regions within one image through map and area.

Why give alt to each linked area as well as the image?

The image needs overall replacement text. Each linked area needs a useful destination name, just as an ordinary text link does.

What makes fixed image maps difficult on small screens?

The picture may need to resize, but fixed coordinates do not automatically adjust to the new feature positions. Small click targets and unclear labels create additional problems.

When would you avoid an image map?

When ordinary text links communicate the choices more clearly, or when a reliable responsive and accessible map cannot be provided. A visual effect alone is not a good reason to complicate navigation.

Quick revision

  • Img usemap refers to map name; area supplies each region.
  • Coordinates describe positions, not a new drawing.
  • Keep linked-region names and equivalent visible links clear.
  • Test size changes, region boundaries and keyboard navigation.