MouseEvent.offsetX is a read-only number: how far the pointer is from the left padding edge of the element that received the event. Learn when to use it instead of clientX / pageX, how it pairs with offsetY, and five try-it labs.
01
Kind
Instance property
02
Returns
Number (CSS px)
03
Status
Baseline Widely available
04
Origin
Target padding edge
05
Pair with
offsetY
06
Best for
Element-local clicks
Fundamentals
Introduction
Drawing on a canvas, marking a spot inside a photo, or measuring a click inside a card all need “where inside this element?” That is offsetX (horizontal) and offsetY (vertical)—coordinates relative to the target’s padding box, not the window or the whole page.
JavaScript
box.addEventListener("click", (event) => {
console.log(event.offsetX); // pixels from the target's left padding edge
});
💡
Beginner tip
Imagine a ruler glued to the left padding edge of the clicked element. Zero is that edge—not the viewport, and not the document.
Concept
Understanding the Property
MDN: offsetX provides the offset in the X coordinate of the mouse pointer between that event and the padding edge of the target node.
Instance — read event.offsetX in a handler.
Number — CSS pixels; a floating-point double.
Target-relative — measured from event.target’s padding edge.
Read-only — set via constructor options on synthetic events only.
Foundation
📝 Syntax
JavaScript
const x = mouseEvent.offsetX;
Value
A double floating-point value in pixels (MDN). Early versions of the specification defined this as an integer; modern browsers report a floating-point number.
Because the listener is on the box, offsetX / offsetY are measured from that box’s padding edge—ideal for element-local tools.
Example 2 — Compare offsetX and clientX
Same click, two coordinate systems: element-local vs viewport.
JavaScript
box.addEventListener("click", (e) => {
console.log({
offsetX: e.offsetX, // relative to the box
clientX: e.clientX, // relative to the viewport
pageX: e.pageX // relative to the document
});
});
MouseEvent.offsetX is Baseline Widely available across modern browsers (MDN: since December 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline · Widely available
MouseEvent.offsetX
Reliable element-local X from the target padding edge—great for canvas, sliders, and hit boxes.
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerSupported in modern IE versions
Full support
offsetXExcellent
Bottom line: Read event.offsetX for target-local horizontal position; pair with offsetY for a full local point.
Wrap Up
Conclusion
event.offsetX gives you the horizontal mouse position relative to the target element’s padding edge. Use it for canvas clicks, local markers, and percentage-based controls; reach for clientX when you need viewport space instead.
Normalize with element width for responsive percentages
Remember the origin is the padding edge
❌ Don’t
Confuse offsetX with viewport clientX
Assume it equals pageX after scrolling
Ignore borders when mentally mapping the padding edge
Use viewport-fixed UI math with local coordinates
Mutate event.offsetX on live user events
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about offsetX
Element-local X from the target padding edge.
5
Core concepts
🖱️01
Instance
on the event
Kind
🔢02
Double
CSS pixels
Type
📐03
Padding edge
target origin
Origin
📈04
Local UX
canvas / sliders
Use
🛡️05
Baseline
widely available
Status
❓ Frequently Asked Questions
It is a read-only number: the horizontal offset of the mouse pointer from the padding edge of the event’s target element, measured in CSS pixels.
No. MDN marks MouseEvent.offsetX as Baseline Widely available (since December 2015). It is not Deprecated, Experimental, or Non-standard, so no status warning banner is needed.
offsetX is measured from the left padding edge of the target node—inside the border, at the start of the padding box. Borders are outside that origin; padding and content are inside.
clientX is relative to the browser viewport. offsetX is relative to the element that received the event (event.target). Moving the same element on the page changes clientX for the same spot on the element, but offsetX stays local to that element.
pageX is relative to the whole document (includes scroll). offsetX stays local to the target element’s padding box and does not grow just because the page scrolled.
A double (floating-point) number in pixels. Early versions of the spec defined it as an integer; modern browsers report a double.
Did you know?
MDN notes that early versions of the specification defined offsetX as an integer, while today it is a floating-point double. That is why you may see values like 40.5 on high-DPI displays instead of whole numbers only.