JavaScript MouseEvent clientX Property

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

What You’ll Learn

MouseEvent.clientX is a read-only number: the mouse’s horizontal position inside the viewport (what you see in the window). Learn how it differs from pageX and screenX, why scroll does not change the left-edge rule, and five try-it labs.

01

Kind

Instance property

02

Returns

Number (CSS px)

03

Status

Baseline Widely available

04

Origin

Viewport left edge

05

Pair with

clientY

06

Scroll

Does not shift origin

Introduction

Tooltips, custom cursors, and click maps often need “where is the pointer in the window?” That is clientX (horizontal) and clientY (vertical)—coordinates relative to the visible viewport, not the whole scrolled page.

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

Imagine a sticky ruler glued to the browser window, not the long document behind it. Zero is always the left edge of what you currently see.

Understanding the Property

MDN: clientX provides the horizontal coordinate within the application’s viewport at which the event occurred (as opposed to the coordinate within the page). Clicking the left edge of the viewport always yields clientX of 0, even if the page is scrolled horizontally.

  • Instance — read event.clientX in a handler.
  • Number — CSS pixels; often a floating-point double.
  • Viewport-relative — not document scroll, not the OS screen.
  • Read-only — set via constructor options on synthetic events only.

📝 Syntax

JavaScript
const x = mouseEvent.clientX;

Value

A number (double) in CSS pixels from the left edge of the viewport.

⚖️ clientX vs Other X Coordinates

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

Pair clientX with clientY for a full viewport point. For document-relative placement (for example absolute-positioned markers on a tall page), prefer pageX / pageY.

⚡ Quick Reference

GoalCode
Read viewport Xevent.clientX
Viewport point{ x: event.clientX, y: event.clientY }
Fixed UI at pointerel.style.left = event.clientX + "px"
Synthetic clicknew MouseEvent("click", { clientX: 100, clientY: 50 })
MDN statusBaseline Widely available (Jul 2015)

🔍 At a Glance

Four facts about clientX.

Kind
instance

On the event

Type
number

CSS pixels

Origin
viewport

Left edge = 0

Status
baseline

Widely available

Examples Gallery

Examples follow MDN MouseEvent.clientX. Move the mouse and compare coordinate systems in the try-it labs.

📚 Getting Started

Read clientX from live pointer events.

Example 1 — Log Client and Screen Coordinates

MDN-style: print positions on every mousemove.

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

document.addEventListener("mousemove", (e) => {
  screenLog.innerText = `
    Screen X/Y: ${e.screenX}, ${e.screenY}
    Client X/Y: ${e.clientX}, ${e.clientY}`;
});
Try It Yourself

How It Works

clientX / clientY update as the pointer moves inside the window. screenX / screenY use the whole monitor.

Example 2 — Left Edge Stays 0 After Scroll

MDN insight: viewport left is always client X of zero.

JavaScript
// Wide page + horizontal scroll — click near the left of the *window*
document.addEventListener("click", (e) => {
  console.log("clientX:", e.clientX);
  console.log("pageX:", e.pageX);
  // Near viewport left: clientX ≈ 0 even if pageX is large after scrolling
});
Try It Yourself

How It Works

After scrolling, the same visual spot near the window’s left edge still reports a small clientX, while pageX includes the scroll offset.

📈 Synthetic Events, pageX & UI Follow

Construct coordinates, compare systems, move fixed UI.

Example 3 — Synthetic Event with clientX

Useful in tests when you need a known viewport point.

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

console.log(event.clientX); // 120
console.log(event.clientY); // 40
Try It Yourself

How It Works

Pass numeric clientX / clientY to MouseEvent(). Some engines require numbers for these fields.

Example 4 — Compare clientX and pageX

When scrollX is zero they often match; after scrolling they diverge.

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

How It Works

A useful mental model: pageX ≈ clientX + window.scrollX (and similarly for Y). Prefer the property that matches your layout coordinate system.

Example 5 — Position Fixed UI with clientX

Great for floating labels that stick to the viewport.

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

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

How It Works

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

🚀 Common Use Cases

  • Floating tooltips and custom cursors that track the pointer in the window.
  • Hit-testing UI chrome that is fixed to the viewport.
  • Logging pointer position while teaching coordinate systems.
  • Synthetic events in tests with known clientX / clientY.
  • Choosing between viewport (client*) and document (page*) layouts.

🔧 How It Works

1

Pointer moves or clicks

OS reports a position relative to the display.

Input
2

Browser maps to the viewport

Converts into CSS pixels from the window’s left edge.

Viewport
3

MouseEvent stores clientX

Your listener reads the horizontal viewport coordinate.

Event
4

Drive fixed UI or hit tests

Use with clientY for a full viewport point.

📝 Notes

Universal Browser Support

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

Horizontal mouse position in viewport CSS pixels—ideal for fixed UI that follows the pointer.

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

Bottom line: Read event.clientX for viewport X; use pageX when you need document coordinates.

Conclusion

event.clientX is the pointer’s X inside the viewport. Use it with clientY for window-relative UI, and switch to pageX when the document scroll position matters.

Continue with clientY, buttons, MouseEvent(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use clientX for position: fixed overlays
  • Pair with clientY for a full point
  • Prefer pageX for document-absolute markers
  • Pass numbers in synthetic MouseEvent options
  • Remember left viewport edge → 0

❌ Don’t

  • Assume clientX equals pageX after scrolling
  • Confuse viewport coords with screenX
  • Forget high-DPI can yield fractional pixel values
  • Use clientX alone when layout is scrolled document space
  • Expect synthetic coords to move the real OS cursor

Key Takeaways

Knowledge Unlocked

Five things to remember about clientX

Viewport X for every mouse event.

5
Core concepts
📐 02

Viewport

not the page

Origin
🔢 03

Number

CSS pixels

Type
⚖️ 04

≠ pageX

scroll differs

Compare
🛡️ 05

Baseline

widely available

Status

❓ Frequently Asked Questions

It is a read-only number: the horizontal mouse position in CSS pixels relative to the browser viewport (the visible window area), not the full page document.
No. MDN marks MouseEvent.clientX as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard, so no status warning banner is needed.
clientX is measured from the left edge of the viewport and ignores page scroll. pageX is measured from the left edge of the document and grows when you scroll right.
screenX is relative to the entire screen (monitor). clientX is relative to the browser viewport only.
Because the viewport’s left edge is always X = 0 for client coordinates. Scrolling the document does not move that edge, so clientX stays based on what you see in the window.
Pass clientX (and usually clientY) in MouseEvent options, for example new MouseEvent("click", { clientX: 120, clientY: 40 }).
Did you know?

MDN highlights a simple test: click the left edge of the browser window and clientX is 0 whether or not the page is scrolled sideways. That single fact is the fastest way to remember viewport vs page coordinates.

Next: MouseEvent.clientY

Learn viewport Y coordinates for mouse events.

clientY →

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