HTML <map> map tag


The <map> tag in HTML is used to define an image map, which is a clickable image with specific areas that can be associated with different links or actions. It allows you to define multiple interactive regions (or "hotspots") on an image, each linking to different destinations or performing different actions.

How the <map> Element Works:

  • The <map> element contains a set of <area> elements, each defining a specific clickable area on the image.
  • The <img> element is linked to the <map> using the usemap attribute.

Example of an Image Map:

<img src="world-map.jpg" alt="World Map" usemap="#worldmap"> <map name="worldmap"> <area shape="rect" coords="34,44,270,350" href="https://en.wikipedia.org/wiki/North_America" alt="North America"> <area shape="circle" coords="477,300,50" href="https://en.wikipedia.org/wiki/South_America" alt="South America"> <area shape="poly" coords="600,400,700,500,650,600,550,500" href="https://en.wikipedia.org/wiki/Africa" alt="Africa"> </map>

Explanation:

  • <img>: The image (world-map.jpg) is made clickable with a usemap="#worldmap" attribute, which associates the image with the map named "worldmap".
  • <map>: Defines the clickable regions on the image.
  • name attribute: The name="worldmap" attribute gives an identifier to the map, which is referenced by the usemap attribute of the image.
  • <area>: Each <area> defines a clickable region on the image. The shape and position of the area are specified using the shape and coords attributes.
    • shape: Defines the shape of the clickable region (e.g., rect for rectangle, circle for circle, poly for polygon).
    • coords: Specifies the coordinates that outline the shape on the image.
    • href: Specifies the URL to link to when the area is clicked.
    • alt: Provides alternative text for accessibility.

Shape Types:

  • rect: A rectangular area, defined by two sets of coordinates: x1, y1, x2, y2, which represent the top-left and bottom-right corners of the rectangle.
  • circle: A circular area, defined by three values: the x and y coordinates of the circle's center and the radius of the circle.
  • poly: A polygonal area, defined by multiple coordinate pairs, specifying the points of the shape.

Example of Coordinates:

<area shape="rect" coords="34,44,270,350" href="link.html" alt="Rectangle Area">

In this example, the rectangle's top-left corner is at (34, 44), and the bottom-right corner is at (270, 350).

Key Points:

  • Interactive Maps: The <map> tag allows you to make an image interactive with multiple clickable regions.
  • Accessibility: Always use the alt attribute in the <area> tag to provide descriptive text for each clickable region for users with disabilities.
  • Fallback: If an area is not supported by the browser, it will fall back to a non-clickable image, so it’s important to ensure the image still makes sense on its own.

In summary, the <map> tag is a powerful way to create interactive images where different regions can link to different places or trigger different actions, making it useful for visual navigation or enhancing user interaction.