HTML - HTML Image Maps Using the <map> and <area> Elements
1. Introduction to HTML Image Maps
An HTML image map is a feature that allows developers to create multiple clickable areas within a single image. Instead of making the entire image a single clickable link, an image map divides the image into different regions, and each region can perform a separate action when clicked.
For example, consider an image of a world map. A developer can make different countries clickable so that when a user clicks on India, the website displays information about India. Similarly, clicking on France or Japan can open separate webpages containing information about those countries.
HTML image maps are useful when a website needs to provide interactive visual navigation without using multiple separate images.
HTML provides two important elements for creating image maps:
-
<map>: Defines the image map and contains the clickable areas. -
<area>: Defines individual clickable regions within the image.
2. How HTML Image Maps Work
An image map works by connecting an image with a map definition. The image is displayed using the <img> element, while the usemap attribute connects it to a specific <map> element.
The <map> element contains one or more <area> elements. Each <area> element defines a clickable region using coordinates and a shape.
When a user clicks inside one of these defined regions, the browser performs the action specified by the href attribute.
The basic process involves three steps:
-
Insert an image into the webpage using the
<img>element. -
Connect the image to a map using the
usemapattribute. -
Define clickable regions inside the
<map>element using<area>elements.
This approach allows a single image to contain multiple independent links.
3. Syntax of the HTML <map> Element
The <map> element is used to create an image map. It acts as a container for all the clickable areas associated with an image.
Syntax:
<img src="world-map.jpg" alt="World Map" usemap="#worldmap">
<map name="worldmap">
<area shape="rect" coords="100,50,200,150"
href="india.html" alt="India">
</map>
In this example:
-
<img>displays the world map image. -
usemap="#worldmap"connects the image to the map namedworldmap. -
<map name="worldmap">defines the image map. -
<area>creates a clickable rectangular region. -
href="india.html"specifies the webpage that opens when the region is clicked. -
alt="India"provides alternative text describing the clickable area.
The value of the usemap attribute must match the name of the corresponding <map> element, including the # symbol in usemap.
4. Different Shapes Used in HTML Image Maps
The <area> element supports different shapes to define clickable regions within an image. Developers can select a shape depending on the visual region they want to make interactive.
There are three commonly used shapes.
A. Rectangle Shape
The rect shape creates a rectangular clickable region within an image.
It requires four coordinate values:
<area shape="rect" coords="50,50,200,150"
href="about.html" alt="About Us">
The coordinates represent:
-
First value: X-coordinate of the top-left corner.
-
Second value: Y-coordinate of the top-left corner.
-
Third value: X-coordinate of the bottom-right corner.
-
Fourth value: Y-coordinate of the bottom-right corner.
This shape is useful for selecting rectangular objects, buildings, photographs, and other clearly defined regions.
B. Circle Shape
The circle shape creates a circular clickable region.
It requires three coordinate values:
<area shape="circle" coords="150,100,50"
href="profile.html" alt="Profile">
The coordinates represent:
-
First value: X-coordinate of the circle's centre.
-
Second value: Y-coordinate of the circle's centre.
-
Third value: Radius of the circle.
This shape is useful when creating clickable regions around circular objects, such as buttons, logos, or specific locations in an illustration.
C. Polygon Shape
The poly shape creates an irregular clickable region using multiple coordinate pairs.
<area shape="poly"
coords="100,50,200,80,180,160,90,140"
href="region.html" alt="Selected Region">
Each pair represents an X-coordinate and a Y-coordinate of a point. The browser connects these points to create the clickable region.
This shape is particularly useful for geographical maps, irregular objects, diagrams, and illustrations.
5. Understanding Coordinates in HTML Image Maps
Coordinates are important because they determine the exact location and size of clickable regions within an image.
HTML image map coordinates are generally measured in pixels, starting from the top-left corner of the original image.
The coordinate system works as follows:
-
The top-left corner represents
(0, 0). -
The X-coordinate increases when moving towards the right.
-
The Y-coordinate increases when moving downwards.
For example, if an image has a width of 500 pixels and a height of 300 pixels, its coordinate range starts at (0, 0) and extends to (500, 300).
Developers must carefully calculate the coordinates to ensure that clickable regions match the intended objects in the image.
Incorrect coordinates can cause clickable areas to appear in the wrong locations or overlap with other regions.
6. Complete Example of an HTML Image Map
The following example demonstrates how to create an image map with multiple clickable regions.
<!DOCTYPE html>
<html>
<head>
<title>HTML Image Map Example</title>
</head>
<body>
<h2>Interactive Image Map</h2>
<img src="world-map.jpg"
alt="World Map"
usemap="#worldmap"
"
height="300">
<map name="worldmap">
<area shape="rect"
coords="100,100,180,180"
href="india.html"
alt="India">
<area shape="circle"
coords="300,120,40"
href="japan.html"
alt="Japan">
<area shape="poly"
coords="200,50,260,70,250,140,190,120"
href="france.html"
alt="France">
</map>
</body>
</html>
In this example, a world map image contains three clickable regions. Each region uses a different shape and directs users to a different webpage.
When users click the rectangular region, they are directed to the India webpage. Clicking the circular region opens the Japan webpage, while clicking the polygon region opens the France webpage.
The actual clickable locations must be adjusted to match the geographical positions in the selected image.
7. Important Attributes Used in Image Maps
Several HTML attributes help developers create and manage image maps effectively.
|
Attribute |
Description |
|---|---|
|
|
Connects an image to a specific image map. |
|
|
Defines the name of the image map. |
|
|
Specifies the shape of a clickable region. |
|
|
Defines the coordinates of the clickable region. |
|
|
Specifies the destination URL. |
|
|
Provides alternative text describing the clickable area. |
|
|
Specifies where the linked webpage should open. |
|
|
Indicates that the linked resource should be downloaded. |
These attributes provide flexibility when designing interactive images and navigation systems.
8. Advantages of HTML Image Maps
HTML image maps offer several advantages in website development.
1. Multiple Links in One Image: Developers can create multiple clickable regions within a single image instead of using separate images for each link.
2. Improved Visual Navigation: Image maps allow users to interact with visual elements such as geographical maps, diagrams, and illustrations.
3. Simple Implementation: HTML provides built-in elements for creating image maps without requiring additional JavaScript libraries.
4. Reduced Image Management: A single image can contain several interactive links, making it easier to manage certain visual navigation layouts.
5. Flexible Clickable Regions: Developers can define rectangular, circular, and irregular clickable areas according to their requirements.
9. Limitations of HTML Image Maps
Although image maps are useful, they also have some limitations.
1. Coordinate Management: Developers must calculate the coordinates accurately. Changes to the image dimensions may require adjustments to the clickable regions.
2. Responsive Design Challenges: Image maps can become difficult to maintain when images are resized on different screen sizes because the original coordinates may no longer match the displayed image.
3. Accessibility Considerations: Developers must provide meaningful alternative text and ensure that clickable regions are accessible to keyboard and assistive technology users.
4. Limited Visual Feedback: HTML image maps do not automatically provide advanced hover effects, animations, or interactive visual feedback.
5. Complex Maintenance: When an image contains many clickable regions, managing and updating their coordinates can become difficult.
10. Practical Applications of HTML Image Maps
HTML image maps can be used in several real-world applications.
-
Geographical Websites: Interactive maps can help users select countries, states, or cities to access location-specific information.
-
Educational Websites: Students can click on different parts of a human body diagram, historical map, or scientific illustration to learn more.
-
Product Websites: Different parts of a product image can link to detailed descriptions or related product pages.
-
Tourism Websites: Interactive destination maps can provide information about tourist attractions, hotels, and important landmarks.
-
Building Layouts: Office or campus maps can allow visitors to select rooms, departments, or facilities.
Conclusion
HTML image maps provide a simple way to create interactive images by defining multiple clickable regions within a single picture. The <map> element acts as a container for the image map, while the <area> element defines the individual clickable sections using shapes and coordinates.
By understanding rectangular, circular, and polygonal regions, developers can create useful visual navigation systems for educational websites, geographical applications, tourism platforms, and product catalogues. Although modern CSS and JavaScript techniques offer more advanced interactive possibilities, HTML image maps remain a useful built-in feature for creating basic clickable image regions.