JavaScript MouseEvent offsetX Property

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Instance property

What You’ll Learn

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

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.

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.

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

⚖️ offsetX vs Other X Coordinates

PropertyMeasured fromTypical use
offsetXTarget padding edgeClicks inside a box, canvas, image
clientXViewport leftFixed UI, window-relative tips
pageXDocument leftAbsolute markers on a scrolled page
screenXScreen (monitor) leftRare OS/monitor-level needs
layerXLayer (non-standard)Legacy only—prefer offsetX

Pair offsetX with offsetY for a full point inside the target. Prefer offsetX over non-standard layerX when you need element-local coordinates.

⚡ Quick Reference

GoalCode
Read local Xevent.offsetX
Local point{ x: event.offsetX, y: event.offsetY }
Canvas / box clickbox.addEventListener("click", (e) => … e.offsetX)
Synthetic clicknew MouseEvent("click", { offsetX: 40, offsetY: 20 })
MDN statusBaseline Widely available (Dec 2015)

🔍 At a Glance

Four facts about offsetX.

Kind
instance

On the event

Type
double

CSS pixels

Origin
padding

Target left edge

Status
baseline

Widely available

Examples Gallery

Examples follow MDN MouseEvent.offsetX. Click inside boxes and compare local vs viewport coordinates in the try-it labs.

📚 Getting Started

Read offsetX from clicks on a target element.

Example 1 — Log offsetX / offsetY Inside a Box

Click anywhere in the box to see local coordinates from its padding edge.

JavaScript
const box = document.querySelector("#box");
const log = document.querySelector("#log");

box.addEventListener("click", (e) => {
  log.textContent = `offsetX: ${e.offsetX}, offsetY: ${e.offsetY}`;
});
Try It Yourself

How It Works

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
  });
});
Try It Yourself

How It Works

Move the box on the page and click the same corner again: offsetX stays similar, while clientX changes with the window position.

📈 Synthetic Events & Local Layout

Construct coordinates, map percentages, place markers.

Example 3 — Synthetic Event with offsetX

Useful in tests when you need a known point inside a target.

JavaScript
const event = new MouseEvent("click", {
  offsetX: 40,
  offsetY: 20,
  bubbles: true
});

console.log(event.offsetX); // 40
console.log(event.offsetY); // 20
Try It Yourself

How It Works

Pass numeric offsetX / offsetY to MouseEvent() when your tests assert element-local hit positions.

Example 4 — Click Position as a Percentage

Normalize local coordinates for responsive progress bars or image maps.

JavaScript
track.addEventListener("click", (e) => {
  const width = track.clientWidth || 1;
  const percent = Math.min(100, Math.max(0, (e.offsetX / width) * 100));
  console.log(`Clicked at ${percent.toFixed(1)}%`);
});
Try It Yourself

How It Works

Dividing offsetX by the element’s width turns a pixel click into a percentage that still works when the control resizes.

Example 5 — Place a Dot at the Click

Use local coordinates to position a marker inside a relatively positioned parent.

JavaScript
const area = document.querySelector("#area");
const dot = document.querySelector("#dot");

area.addEventListener("click", (e) => {
  // area { position: relative }, dot { position: absolute }
  dot.style.left = `${e.offsetX}px`;
  dot.style.top = `${e.offsetY}px`;
});
Try It Yourself

How It Works

With a relatively positioned parent, absolute left/top map cleanly to offsetX / offsetY (you may subtract half the marker size to center it).

🚀 Common Use Cases

  • Drawing or sampling pixels on a <canvas> or image.
  • Custom sliders and progress bars that use click position as a percentage.
  • Placing markers, stickers, or hotspots inside a card or map.
  • Unit tests that assert a click at a known local point.
  • Replacing non-standard layerX with a standard element-local X.

🔧 How It Works

1

Pointer hits an element

The browser picks event.target for this mouse event.

Target
2

Measure from the padding edge

Horizontal distance from the target’s left padding edge.

Layout
3

MouseEvent stores offsetX

Your listener reads the local X in CSS pixels.

Event
4

Use local coordinates

Draw, place markers, or compute percentages inside the element.

📝 Notes

  • Baseline Widely available (MDN, since December 2015).
  • Not Deprecated, Experimental, or Non-standard — no status banner required.
  • Origin is the target’s padding edge (inside the border).
  • Value type is a floating-point double in modern engines.
  • Related: mozInputSource, clientX, layerX, MouseEvent(), Window, JavaScript hub.

Universal Browser Support

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.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Supported in modern IE versions
Full support
offsetX Excellent

Bottom line: Read event.offsetX for target-local horizontal position; pair with offsetY for a full local point.

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.

Continue with offsetY, clientX, mozInputSource, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use offsetX for element-local hit positions
  • Pair with offsetY for a full local point
  • Prefer it over non-standard layerX for new code
  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about offsetX

Element-local X from the target padding edge.

5
Core concepts
🔢 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.

Next: MouseEvent.offsetY

Learn element-local Y coordinates from the target padding edge.

offsetY →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful