JavaScript Document elementFromPoint() Method

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

What You’ll Learn

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

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 topmost Element 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.

Related tutorials: caretPositionFromPoint(), caretRangeFromPoint(), createElement().

Understanding document.elementFromPoint()

An instance method on Document (usually document.elementFromPoint(x, y) on the live page).

  • x — horizontal coordinate from the left edge of the viewport (MDN).
  • y — vertical coordinate from the top edge of the viewport (MDN).
  • Return value — the topmost Element at that point (MDN).
  • null — outside the visible document bounds, or either coordinate is negative (MDN).
  • pointer-events: none — ignored; the element below is returned (MDN).
  • iframes — if the point hits another document, the parent element (the <iframe>) is returned (MDN).

📝 Syntax

General form of Document.elementFromPoint (MDN):

JavaScript
elementFromPoint(x, y)

Parameters

  • x — horizontal coordinate of a point, relative to the left edge of the current viewport (MDN).
  • y — vertical coordinate of a point, relative to the top edge of the current viewport (MDN).

Return value

The topmost Element object located at the specified coordinates (MDN), or null when the point is invalid / outside (MDN).

Exceptions

None highlighted on MDN for this method.

MDN example

JavaScript
function changeColor(newColor) {
  const elem = document.elementFromPoint(2, 2);
  elem.style.color = newColor;
}

document.querySelectorAll("button").forEach((button) => {
  button.addEventListener("click", (event) => {
    changeColor(event.target.textContent.toLowerCase());
  });
});

⚡ Quick Reference

GoalCode
Hit-test a pointdocument.elementFromPoint(x, y)
Under the mousedocument.elementFromPoint(e.clientX, e.clientY)
Guard nullconst el = document.elementFromPoint(x, y); if (!el) return;
Read tagel.tagName
Need text offsetdocument.caretPositionFromPoint(x, y) (MDN)
MDN statusBaseline Widely available

🔍 At a Glance

Four facts about document.elementFromPoint().

Returns
Element

or null

Coords
viewport

x, y

Picks
topmost

MDN

Status
Baseline

since 2015

📋 Valid point vs invalid point

Inside visible viewportOutside / negative coords
Typical resultTopmost Elementnull (MDN)
Safe codingUse the elementAlways null-check first
Mouse helpersclientX / clientY match viewportDo not use pageX/pageY alone

Examples Gallery

Examples follow MDN Document: elementFromPoint() and practical viewport hit-test patterns.

📚 Getting Started

Hit-test fixed and mouse coordinates.

Example 1 — MDN: change color at (2, 2)

Find the element near the top-left of the viewport and restyle it.

JavaScript
function changeColor(newColor) {
  const elem = document.elementFromPoint(2, 2);
  if (!elem) return;
  elem.style.color = newColor;
}

changeColor("blue");
console.log(document.elementFromPoint(2, 2).tagName);
Try It Yourself

How It Works

MDN’s demo uses a tiny inset from the corner so the hit lands on the paragraph (or whatever is painted there in your layout).

Example 2 — Element under the mouse

Use clientX / clientY — they are already viewport-relative.

JavaScript
document.addEventListener("mousemove", (event) => {
  const el = document.elementFromPoint(event.clientX, event.clientY);
  console.log(el ? el.tagName : "null");
});
Try It Yourself

How It Works

Mouse event clientX/clientY match the coordinate system MDN describes for elementFromPoint. Prefer them over pageX/pageY for this API.

📈 Practical Patterns

Null results, pointer-events, and measuring a box center.

Example 3 — null for invalid points (MDN)

Negative coordinates return null.

JavaScript
console.log(document.elementFromPoint(-1, 10)); // null
console.log(document.elementFromPoint(10, -1)); // null
console.log(document.elementFromPoint(10, 10) !== null); // usually true
Try It Yourself

How It Works

MDN: outside the visible bounds, or either coordinate negative → null. Always null-check before reading properties.

Example 4 — pointer-events: none is ignored (MDN)

The overlay is skipped; the element underneath is returned.

JavaScript
// #overlay has pointer-events: none and covers #target
const box = document.getElementById("target");
const rect = box.getBoundingClientRect();
const x = rect.left + rect.width / 2;
const y = rect.top + rect.height / 2;

const el = document.elementFromPoint(x, y);
console.log(el && el.id); // "target" (not "overlay")
Try It Yourself

How It Works

MDN: elements with pointer-events: none are ignored for this hit test.

Example 5 — Hit-test the center of an element

Convert a layout box to viewport coordinates with getBoundingClientRect().

JavaScript
const card = document.getElementById("card");
const r = card.getBoundingClientRect();
const midX = r.left + r.width / 2;
const midY = r.top + r.height / 2;

const hit = document.elementFromPoint(midX, midY);
console.log(hit === card || card.contains(hit)); // true
Try It Yourself

How It Works

getBoundingClientRect() already returns viewport coordinates, so its midpoints plug straight into elementFromPoint.

🚀 Common Use Cases

  • Inspect under the cursor — debug overlays and custom inspectors.
  • Drag / drop helpers — find the drop target under a pointer.
  • Games / canvases beside DOM — see which HTML control sits at a point.
  • Ignore ghost overlays — rely on pointer-events: none (MDN).
  • Not for text offsets — use caretPositionFromPoint (MDN).
  • Stacked layers — consider elementsFromPoint when you need every element.

🧠 How elementFromPoint() Works

1

Pass viewport x, y

MDN: coordinates are relative to the current viewport.

Input
2

Validate the point

Outside / negative → null (MDN).

Check
3

Skip non-interactive layers

pointer-events: none elements are ignored (MDN).

Filter
4

Return the topmost Element

Or the iframe element if the hit is in a nested document (MDN).

📝 Notes

  • MDN: Baseline Widely available since July 2015.
  • Coordinates are viewport-relative (MDN).
  • Returns the topmost Element, or null outside / for negative coords (MDN).
  • pointer-events: none elements are ignored (MDN).
  • For text caret offsets, prefer caretPositionFromPoint() (MDN).
  • Related: caretPositionFromPoint(), caretRangeFromPoint(), createTreeWalker().

Browser Support

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.

Baseline Widely available
Google Chrome Supported
Yes
Mozilla Firefox Supported
Yes
Apple Safari Supported
Yes
Microsoft Edge Supported
Yes
Opera Supported
Yes
Internet Explorer Supported (legacy)
Yes
elementFromPoint() Wide

Bottom line: Use elementFromPoint(x, y) for viewport hit tests. Null-check results, and prefer clientX/clientY from mouse events.

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.

Continue with createTreeWalker(), elementsFromPoint(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Pass viewport coordinates (clientX / clientY)
  • Null-check before reading tagName or styles
  • 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)

Key Takeaways

Knowledge Unlocked

Five things to remember about elementFromPoint()

Viewport hit-test for the topmost Element.

5
Core concepts
📄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.

Next: elementsFromPoint()

Learn how to get the full top-to-bottom Element stack at viewport x/y.

elementsFromPoint() →

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.

7 people found this page helpful