On this page8 sections
Lecture 012: Image Maps, Background Images & the Picture Element
| Category | 3: Links, Images, Tables & Lists |
| Lecture | 012 of 048 |
| Estimated video length | 12 min |
| Target | HTML5 · any modern browser |
| Level | Beginner |
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:
<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 matchesname="halves"on the map.- Each
<area>has ashape,coords(pixel positions) and anhref.
| Shape | coords |
|---|---|
rect | left, top, right, bottom |
circle | centre x, centre y, radius |
poly | x1,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
<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>| Property | Common value | Effect |
|---|---|---|
background-repeat | no-repeat | Show the image once, instead of tiling it |
background-size | cover | Fill the whole element, cropping if needed |
background-attachment | fixed | Keep 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:
<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/webpfirst, with a JPG fallback
<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
- Which attribute connects an
<img>to a<map>? - Write an
<area>for a circle centred at (50, 50) with radius 40 that links toabout.html. - Which CSS property puts an image behind an element?
- How do you stop a background image from tiling?
- In
<picture>, which source does the browser use? - Why must
<picture>always contain an<img>?
Practice Answers
usemap<area shape="circle" coords="50,50,40" href="about.html" alt="About us">background-imagebackground-repeat: no-repeat;- The first
<source>whosemediaortypematches. - It's the fallback when no source matches, and it carries the
alttext.
Key Takeaways
<map>+<area>make parts of an image clickable.background-image(withno-repeatandcover) decorates behind content.<picture>+<source>choose an image per screen size or format.- Keep important pictures in
<img>with goodalttext.