On this page8 sections
  1. Learning Objectives
  2. 1. Image Maps
  3. 2. Background Images
  4. 3. The <picture> Element
  5. Common Beginner Mistakes
  6. Practice Questions
  7. Practice Answers
  8. Key Takeaways

Lecture 012: Image Maps, Background Images & the Picture Element

Category3: Links, Images, Tables & Lists
Lecture012 of 048
Estimated video length12 min
TargetHTML5 · any modern browser
LevelBeginner

Three more ways to work with pictures: an image map makes different parts of one image clickable, a background image sits behind content, and <picture> lets the browser choose the best image for the screen.


Learning Objectives

  • Make clickable regions on an image with <map> and <area>
  • Put an image behind content with background-image
  • Serve different images to different screens with <picture> and <source>

1. Image Maps

An image map links an image to a <map> of clickable areas.

Example: click the left or right half of the picture:

html
<img src="https://picsum.photos/id/1062/400/200" alt="Two halves to click" width="400" height="200" usemap="#halves">

<map name="halves">
  <area shape="rect" coords="0,0,200,200" href="#left" alt="Left half">
  <area shape="rect" coords="200,0,400,200" href="#right" alt="Right half">
</map>

<p id="left">You can link the left half here…</p>
<p id="right">…and the right half here.</p>
  • usemap="#halves" on the image matches name="halves" on the map.
  • Each <area> has a shape, coords (pixel positions) and an href.
Shapecoords
rectleft, top, right, bottom
circlecentre x, centre y, radius
polyx1,y1, x2,y2, x3,y3 … (any outline)

Image maps don't resize with the image, so use them for fixed-size images such as a floor plan or a diagram.


2. Background Images

Any element can have an image behind it, set with CSS background-image.

Example

html
<div style="background-image: url('https://picsum.photos/id/1018/600/240'); background-size: cover; background-position: center; padding: 60px 20px; color: white;">
  <h2>Text on top of a photo</h2>
</div>
PropertyCommon valueEffect
background-repeatno-repeatShow the image once, instead of tiling it
background-sizecoverFill the whole element, cropping if needed
background-attachmentfixedKeep the image still while the page scrolls

Use <img> for pictures that are content (a product photo), and a background image for decoration (a banner pattern).


3. The <picture> Element

<picture> holds several <source> options and one <img>. The browser uses the first source that matches, and falls back to the <img>.

Example: drag the preview corner to make it narrow, then press Run again:

html
<picture>
  <source media="(min-width: 600px)" srcset="https://picsum.photos/id/1015/800/300">
  <source media="(min-width: 380px)" srcset="https://picsum.photos/id/1016/500/300">
  <img src="https://picsum.photos/id/1025/300/300" alt="A different photo for each screen size" style="max-width: 100%;">
</picture>

Common uses:

  • Art direction: a wide photo on desktops, a tighter crop on phones
  • New formats: offer image/webp first, with a JPG fallback
html
<picture>
  <source srcset="photo.webp" type="image/webp">
  <img src="photo.jpg" alt="Our shop">
</picture>

The <img> is required, and it's where the alt text goes.


Common Beginner Mistakes

1. Mismatched map names. usemap="#plan" needs <map name="plan">. Note the #.

2. Missing alt on <area>. Every clickable area needs alt, like a link's text.

3. Content as a background image. Screen readers can't see background images, so important pictures belong in <img>.

4. <picture> without <img>. Nothing shows if no source matches.


Practice Questions

  1. Which attribute connects an <img> to a <map>?
  2. Write an <area> for a circle centred at (50, 50) with radius 40 that links to about.html.
  3. Which CSS property puts an image behind an element?
  4. How do you stop a background image from tiling?
  5. In <picture>, which source does the browser use?
  6. Why must <picture> always contain an <img>?

Practice Answers

  1. usemap
  2. <area shape="circle" coords="50,50,40" href="about.html" alt="About us">
  3. background-image
  4. background-repeat: no-repeat;
  5. The first <source> whose media or type matches.
  6. It's the fallback when no source matches, and it carries the alt text.

Key Takeaways

  • <map> + <area> make parts of an image clickable.
  • background-image (with no-repeat and cover) decorates behind content.
  • <picture> + <source> choose an image per screen size or format.
  • Keep important pictures in <img> with good alt text.