document.elementFromPoint() is an instance method that returns the topmost Element at viewport coordinates (see MDN Document: elementFromPoint()). Learn x / y, when the result is null, how pointer-events: none is ignored, iframe notes, how this differs from caret APIs, and five try-it labs.
01
Kind
Instance method
02
Args
x, y (viewport)
03
Returns
Element or null
04
Picks
Topmost element
05
Ignores
pointer-events:none
06
Status
Baseline
Fundamentals
Introduction
Hit-testing answers a simple question: which element is under this point on the screen? Games, custom drag handles, and “what is under the mouse?” tools all need that answer.
document.elementFromPoint(x, y) returns the topmostElement at those coordinates. MDN: coordinates are relative to the viewport (not the full document scroll height).
💡
Viewport hit test
1) Pick viewport x and y 2) const el = document.elementFromPoint(x, y) 3) Use the element — or handle null if the point is off-screen
MDN tip: if you need a position inside text (caret offset), use caretPositionFromPoint() instead.
Document.elementFromPoint() is Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline Widely available
Document.elementFromPoint()
Find the topmost Element at viewport coordinates across all major browsers.
BaselineWidely available
Google ChromeSupported
Yes
Mozilla FirefoxSupported
Yes
Apple SafariSupported
Yes
Microsoft EdgeSupported
Yes
OperaSupported
Yes
Internet ExplorerSupported (legacy)
Yes
elementFromPoint()Wide
Bottom line: Use elementFromPoint(x, y) for viewport hit tests. Null-check results, and prefer clientX/clientY from mouse events.
Wrap Up
Conclusion
document.elementFromPoint(x, y) returns the topmost element under a viewport point. Guard against null, remember pointer-events: none is skipped, and switch to caret APIs when you need a text offset — just like MDN recommends.
Use getBoundingClientRect() to convert boxes to viewport points
Prefer caret APIs when you need text offsets (MDN)
Use elementsFromPoint when you need the full stack
❌ Don’t
Feed pageX/pageY without adjusting for scroll
Assume a non-null result for every coordinate
Expect overlays with pointer-events: none to be returned (MDN)
Use this alone when you need caret offsets inside text
Forget iframe hits return the iframe element itself (MDN)
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about elementFromPoint()
Viewport hit-test for the topmost Element.
5
Core concepts
📝01
Returns
Element
or null
📄02
Coords
viewport
MDN
🎯03
Picks
topmost
MDN
🚫04
Ignores
pe:none
MDN
🛡05
Status
Baseline
2015
❓ Frequently Asked Questions
MDN: Document.elementFromPoint() returns the topmost Element at the specified coordinates relative to the viewport.
No. MDN marks Document.elementFromPoint() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
MDN: x is the horizontal coordinate from the left edge of the current viewport; y is the vertical coordinate from the top edge of the current viewport.
MDN: if the specified point is outside the visible bounds of the document, or either coordinate is negative, the result is null.
MDN: elements with pointer-events set to none are ignored, and the element below is returned.
MDN: if you need the specific position inside the element (for example a text offset), use Document.caretPositionFromPoint(). elementFromPoint only returns the topmost Element.
Did you know?
If the point lands inside an <iframe>, MDN says you get the iframe element itself (from the parent document), not a node from the nested document — unless you call elementFromPoint on that nested document with coordinates relative to it.