<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/get-the-mouse-click-position-in-javascript-5oi8i3cwv" -->

---
title: Get the Mouse Click Position in JavaScript | daily.dev
description: To get a click's position inside an element, subtract the element's getBoundingClientRect() left and top from the event's clientX and clientY. Both values are...
canonical: https://daily.dev/posts/get-the-mouse-click-position-in-javascript-5oi8i3cwv
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Get the Mouse Click Position in JavaScript | daily.dev
og:description: To get a click's position inside an element, subtract the element's getBoundingClientRect() left and top from the event's clientX and clientY. Both values are...
og:url: https://daily.dev/posts/get-the-mouse-click-position-in-javascript-5oi8i3cwv
og:image: https://api.daily.dev/og/posts/5oi8I3CWV.png
og:image:alt: Get the Mouse Click Position in JavaScript
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Get the Mouse Click Position in JavaScript

**[Kirupa](https://daily.dev/sources/kirupa)** · 14 min read · 1 upvotes · 0 comments

## Summary

To get a click's position inside an element, subtract the element's getBoundingClientRect() left and top from the event's clientX and clientY. Both values are measured from the viewport, so the result stays correct after the page scrolls. offsetX and offsetY are measured from the innermost element under the pointer, so clicking a child element changes what they mean. Mixing pageX with getBoundingClientRect breaks once the page scrolls. Pointer events give the same clientX and clientY for mouse, touch and pen. Scaled elements need a divide by the scale, and canvases need a multiply by the width ratio.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.kirupa.com/html5/getting_mouse_click_position.htm>

## Questions this post answers

### How do I get the click position inside an element in JavaScript, even when the page is scrolled?

Subtract the element's getBoundingClientRect() left and top from the event's clientX and clientY. Both are measured from the viewport, so they change together as the page scrolls and the difference stays correct. Use e.currentTarget for the element the listener is attached to. Don't subtract rect.left from pageX, because that breaks after scrolling.

_Developers handling mouse and touch input keep their JavaScript event knowledge current on daily.dev._

### Why are offsetX and offsetY wrong when I click a child element inside my container?

offsetX and offsetY are measured from the event's target, the innermost element under the pointer, not the element holding the listener. Clicking a child makes the numbers relative to that child. They also start at the padding edge, so a 6 pixel border shifts results by 6. Use getBoundingClientRect on currentTarget, or set pointer-events: none on children.

_Anyone debugging DOM event quirks can follow practical JavaScript fixes like this on daily.dev._

### How do I get the correct click position on a canvas or a CSS-scaled element?

getBoundingClientRect returns the on-screen size, transforms included. For a scale(0.75) element, divide the click position by rect.width / element.offsetWidth. For a canvas whose width and height attributes differ from its displayed size, multiply by canvas.width / rect.width horizontally and canvas.height / rect.height vertically.

_Developers building canvas and interactive UI work stay sharp on browser details through daily.dev._

## Similar posts on daily.dev

- [Make a Circle Follow the Mouse Cursor in JavaScript](https://daily.dev/posts/make-a-circle-follow-the-mouse-cursor-in-javascript-gsdugfcgn) · Kirupa · 0 upvotes · 0 comments
- [we finally learned to center a div, then browsers added sidebars](https://daily.dev/posts/we-finally-learned-to-center-a-div-then-browsers-added-sidebars-ehrewltuh) · Hacker News · 8 upvotes · 5 comments
- [Medium](https://daily.dev/posts/medium-b7ne44sxm) · Medium · 0 upvotes · 0 comments
- [translateX\(\)](https://daily.dev/posts/translatex--2gqlsumly) · CSS-Tricks · 0 upvotes · 0 comments
- [Getting Started with Anchor Positioning • Josh W. Comeau](https://daily.dev/posts/getting-started-with-anchor-positioning-josh-w-comeau-gk8wgqtjg) · Josh W Comeau · 83 upvotes · 3 comments

---

Tags: [#javascript](https://daily.dev/tags/javascript)

[View this post on daily.dev](https://daily.dev/posts/get-the-mouse-click-position-in-javascript-5oi8i3cwv)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Get the Mouse Click Position in JavaScript","url":"https://daily.dev/posts/get-the-mouse-click-position-in-javascript-5oi8i3cwv","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/get-the-mouse-click-position-in-javascript-5oi8i3cwv"},"datePublished":"2026-11-28T06:30:38.131Z","dateModified":"2026-11-28T06:31:10.463Z","description":"To get a click's position inside an element, subtract the element's getBoundingClientRect() left and top from the event's clientX and clientY. Both values are...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/0200303acaaf36a0a5955bd069f8010c?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/0200303acaaf36a0a5955bd069f8010c?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Kirupa","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Kirupa","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/ff2fdb4927d34b81bd07cd18d6049a98","url":"https://daily.dev/sources/kirupa"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/get-the-mouse-click-position-in-javascript-5oi8i3cwv","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"javascript","timeRequired":"PT14M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Kirupa","item":"https://daily.dev/sources/kirupa"},{"@type":"ListItem","position":3,"name":"Get the Mouse Click Position in JavaScript"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/get-the-mouse-click-position-in-javascript-5oi8i3cwv#faq","mainEntity":[{"@type":"Question","name":"How do I get the click position inside an element in JavaScript, even when the page is scrolled?","acceptedAnswer":{"@type":"Answer","text":"Subtract the element's getBoundingClientRect() left and top from the event's clientX and clientY. Both are measured from the viewport, so they change together as the page scrolls and the difference stays correct. Use e.currentTarget for the element the listener is attached to. Don't subtract rect.left from pageX, because that breaks after scrolling. Developers handling mouse and touch input keep their JavaScript event knowledge current on daily.dev."}},{"@type":"Question","name":"Why are offsetX and offsetY wrong when I click a child element inside my container?","acceptedAnswer":{"@type":"Answer","text":"offsetX and offsetY are measured from the event's target, the innermost element under the pointer, not the element holding the listener. Clicking a child makes the numbers relative to that child. They also start at the padding edge, so a 6 pixel border shifts results by 6. Use getBoundingClientRect on currentTarget, or set pointer-events: none on children. Anyone debugging DOM event quirks can follow practical JavaScript fixes like this on daily.dev."}},{"@type":"Question","name":"How do I get the correct click position on a canvas or a CSS-scaled element?","acceptedAnswer":{"@type":"Answer","text":"getBoundingClientRect returns the on-screen size, transforms included. For a scale(0.75) element, divide the click position by rect.width / element.offsetWidth. For a canvas whose width and height attributes differ from its displayed size, multiply by canvas.width / rect.width horizontally and canvas.height / rect.height vertically. Developers building canvas and interactive UI work stay sharp on browser details through daily.dev."}}]}
```

