HTML - coords Attribute



The 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.

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).

Syntax

Following is the syntax for HTML 'coords’ attribute −

<area coords = "coordinates_values">

Following are the values and descriptions of the coords attribute −

Sr.No coords Attribute & Descriptions
1

X1, y1, x2, y2

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

X, y, radius

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

Example

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.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML 'coords' attribute</title>
</head>
<body>
   <p>Click on the HTML, CSS and JavaScript Logo to redirect on different page.</p>
   <img src="images (1).png" usemap="#mymap" width="700" height="231">
   <map name="mymap">
      <area shape="rect" coords="7, 38, 142, 281" href="html logo.html" alt="HTML">
      <area shape="rect" coords="280, 48, 416, 223" href="css logo.html" alt="CSS">
      <area shape="rect" coords="562, 50, 694, 222" href="js log.html" alt="JS">
   </map>
</body>
</html>

html logo.html

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML</title>
</head>
<body>
   <img src="html logo.jpg" alt="HTML logo">
</body>
</html>

css logo.html

<!DOCTYPE html>
<html lang="en">
<head>
   <title>CSS</title>
</head>
<body>
   <img src="css logo.png" alt="Css Logo">
</body>
</html>

Js logo.html

<!DOCTYPE html>
<html lang="en">
<head>
   <title>Js</title>
</head>
<body>
   <img src="js logo.png" alt="javaScript logo">
</body>
</html>

On running the above code, it will generate an output consisting of the images displayed on the webpage. when the user clicks on the individuals images it further open to open single image.

html_attributes_reference.htm
Advertisements