JavaScript MouseEvent pageX Property

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

What You’ll Learn

MouseEvent.pageX is a read-only number: the mouse’s horizontal position relative to the entire document, including scrolled-away content. Learn how it differs from clientX and offsetX, the scroll formula, and five try-it labs.

01

Kind

Instance property

02

Returns

Number (CSS px)

03

Status

Baseline Widely available

04

Origin

Document left edge

05

Pair with

pageY

06

Scroll

Included in the value

Introduction

Absolute-positioned pins, annotations on a long page, and document-space hit maps need “where on the page?” That is pageX (horizontal) and pageY (vertical)—coordinates relative to the whole document, not just the visible window.

JavaScript
document.addEventListener("mousemove", (event) => {
  console.log(event.pageX); // pixels from document left (includes scroll)
});
💡
Beginner tip

MDN: if 200px of the left side is scrolled out of view and you click 100px from the viewport’s left edge, pageX is 300.

Understanding the Property

MDN: pageX returns the X (horizontal) coordinate (in pixels) at which the mouse was clicked, relative to the left edge of the entire document. This includes any portion of the document not currently visible.

  • Instance — read event.pageX in a handler.
  • Number — CSS pixels; a floating-point double.
  • Document-relative — includes horizontal scroll.
  • Read-only — set via constructor options on synthetic events only.

📝 Syntax

JavaScript
const x = mouseEvent.pageX;

Value

A double floating-point number of pixels from the left edge of the document (MDN), regardless of scrolling or viewport positioning. Originally specified as a long integer; CSSOM View redefined it as a double for subpixel precision.

⚖️ pageX vs Other X Coordinates

PropertyMeasured fromScroll effect
pageXDocument leftGrows when you scroll right
clientXViewport leftOrigin stays fixed to the window
offsetXTarget padding edgeLocal to the hit element
screenXScreen (monitor) leftIndependent of page scroll

Mental model: pageX ≈ clientX + window.scrollX. Pair pageX with pageY for a full document point.

⚡ Quick Reference

GoalCode
Read document Xevent.pageX
Document point{ x: event.pageX, y: event.pageY }
Relate to viewportpageX ≈ clientX + scrollX
Absolute markerel.style.left = event.pageX + "px"
MDN statusBaseline Widely available (Jul 2015)

🔍 At a Glance

Four facts about pageX.

Kind
instance

On the event

Type
double

CSS pixels

Origin
document

Left edge = 0

Status
baseline

Widely available

Examples Gallery

Examples follow MDN MouseEvent.pageX. Move the mouse and scroll horizontally in the try-it labs to see document vs viewport values.

📚 Getting Started

Read pageX / pageY from live pointer events.

Example 1 — Show Position Relative to Page Origin

MDN-style demo: update labels on mousemove inside a box.

JavaScript
const box = document.querySelector(".box");
const pageXEl = document.getElementById("x");
const pageYEl = document.getElementById("y");

function updateDisplay(event) {
  pageXEl.innerText = event.pageX;
  pageYEl.innerText = event.pageY;
}

box.addEventListener("mousemove", updateDisplay);
box.addEventListener("mouseenter", updateDisplay);
box.addEventListener("mouseleave", updateDisplay);
Try It Yourself

How It Works

Each move updates the labels from event.pageX and event.pageY—document-relative coordinates, as on MDN.

Example 2 — Scroll Adds to pageX

MDN insight: scrolled-away pixels are included in the document X.

JavaScript
// Wide page + horizontal scroll — click near the left of the *window*
document.addEventListener("click", (e) => {
  console.log("clientX:", e.clientX);
  console.log("scrollX:", window.scrollX);
  console.log("pageX:", e.pageX);
  // Often: pageX ≈ clientX + scrollX
  // Example: scrollX 200 + clientX 100 → pageX 300
});
Try It Yourself

How It Works

clientX stays small near the window’s left edge; pageX adds the horizontal scroll distance.

📈 Synthetic Events, Compare & Markers

Construct coordinates, compare systems, place absolute markers.

Example 3 — Synthetic Event with pageX

Useful in tests when you need a known document point.

JavaScript
const event = new MouseEvent("click", {
  pageX: 320,
  pageY: 80,
  bubbles: true
});

console.log(event.pageX); // 320
console.log(event.pageY); // 80
Try It Yourself

How It Works

Pass numeric pageX / pageY to MouseEvent() when tests assert document-space positions.

Example 4 — Compare pageX, clientX, offsetX

Three X systems on the same click.

JavaScript
box.addEventListener("click", (e) => {
  console.log({
    pageX: e.pageX,     // document
    clientX: e.clientX, // viewport
    offsetX: e.offsetX, // target padding edge
    scrollX: window.scrollX
  });
});
Try It Yourself

How It Works

With scrollX === 0, pageX and clientX often match; offsetX stays local to the box.

Example 5 — Place an Absolute Marker with pageX

Document-space pins stay correct after horizontal scroll.

JavaScript
const pin = document.querySelector("#pin");

document.addEventListener("click", (e) => {
  // pin { position: absolute } on the document/body
  pin.style.left = `${e.pageX}px`;
  pin.style.top = `${e.pageY}px`;
});
Try It Yourself

How It Works

With position: absolute relative to the document, left/top map to pageX / pageY. For fixed viewport UI, prefer clientX instead.

🚀 Common Use Cases

  • Absolute markers and annotations on scrolled pages.
  • Document-space hit maps and click heatmaps.
  • Teaching the difference between viewport and document coordinates.
  • Synthetic events in tests with known pageX / pageY.
  • Layouts that use position: absolute in document space.

🔧 How It Works

1

Pointer clicks or moves

OS reports a position relative to the display.

Input
2

Browser maps to the document

Adds horizontal scroll so X is from the page’s left edge.

Document
3

MouseEvent stores pageX

Your listener reads the horizontal document coordinate.

Event
4

Use document coordinates

Place absolute UI or compare with clientX + scrollX.

📝 Notes

  • Baseline Widely available (MDN, since July 2015).
  • Not Deprecated, Experimental, or Non-standard — no status banner required.
  • Includes any portion of the document not currently visible (horizontal scroll).
  • Value type is a floating-point double in modern engines (CSSOM View).
  • Related: offsetY, clientX, offsetX, MouseEvent(), Window, JavaScript hub.

Universal Browser Support

MouseEvent.pageX is Baseline Widely available across modern browsers (MDN: since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline · Widely available

MouseEvent.pageX

Reliable document-relative X including horizontal scroll—great for absolute page markers.

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
pageX Excellent

Bottom line: Read event.pageX for document-space horizontal position; pair with pageY for a full document point.

Conclusion

event.pageX gives you the horizontal mouse position relative to the entire document, including scrolled content. Use it for absolute page markers; reach for clientX for viewport UI and offsetX for element-local clicks.

Continue with pageY, clientX, offsetX, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use pageX for document-space horizontal positions
  • Pair with pageY for a full document point
  • Remember pageX ≈ clientX + scrollX
  • Use absolute positioning when placing page markers
  • Prefer clientX for position: fixed UI

❌ Don’t

  • Confuse pageX with viewport clientX
  • Ignore horizontal scroll when debugging large pageX values
  • Use page coordinates for fixed tooltips without converting
  • Assume it equals offsetX inside an element
  • Mutate event.pageX on live user events

Key Takeaways

Knowledge Unlocked

Five things to remember about pageX

Document-relative X that includes horizontal scroll.

5
Core concepts
🔢 02

Double

CSS pixels

Type
📄 03

Document

left edge origin

Origin
📈 04

Scroll-aware

+ scrollX

Math
🛡️ 05

Baseline

widely available

Status

❓ Frequently Asked Questions

It is a read-only number: the horizontal mouse position in CSS pixels relative to the left edge of the entire document, including any part of the page that is scrolled out of view.
No. MDN marks MouseEvent.pageX as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard, so no status warning banner is needed.
pageX includes horizontal scroll. MDN’s example: if 200px of the left side is scrolled away and you click 100px from the viewport’s left edge, pageX is 300.
clientX is relative to the viewport (window) and ignores document scroll. pageX is relative to the document and grows when you scroll right. Often: pageX ≈ clientX + window.scrollX.
offsetX is relative to the target element’s padding edge. pageX is relative to the whole document. Use offsetX for clicks inside a box; use pageX for markers on a scrolled page.
A double floating-point number of pixels. It was originally defined as a long integer; the CSSOM View Module redefined it as a double for subpixel precision.
Did you know?

MDN notes that pageX was originally specified as a long integer in the Touch Events world, then redefined in the CSSOM View Module as a double for subpixel precision. Both still show up as JavaScript Number, but engines may handle them differently under the hood.

Next: MouseEvent.pageY

Learn document-relative Y coordinates that include vertical scroll.

pageY →

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