HTML Image Maps Image Maps
HTML Image Maps Image Maps
Image Maps
The <map> tag defines an image-map. An image-map is an image with clickable areas.
<!DOCTYPE html>
<html>
<head>
<title>Image Map</title
</head>
<body>
<h2>Image Maps</h2>
<p>Click on the computer, the phone, or the cup of coffee to go to a new page and read more about the topic:</p>
<map name="workmap">
<area shape="rect" coords="34,44,270,350" alt="Computer" href="computer.htm">
<area shape="rect" coords="290,172,333,250" alt="Phone" href="phone.htm">
<area shape="circle" coords="337,300,44" alt="Cup of coffee" href="coffee.htm">
</map>
</body>
</html>
The Image
The image is inserted using the <img> tag. The only difference from other images is that you must add a usemap
attribute:
<img src="workplace.jpg" alt="Workplace" usemap="#workmap">
The usemap value starts with a hash tag # followed by the name of the image map, and is used to create a relationship
between the image and the image map.
The Map
Then add a <map> element.
The <map> element is used to create an image map, and is linked to the image by using the name attribute:
<map name="workmap">
The name attribute must have the same value as the usemap attribute.
The Areas
Then add the clickable areas.
A clickable area is defined using an <area> element.
Shape
You must define the shape of the area, and you can choose one of these values:
rect - defines a rectangular region
circle - defines a circular region
poly - defines a polygonal region
default - defines the entire region
Coordinates
You must define some coordinates to be able to place the clickable area onto the image.
The coordinates come in pairs, one for the x-axis and one for the y-axis.
Rectangle
The coordinates 34, 44 is located 34 pixels from the left margin and 44 pixels from the top:
The coordinates 270, 350 is located 270 pixels from the left margin and 350 pixels from the top:
<area shape="rect" coords="34, 44, 270, 350" href="computer.htm">
Circle
To add a circle area, first locate the coordinates of the center of the circle:
337, 300
Then specify the radius of the circle:
44 pixels
<area shape="circle" coords="337, 300, 44" href="coffee.htm">
The <picture> element contains a number of <source> elements, each referring to different image sources. This way the
browser can choose the image that best fits the current view and/or device.
Each <source> element have attributes describing when their image is the most suitable.
<!DOCTYPE html>
<html>
<head>
<title>Picture Element</title>
</head>
<body>
<picture>
<source media="(min-width: 650px)" srcset="img_food.jpg">
<source media="(min-width: 465px)" srcset="img_car.jpg">
<img src="img_girl.jpg" style="width:auto;">
</picture>
<p>Resize the browser to see different versions of the picture loading at different viewport sizes.
The browser looks for the first source element where the media query matches the user's current viewport width,
and fetches the image specified in the srcset attribute.</p>
<p>The img element is required as the last child tag of the picture declaration block.
The img element is used to provide backward compatibility for browsers that do not support the picture element, or if
none of the source tags matched.
</p>
<p><strong>Note:</strong> The picture element is not supported in IE12 and earlier or Safari 9.0 and earlier.</p>
</body>
</html>