HTML - usemap Attribute



The usemap is an HTML attribute that creates relationships between <img> and <map> by specifying an image or an object with clickable areas.

The usemap attribute is associated with a <map> element’s name or id attribute and creates a relationship between the <img> and the <map>.

Following are the elements where the value attribute can be used −

  • <img>
  • <object>

Syntax

Following is the syntax of the usemap attribute −

<img usemap-"#mapname">

The "mapname" is used to hold the map name containing the hash (#) character.

Example

In the following example, we are creating an image map with clickable areas by using the usemap attribute, as follows−

<!DOCTYPE html>
<html>
<body>
   <h1>The map and area elements</h1>
   <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="https://cdn.pixabay.com/photo/2017/11/27/21/31/computer-2982270_640.jpg" alt="Workplace" usemap="#workmap" width="400" height="379">
   <map name="workmap">
      <area shape="rect" coords="100,50,200,250" alt="Computer" href="https://images.unsplash.com/photo-1499951360447-b19be8fe80f5?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1170&q=80">
      <area shape="rect" coords="60,250,150,120" alt="Phone" href="https://images.unsplash.com/photo-1511707171634-5f897ff02aa9?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8Mnx8bW9iaWxlJTIwcGhvbmV8ZW58MHx8MHx8fDA%3D&w=1000&q=80">
      <area shape="circle" coords="40,50,120" alt="Cup of coffee" href="https://img.freepik.com/premium-photo/coffee-cup-old-wooden-table-with-cream_158502-63.jpg">
   </map>
</body>
</html>

On running the above code, the output window will pop up displaying the image on the webpage. when the user clicks on the respective areas of the image it further displays another image.

Example

Considering the another scenario, where we are going to use the usemap attribute with the object element.

<!DOCTYPE html>
<html>
<body>
   <h1>The object usemap attribute</h1>
   <object data="https://img.freepik.com/free-vector/big-animals-set_1284-10911.jpg?w=2000" width="250" height="200" usemap="#animalmap"></object>
   <map name="animalmap">
      <area shape="rect" coords="30,10,100,100" alt="elephant" href="https://media.istockphoto.com/id/479667835/photo/background-elephant.jpg?s=612x612&w=0&k=20&c=DwfinIerTq104FXWcBBwegTWcmw8tf18EGqa5IpEyFk=">
   </map>
   <p>
      <b>Note:</b> The usemap attribute of the object element is not supported in Chrome, Edge, Safari, and Opera.
   </p>
</body>
</html>

When we run the above code, it will generate an output consisting of the image along with a text displayed on the webpage.

html_attributes_reference.htm
Advertisements