JavaScript MouseEvent offsetY Property

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

What You’ll Learn

MouseEvent.offsetY is a read-only number: how far the pointer is from the top padding edge of the element that received the event. Learn when to use it instead of clientY / pageY, how it pairs with offsetX, 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

offsetX

06

Best for

Element-local clicks

Introduction

Sampling a row on a canvas, placing a pin on a photo, or measuring a click inside a card needs “how far down inside this element?” That is offsetY (vertical), paired with offsetX (horizontal)—coordinates relative to the target’s padding box.

JavaScript
box.addEventListener("click", (event) => {
  console.log(event.offsetY); // pixels from the target's top padding edge
});
💡
Beginner tip

Imagine a ruler glued to the top padding edge of the clicked element. Zero is that edge—not the viewport top, and not the document top.

Understanding the Property

MDN: offsetY provides the offset in the Y coordinate of the mouse pointer between that event and the padding edge of the target node.

  • Instance — read event.offsetY in a handler.
  • Number — CSS pixels; a floating-point double.
  • Target-relative — measured from event.target’s top padding edge.
  • Read-only — set via constructor options on synthetic events only.

📝 Syntax

JavaScript
const y = mouseEvent.offsetY;

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.

⚖️ offsetY vs Other Y Coordinates

PropertyMeasured fromTypical use
offsetYTarget top padding edgeClicks inside a box, canvas, image
clientYViewport topFixed UI, window-relative tips
pageYDocument topAbsolute markers on a scrolled page
screenYScreen (monitor) topRare OS/monitor-level needs
layerYLayer (non-standard)Legacy only—prefer offsetY

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

⚡ Quick Reference

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

🔍 At a Glance

Four facts about offsetY.

Kind
instance

On the event

Type
double

CSS pixels

Origin
padding

Target top edge

Status
baseline

Widely available

Examples Gallery

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

📚 Getting Started

Read offsetY 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, offsetY is measured from that box’s top padding edge—ideal for element-local vertical tools.

Example 2 — Compare offsetY and clientY

Same click, two coordinate systems: element-local vs viewport.

JavaScript
box.addEventListener("click", (e) => {
  console.log({
    offsetY: e.offsetY, // relative to the box
    clientY: e.clientY, // relative to the viewport
    pageY: e.pageY      // relative to the document
  });
});
Try It Yourself

How It Works

Move the box down the page and click the same corner again: offsetY stays similar, while clientY changes with the window position.

📈 Synthetic Events & Local Layout

Construct coordinates, map vertical percentages, place markers.

Example 3 — Synthetic Event with offsetY

Useful in tests when you need a known vertical 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 Vertical Percentage

Normalize local Y for responsive volume meters or vertical scrubbers.

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

How It Works

Dividing offsetY by the element’s height 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 top maps cleanly to offsetY (you may subtract half the marker size to center it).

🚀 Common Use Cases

  • Drawing or sampling rows on a <canvas> or image.
  • Custom vertical sliders and volume meters using 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 vertical point.
  • Replacing non-standard layerY with a standard element-local Y.

🔧 How It Works

1

Pointer hits an element

The browser picks event.target for this mouse event.

Target
2

Measure from the top padding edge

Vertical distance from the target’s top padding edge.

Layout
3

MouseEvent stores offsetY

Your listener reads the local Y in CSS pixels.

Event
4

Use local coordinates

Draw, place markers, or compute vertical 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 top padding edge (inside the border).
  • Value type is a floating-point double in modern engines.
  • Related: offsetX, clientY, layerY, MouseEvent(), Window, JavaScript hub.

Universal Browser Support

MouseEvent.offsetY 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.offsetY

Reliable element-local Y from the target padding edge—great for canvas, vertical 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
offsetY Excellent

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

Conclusion

event.offsetY gives you the vertical mouse position relative to the target element’s top padding edge. Use it for canvas clicks, local markers, and percentage-based vertical controls; reach for clientY when you need viewport space instead.

Continue with offsetX, clientY, pageX, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use offsetY for element-local vertical hit positions
  • Pair with offsetX for a full local point
  • Prefer it over non-standard layerY for new code
  • Normalize with element height for responsive percentages
  • Remember the origin is the top padding edge

❌ Don’t

  • Confuse offsetY with viewport clientY
  • Assume it equals pageY after scrolling
  • Ignore borders when mentally mapping the padding edge
  • Use viewport-fixed UI math with local coordinates
  • Mutate event.offsetY on live user events

Key Takeaways

Knowledge Unlocked

Five things to remember about offsetY

Element-local Y from the target top padding edge.

5
Core concepts
🔢 02

Double

CSS pixels

Type
📐 03

Padding edge

target top

Origin
📈 04

Local UX

canvas / meters

Use
🛡️ 05

Baseline

widely available

Status

❓ Frequently Asked Questions

It is a read-only number: the vertical offset of the mouse pointer from the padding edge of the event’s target element, measured in CSS pixels.
No. MDN marks MouseEvent.offsetY as Baseline Widely available (since December 2015). It is not Deprecated, Experimental, or Non-standard, so no status warning banner is needed.
offsetY is measured from the top 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.
clientY is relative to the browser viewport. offsetY is relative to the element that received the event (event.target). Moving the same element on the page changes clientY for the same spot on the element, but offsetY stays local to that element.
pageY is relative to the whole document (includes vertical scroll). offsetY 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?

Like offsetX, MDN notes that early versions of the specification defined offsetY as an integer, while today it is a floating-point double. That is why you may see values like 20.5 on high-DPI displays instead of whole numbers only.

Next: MouseEvent.pageX

Learn document-relative X coordinates that include horizontal scroll.

pageX →

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