HTML - Image Map

-

Creating an Image Map

Preparing the Image

When creating an image map, the first step is to choose an image that complements your web page's content and design. The image should be clear, appealing, and relevant to the clickable areas you plan to define. Once you have selected an image, make sure it is web-friendly by optimizing its size and format. Large images can slow down your web page's loading time, so it's important to find a balance between image quality and file size. Common web-friendly image formats include JPEG, PNG, and GIF.

After preparing the image, you need to determine the clickable areas. These are the specific regions of the image that users can interact with by clicking. Consider the layout and content of your image and decide which areas should be clickable. You can use image editing software or online tools to determine the coordinates of these areas, which will be necessary when defining the image map.

Using the <map> Element

To create an image map, you need to use the <map> element in your HTML code. The <map> element is a container for the clickable areas of your image. It requires a unique name attribute that identifies the image map.

Example: Basic Syntax of the <map> Element

<map name="myImageMap">
  <!-- clickable areas will be defined here -->
</map>

Replace "myImageMap" with a descriptive name for your specific image map.

To link the image map to your image, you need to use the usemap attribute on the <img> element. The value of the usemap attribute should be the name of your image map preceded by a hash symbol (#).

<img src="image.jpg" alt="My Image" usemap="#myImageMap">

Make sure the name in the usemap attribute matches the name you defined in the <map> element.

Defining Clickable Areas with <area>

Within the <map> element, you can define the clickable areas using the <area> element. Each <area> element represents a specific region of the image that users can interact with. The <area> element has several attributes that define the shape, coordinates, and hyperlink of the clickable area.

The shape attribute specifies the shape of the clickable area. It can have values such as "rect" for a rectangular area, "circle" for a circular area, or "poly" for a polygonal area.

The coordinates of the clickable area are specified using the coords attribute. The format of the coordinates depends on the shape you choose. For rectangular areas, you need to provide the coordinates of the top-left and bottom-right corners (x1, y1, x2, y2). For circular areas, you need to provide the center coordinates and the radius (center-x, center-y, radius). For polygonal areas, you need to provide a series of coordinates for each point of the polygon (x1, y1, x2, y2, ..., xn, yn).

To add a hyperlink to the clickable area, you use the href attribute. The value of the href attribute should be the URL or the anchor link you want to navigate to when the area is clicked.

Example: Defining Clickable Areas Using the <area> Element

<map name="myImageMap">
  <area shape="rect" coords="0,0,100,100" href="page1.html" alt="Area 1">
  <area shape="circle" coords="200,150,50" href="page2.html" alt="Area 2">
  <area shape="poly" coords="300,50,350,100,300,150" href="page3.html" alt="Area 3">
</map>

In this example, three clickable areas are defined: a rectangular area, a circular area, and a polygonal area. Each area has its own coordinates and hyperlink.

By combining the <map> element, the usemap attribute on the <img> element, and the <area> elements, you can create an interactive image map that allows users to click on different regions of the image and navigate to different pages or perform specific actions.

Image Map Shapes

When defining clickable areas in an image map, you can use different shapes to create precise and interactive regions. The three main shapes available are rectangles, circles, and polygons. Each shape has its own syntax and attributes for specifying the coordinates of the clickable area.

Rectangle (shape="rect")

A rectangular clickable area is defined using the shape="rect" attribute. To specify the coordinates of a rectangular area, you need to provide the values for coords in the following order: coords="x1,y1,x2,y2".

Parameter Description
x1 The x-coordinate of the top-left corner of the rectangle.
y1 The y-coordinate of the top-left corner of the rectangle.
x2 The x-coordinate of the bottom-right corner of the rectangle.
y2 The y-coordinate of the bottom-right corner of the rectangle.

Example: Defining a rectangular clickable area

<map name="myImageMap">
  <area shape="rect" coords="50,50,200,150" href="rectangle.html" alt="Rectangular Area">
</map>

In this example, a rectangular area is defined with the top-left corner at coordinates (50, 50) and the bottom-right corner at coordinates (200, 150). When clicked, it will navigate to the rectangle.html page.

Circle (shape="circle")

A circular clickable area is defined using the shape="circle" attribute. To specify the coordinates of a circular area, you need to provide the values for coords in the following order: coords="center-x,center-y,radius".

Parameter Description
center-x The x-coordinate of the center of the circle.
center-y The y-coordinate of the center of the circle.
radius The radius of the circle.

Example: Defining a circular clickable area

<map name="myImageMap">
  <area shape="circle" coords="300,200,50" href="circle.html" alt="Circular Area">
</map>

In this example, a circular area is defined with its center at coordinates (300, 200) and a radius of 50 pixels. When clicked, it will navigate to the circle.html page.

Polygon (shape="poly")

A polygonal clickable area is defined using the shape="poly" attribute. To specify the coordinates of a polygonal area, you need to provide a series of x and y coordinates for each point of the polygon in the coords attribute: coords="x1,y1,x2,y2,...,xn,yn".

Parameter Description
x1,y1 The coordinates of the first point of the polygon.
x2,y2 The coordinates of the second point of the polygon.
... ...
xn,yn The coordinates of the last point of the polygon.

Example: Defining a polygonal clickable area

<map name="myImageMap">
  <area shape="poly" coords="400,100,500,150,450,300,350,250" href="polygon.html" alt="Polygonal Area">
</map>

In this example, a polygonal area is defined with four points: (400,100), (500,150), (450,300), and (350,250). When clicked, it will navigate to the polygon.html page.

By using these different shapes - rectangles, circles, and polygons - you can create precisely defined clickable areas on your image map. This allows you to provide a more interactive and engaging experience for your users, enabling them to click on specific regions of the image to access additional content or perform actions.