Image Maps
The HTML <map>
tag defines an image map. An image map is an image with clickable areas. The areas are defined with one or more <area>
tags.
Try to click on the computer, phone, or the cup of coffee in the image below:
<!DOCTYPE html>
<html>
<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>
<img src="workplace.jpg" alt="Workplace" usemap="#workmap" width="400" height="379">
<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>
How Does it Work?
The idea behind an image map is that you should be able to perform different actions depending on where in the image you click.
To create an image map you need an image, and some HTML code that describes the clickable areas.
The Image
The image is inserted using the <img>
tag. The only difference from other images is that you must add a usemap
attribute:
Syntax:
<img src="workplace.jpg" alt="Workplace" usemap="#workmap">
Create Image 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 required name
attribute:
<map name="workmap">
The name
attribute must have the same value as the <img>
's 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 clickable area, and you can choose one of these values:
rect
- defines a rectangular regioncircle
- defines a circular regionpoly
- defines a polygonal regiondefault
- defines the entire region
You must also define some coordinates to be able to place the clickable area onto the image.
Shape="rect"
The coordinates for shape="rect"
come in pairs, one for the x-axis and one for the y-axis.
So, the coordinates 34,44
is located 34 pixels from the left margin and 44 pixels from the top:
Shape="poly"
The shape="poly"
contains several coordinate points, which creates a shape formed with straight lines (a polygon).
This can be used to create any shape.
Like maybe a croissant shape!
<!DOCTYPE html>
<html>
<body>
<h2>Image Maps</h2>
<p>Click on the cup of coffee to execute a JavaScript function:</p>
<img src="workplace.jpg" alt="Workplace" usemap="#workmap" width="400" height="379">
<map name="workmap">
<area shape="circle" coords="337,300,44" href="coffee.htm" onclick="myFunction()">
</map>
<script>
function myFunction() {
alert("You clicked the data name!");
}
</script>
</body>
</html>
Chapter Summary
- Use the HTML
<map>
element to define an image map - Use the HTML
<area>
element to define the clickable areas in the image map - Use the HTML
usemap
attribute of the<img>
element to point to an image map
No comments
Welcome to saveracom.blogspot.com