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
Fundamentals
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.
Concept
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.
Foundation
📝 Syntax
JavaScript
const x = mouseEvent.clientX;
Value
A number (double) in CSS pixels from the left edge of the viewport.
Compare
⚖️ clientX vs Other X Coordinates
Property
Measured from
Scroll effect
clientX
Viewport left
Origin stays fixed to the window
pageX
Document left
Grows when you scroll right
screenX
Screen (monitor) left
Independent of page scroll
offsetX
Target element padding edge
Local 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.
Cheat Sheet
⚡ Quick Reference
Goal
Code
Read viewport X
event.clientX
Viewport point
{ x: event.clientX, y: event.clientY }
Fixed UI at pointer
el.style.left = event.clientX + "px"
Synthetic click
new MouseEvent("click", { clientX: 100, clientY: 50 })
MDN status
Baseline Widely available (Jul 2015)
Snapshot
🔍 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
Hands-On
Examples Gallery
Examples follow MDN MouseEvent.clientX. Move the mouse and compare coordinate systems in the try-it labs.
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
});
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.
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerSupported in modern IE versions
Full support
clientXExcellent
Bottom line: Read event.clientX for viewport X; use pageX when you need document coordinates.
Wrap Up
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.
Use clientX alone when layout is scrolled document space
Expect synthetic coords to move the real OS cursor
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about clientX
Viewport X for every mouse event.
5
Core concepts
🖱️01
Instance
on the event
Kind
📐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.