HTML - coords Attribute



HTML coords attribute is used to set the coordinates of an area in an image map.

It is used together with the shape attribute to specify the size, shape, and placement of an area. The coordinates of the top-left corner of an area are (0,0). The coords attribute is not used within the <area> element if the shape is set to default.

Syntax

<area coords = "coordinates_values">

Following are the values and descriptions of the coords attribute

coords value Descriptions

x1, y1, x2, y2

Specifies the coordinates of the top-left and bottom-right corners of the rectangle (shape = "rect").

x, y, radius

Specifies the coordinates of the circle and the radius (shape = "circle").

Applies On

Below listed elements allow using of the HTML coords attribute

Element Description
<area> HTML <area> tag specifies the areas of the image, mapping that can be clicked on or are active areas that are linked to by hyperlinks.

Examples of HTML coords attribute

Bellow examples will illustrate the HTML coords attribute, where and how we should use this attribute!

Define clickable area in an image

In the following example, we are using the HTML ‘coords’ attribute within the image <area> element to set coordinates of an area in an image. In the output screen when you click yellow area you will redirect to HTML page.

<!DOCTYPE html>
<html>

<head>
    <title>HTML coords attribute</title>
</head>

<body>
    <img src="html/images/rect.jpg" alt="Rectangle" 
         border="0" usemap="#tutorials" />
    <map name="tutorials">
      <area shape="rect" coords="31,101,164,157" 
            alt="HTML Tutorial" href="/html/index.htm" 
            target="_blank">   
    </map>
</body>

</html>

Define multiple clickable area in single image

Here we define three different area in single image when clicked will be redirected accordingly

<!DOCTYPE html>
<html>

<head>
    <title>HTML area Tag</title>
</head>

<body>
    <img src="/images/usemap.gif" alt="usemap" border="0" 
         usemap="#tutorials" />
    <map name="tutorials">
   <area shape = "poly"  coords = "74,0,113,29,98,72,52,72,38,27"
         href = "/perl/index.htm"  alt = "Perl Tutorial"  
         target = "_blank">
      
   <area shape = "rect"  coords = "22,83,126,125"  alt = "HTML Tutorial" 
         href = "/html/index.htm"  target = "_blank">
      
   <area shape = "circle"  coords = "73,168,32"  alt = "PHP Tutorial" 
         href = "/php/index.htm"  target = "_blank">
   </map>
</body>

</html>

How to get coordinates of an image

Following are steps to get coordinates of an image

  • Open any image on the Paint app.
  • Click on resize and change resize unit from percentage to pixel.
  • Give the same pixel width and height to your image in HTML.
  • Highlight the top-left corner of that part of the image, and you will see the coordinates in your paint application's bottom left corner in px(x1, y1).
  • Highlight the bottom-right corner of that part of the image, and you will see the coordinates in your paint application's bottom left corner in px(x2, y2).

Supported Browsers

Attribute Chrome Edge Firefox Safari Opera
coords Yes Yes Yes Yes Yes
html_attributes_reference.htm
Advertisements