JavaScript MouseEvent x Property

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

What You’ll Learn

MouseEvent.x is a read-only number and an alias for clientX: horizontal position inside the viewport. Learn why the short name exists, prove x === clientX, and practice with five try-it labs.

01

Kind

Instance property

02

Returns

Number (CSS px)

03

Status

Baseline Widely available

04

Alias of

clientX

05

Pair with

y / clientY

06

Origin

Viewport left edge

Introduction

Some APIs like short names. event.x is simply another way to read the same viewport X that event.clientX already gives you. Pair it with event.y (alias of clientY) for a compact viewport point.

JavaScript
document.addEventListener("mousemove", (event) => {
  console.log(event.x);       // same as event.clientX
  console.log(event.clientX); // viewport X in CSS pixels
});
💡
Beginner tip

MDN one-liner: MouseEvent.x is an alias for MouseEvent.clientX. If you already understand clientX, you already understand x.

Understanding the Property

MDN: the x property is an alias for clientX. That means it is the horizontal coordinate within the application’s viewport at which the event occurred—not the document page and not the OS screen.

  • Instance — read event.x in a handler.
  • Number — CSS pixels from the viewport’s left edge.
  • Alias — same value as event.clientX.
  • Read-only — set via constructor options (usually clientX) on synthetic events.

📝 Syntax

JavaScript
const x = mouseEvent.x;

Value

A number in CSS pixels from the left edge of the viewport—identical to mouseEvent.clientX on supporting browsers.

⚖️ x vs Other X Coordinates

PropertyMeasured fromNotes
xViewport leftAlias of clientX
clientXViewport leftSame value as x
pageXDocument leftIncludes horizontal scroll
screenXScreen (monitor) leftIndependent of page scroll

Prefer clientX in team code if clarity matters; use x when you want the short CSSOM View names alongside y.

⚡ Quick Reference

GoalCode
Read viewport Xevent.x or event.clientX
Prove aliasevent.x === event.clientX
Viewport point{ x: event.x, y: event.y }
Synthetic clicknew MouseEvent("click", { clientX: 100, clientY: 50 })
MDN statusBaseline Widely available (Apr 2017)

🔍 At a Glance

Four facts about x.

Kind
instance

On the event

Type
number

CSS pixels

Alias
clientX

Same value

Status
baseline

Widely available

Examples Gallery

Examples follow MDN MouseEvent.x and the related clientX behavior. Move the mouse and confirm the alias in the try-it labs.

📚 Getting Started

Read x from live pointer events.

Example 1 — Log x and y

Short names for viewport coordinates on every move.

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

document.addEventListener("mousemove", (e) => {
  log.textContent = `x: ${e.x}, y: ${e.y}`;
});
Try It Yourself

How It Works

e.x / e.y update as the pointer moves. They are viewport coordinates—the same numbers you would get from clientX / clientY.

Example 2 — Prove x === clientX

MDN’s core claim as a one-line check.

JavaScript
document.addEventListener("click", (e) => {
  console.log(e.x);
  console.log(e.clientX);
  console.log(e.x === e.clientX); // true when both are numbers
});
Try It Yourself

How It Works

If the equality check prints true, you have confirmed the alias on this browser for that click.

📈 Synthetic Events, Compare & UI

Construct coordinates, compare systems, move fixed UI.

Example 3 — Synthetic Event: clientX Sets x

Pass clientX in options; read either name afterward.

JavaScript
const event = new MouseEvent("click", {
  clientX: 120,
  clientY: 40,
  bubbles: true
});

console.log(event.clientX); // 120
console.log(event.x);       // 120 (alias)
console.log(event.x === event.clientX); // true
Try It Yourself

How It Works

The MouseEvent() constructor commonly accepts clientX / clientY. Engines that implement the alias then expose the same numbers on x / y.

Example 4 — Compare x, clientX, and pageX

Alias pair vs document coordinate.

JavaScript
document.addEventListener("click", (e) => {
  console.log({
    x: e.x,
    clientX: e.clientX,
    pageX: e.pageX,
    scrollX: window.scrollX
  });
  // x and clientX match; pageX ≈ clientX + scrollX
});
Try It Yourself

How It Works

x and clientX stay locked together. pageX diverges once the document is scrolled horizontally.

Example 5 — Position Fixed UI with x

Same pattern as clientX, shorter property names.

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

document.addEventListener("mousemove", (e) => {
  tip.style.left = `${e.x + 12}px`;
  tip.style.top = `${e.y + 12}px`;
  tip.textContent = `${e.x}, ${e.y}`;
});
Try It Yourself

How It Works

With position: fixed, left/top are already in viewport space—so x / y map directly, just like clientX / clientY.

🚀 Common Use Cases

  • Compact viewport points: { x: event.x, y: event.y }.
  • Floating tooltips and custom cursors (same as clientX).
  • Teaching that CSSOM View exposes short aliases for client coordinates.
  • Code that already uses x / y naming consistently.
  • Quick equality checks while debugging coordinate systems.

🔧 How It Works

1

Pointer moves or clicks

The browser computes viewport coordinates.

Input
2

MouseEvent stores clientX

Horizontal CSS pixels from the viewport’s left edge.

Viewport
3

x aliases that value

Reading event.x returns the same number as clientX.

Alias
4

Use either name

Pick x or clientX; keep the team consistent.

📝 Notes

Universal Browser Support

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

Baseline · Widely available

MouseEvent.x

Short alias for clientX—viewport X in CSS pixels for mouse events.

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

Bottom line: Read event.x or event.clientX for viewport X; they are the same value on supporting browsers.

Conclusion

event.x is the short name for viewport X—an alias of clientX. Use either property for fixed UI and hit tests; reach for pageX when document scroll matters.

Continue with y, clientX, clientY, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Treat x as identical to clientX
  • Pair with y (or clientY) for a full point
  • Use position: fixed overlays with viewport coordinates
  • Prefer one naming style in a codebase for consistency
  • Pass clientX in synthetic MouseEvent options

❌ Don’t

  • Assume x equals pageX after scrolling
  • Confuse viewport x with screen screenX
  • Mix x and clientX randomly in the same file
  • Use viewport coords for document-absolute markers
  • Mutate event.x on live user events

Key Takeaways

Knowledge Unlocked

Five things to remember about x

Short alias for clientX—viewport horizontal position.

5
Core concepts
🔄 02

Alias

of clientX

MDN
📐 03

Viewport

left edge origin

Origin
🔢 04

Number

CSS pixels

Type
🛡️ 05

Baseline

widely available

Status

❓ Frequently Asked Questions

It is a read-only number that is an alias for MouseEvent.clientX: the horizontal mouse position in CSS pixels relative to the browser viewport (the visible window area).
No. MDN marks MouseEvent.x as Baseline Widely available (since April 2017). It is not Deprecated, Experimental, or Non-standard, so no status warning banner is needed.
Yes. MDN states that MouseEvent.x is an alias for MouseEvent.clientX. In supporting browsers, event.x === event.clientX for the same event.
Both work. Many tutorials and codebases prefer clientX because the name clearly says “client/viewport.” Use x when you want shorter property names or CSSOM View-style naming next to event.y.
x / clientX are viewport-relative and ignore document scroll. pageX is document-relative and grows when you scroll right.
Pass clientX in MouseEvent options (for example new MouseEvent("click", { clientX: 120, clientY: 40 })). Engines typically expose the same value on event.x as well.
Did you know?

MouseEvent.x and MouseEvent.y come from the CSSOM View Module, which also defines many scrolling and layout measurement APIs. The short names make mouse coordinates feel more like a simple { x, y } point.

Next: MouseEvent.y

Learn the short alias for clientY—viewport Y coordinates.

y →

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