HTML

How to get coordinates of an image in HTML

Ravindra Kumar

To get the coordinates of a click within an image in HTML using JavaScript, you can use the click event and retrieve the coordinates using event.clientX, event.clientY, event.offsetX, and event.offsetY.

Here’s an example:

Example Code:


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Get Image Coordinates</title>
</head>
<body>

<h2>Click on the image to get coordinates</h2>
<img id="myImage" src="your-image.jpg" alt="Sample Image" width="500" style="cursor: crosshair;">

<p id="coordinates">Click on the image to see the coordinates.</p>

<script>
document.getElementById("myImage").addEventListener("click", function(event) {
let x = event.offsetX; // X coordinate relative to the image
let y = event.offsetY; // Y coordinate relative to the image

document.getElementById("coordinates").innerText = `X: ${x}, Y: ${y}`;
});
</script>

</body>
</html>

Explanation:

  • event.offsetX and event.offsetY give the coordinates relative to the image.

  • event.clientX and event.clientY give the coordinates relative to the browser window.

  • The image has an id="myImage", and we attach a click event listener to it.

  • When the image is clicked, the coordinates are displayed in the <p> element.

Let me know if you need further customizations!

Ravindra Kumar

Ravindra is a passionate full stack developer and dedicated blogger with a flair for crafting user-friendly web applications and insightful articles. With expertise spanning front-end and back-end technologies, Ravindra brings ideas to life through innovative coding solutions.

Suggested Reading

How to Create Image Maps in HTML ?

Creating image link in HTML, Learn how to give multiple links in single image in HTML Suppose, You want to give a clickable link to specific part of a image that can be possible by <map> tag of HTML.You can also target multiple clickable links to a particular are of the image.so here you need […]

What is the difference between _top and _parent target attribute of <a> Tag in HTML

What is the difference between _top and _parent target attribute of <a> Tag in HTML Answer _top : If you are using nested frame then it goes to the first / top windows _parent : If you are using nested frame then it goes to the parent / above windows on which you clicked the […]

How to Design Stylish Forms in HTML

In this post, i am going to explain that how can we design a stylish HTML form for our website and this form will be good looking.I am going to use HTML,CSS, Jquery and bootstrape in this example. First, You need to have a index.html file to design the input fields and i am going […]

How to designing responsive Navigation bar using HTML and CSS

Every website needs Navigation bar to navigate on the website properly and this is very usefull for users as well.If a website does not have good navigation interface, users prefer to leave this type of website immediately. index.html <!DOCTYPE html> <html lang="en"> <head> <title>Menu Tutorial</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="style.css"/> <link […]

What is initial-scale=1.0

The initial-scale=1.0 is a setting used within the HTML <meta> viewport tag. It controls the initial zoom level of a webpage when it’s first loaded on a mobile device or a responsive screen. <meta name=”viewport” content=”width=device-width, initial-scale=1.0″> Meaning of initial-scale=1.0: initial-scale=1.0 sets the zoom level to 100% when the page first loads. A value of […]

How to make sticky header using HTML and CSS

Creating a sticky header using HTML and CSS is straightforward. A sticky header remains visible at the top of the viewport as the user scrolls down the page. You can achieve this using the position: sticky property in CSS. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Sticky Header</title> <link rel="stylesheet" href="styles.css"> […]