JavaScript Document elementsFromPoint() Method

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

What You’ll Learn

document.elementsFromPoint() is an instance method that returns an array of all elements at viewport coordinates (see MDN Document: elementsFromPoint()). Learn top-to-bottom ordering, how it relates to elementFromPoint(), reading stacks under the mouse, overlapping layers, and five try-it labs.

01

Kind

Instance method

02

Args

x, y (viewport)

03

Returns

Element[]

04

Order

Top → bottom

05

Like

elementFromPoint

06

Status

Baseline

Introduction

elementFromPoint() answers “what is on top?” elementsFromPoint() answers “what is the whole stack under this point?” — useful for overlays, stacking contexts, and debugging layers.

MDN: the method returns an array of all elements at the specified coordinates (viewport-relative), ordered from the topmost to the bottommost box. It operates in a similar way to elementFromPoint().

💡
Full hit stack

1) Pick viewport x, y
2) const stack = document.elementsFromPoint(x, y)
3) stack[0] is the topmost element; later indexes are underneath

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

Understanding document.elementsFromPoint()

An instance method on Document (usually document.elementsFromPoint(x, y)).

  • x / y — point coordinates (MDN); treat as viewport-relative like elementFromPoint.
  • Return value — an array of Element objects (MDN).
  • Order — topmost → bottommost (MDN).
  • Similarity — operates similarly to elementFromPoint() (MDN).
  • Index 0 — typically matches what elementFromPoint(x, y) returns.
  • Feature check — MDN example uses if (document.elementsFromPoint).

📝 Syntax

General form of Document.elementsFromPoint (MDN):

JavaScript
elementsFromPoint(x, y)

Parameters

  • x — the horizontal coordinate of a point (MDN).
  • y — the vertical coordinate of a point (MDN).

Return value

An array of Element objects, ordered from the topmost to the bottommost box of the viewport (MDN).

Exceptions

None highlighted on MDN for this method.

MDN example

JavaScript
let output = document.getElementById("output");
if (document.elementsFromPoint) {
  let elements = document.elementsFromPoint(30, 20);
  elements.forEach((elt, i) => {
    output.textContent += elt.localName;
    if (i < elements.length - 1) {
      output.textContent += " < ";
    }
  });
}

⚡ Quick Reference

GoalCode
Full stackdocument.elementsFromPoint(x, y)
Topmost onlydocument.elementsFromPoint(x, y)[0]
Under mousedocument.elementsFromPoint(e.clientX, e.clientY)
Print tagsstack.map((el) => el.localName).join(" < ")
Feature-detectif (document.elementsFromPoint) { ... }
MDN statusBaseline Widely available (since Jan 2020)

🔍 At a Glance

Four facts about document.elementsFromPoint().

Returns
Element[]

array

Order
top→bottom

MDN

Coords
viewport

x, y

Status
Baseline

since 2020

📋 One element vs the full stack

elementFromPointelementsFromPoint
Result typeSingle Element or nullElement[]
Sees overlays under top?No (only topmost)Yes (full stack)
Typical useQuick hit targetLayer / overlay tools
MDN relationshipSibling APISimilar, returns all (MDN)

Examples Gallery

Examples follow MDN Document: elementsFromPoint() and practical stacking hit-test patterns.

📚 Getting Started

Read the element stack at a fixed or mouse point.

Example 1 — MDN: print the stack at (30, 20)

Join localName values with < like MDN’s sample.

JavaScript
const output = document.getElementById("output");
if (document.elementsFromPoint) {
  const elements = document.elementsFromPoint(30, 20);
  elements.forEach((elt, i) => {
    output.textContent += elt.localName;
    if (i < elements.length - 1) {
      output.textContent += " < ";
    }
  });
}
Try It Yourself

How It Works

MDN starts from the topmost painted box and walks downward through ancestors / stacked boxes at that point.

Example 2 — Stack under the mouse

Use clientX / clientY with the array API.

JavaScript
document.addEventListener("mousemove", (event) => {
  const stack = document.elementsFromPoint(event.clientX, event.clientY);
  console.log(stack.map((el) => el.localName).join(" < "));
});
Try It Yourself

How It Works

Mouse clientX/clientY match the viewport coordinate system used by hit-testing APIs.

📈 Practical Patterns

Compare APIs, inspect overlays, and search the stack.

Example 3 — First item vs elementFromPoint

Confirm the top of the stack matches the single-element API.

JavaScript
const x = 40;
const y = 40;
const top = document.elementFromPoint(x, y);
const stack = document.elementsFromPoint(x, y);

console.log(stack[0] === top); // true (typical)
console.log(stack.length);
Try It Yourself

How It Works

MDN says the APIs are similar; the plural form just keeps every layer instead of stopping at the top.

Example 4 — Overlapping positioned layers

See both the overlay and the card underneath in one array.

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

console.log(stack.map((el) => el.id || el.localName).join(" < "));
// e.g. "overlay < card < body < html"
Try It Yourself

How It Works

When two boxes overlap at the same viewport point, both can appear in the returned stack (plus ancestors such as body / html).

Example 5 — Find a specific element in the stack

Use Array.prototype.find / includes on the result.

JavaScript
const panel = document.getElementById("panel");
const r = panel.getBoundingClientRect();
const stack = document.elementsFromPoint(r.left + 8, r.top + 8);

const hitPanel = stack.includes(panel);
const firstDiv = stack.find((el) => el.localName === "div");

console.log(hitPanel);                 // true
console.log(firstDiv && firstDiv.id);  // "panel" (or a child on top)
Try It Yourself

How It Works

Because the return value is a normal array, you can filter, find, or map without extra DOM APIs.

🚀 Common Use Cases

  • Overlay debugging — see every layer under the cursor.
  • Custom inspectors — list the hit stack like DevTools.
  • Drop targets under modals — peek at elements beneath a transparent layer.
  • Games / HUD UI — decide which stacked control was intended.
  • When one is enough — prefer elementFromPoint for a single top hit.
  • Text carets — still use caret APIs for offsets inside text.

🧠 How elementsFromPoint() Works

1

Pass x, y

Point coordinates relative to the viewport (like elementFromPoint).

Input
2

Collect every hit box

MDN: all elements at that point, not only the topmost.

Stack
3

Sort top → bottom

MDN: ordered from the topmost to the bottommost box.

Order
4

Return an Element array

Map, filter, or compare index 0 with elementFromPoint.

📝 Notes

  • MDN: Baseline Widely available since January 2020.
  • Returns an array ordered topmost → bottommost (MDN).
  • Operates similarly to elementFromPoint() (MDN).
  • Prefer viewport coords (clientX / clientY).
  • MDN’s sample feature-detects with if (document.elementsFromPoint).
  • Related: elementFromPoint(), caretPositionFromPoint(), createTreeWalker().

Browser Support

Document.elementsFromPoint() is Baseline Widely available on MDN (since January 2020). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline Widely available

Document.elementsFromPoint()

Return every Element stacked at viewport coordinates across modern 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 Not supported
No
elementsFromPoint() Wide

Bottom line: Use elementsFromPoint(x, y) when you need the full top-to-bottom hit stack. Prefer elementFromPoint when only the topmost Element matters.

Conclusion

document.elementsFromPoint(x, y) returns every element under a viewport point, ordered topmost to bottommost. Use it for overlays and layer tools; reach for elementFromPoint when you only need the top hit — just like MDN describes the relationship between the two APIs.

Continue with elementFromPoint(), enableStyleSheetsForSet(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use viewport coordinates (clientX / clientY)
  • Treat stack[0] as the topmost hit
  • Feature-detect in mixed / older environments (MDN pattern)
  • Prefer this API when overlays matter
  • Map localName / id for readable debug output

❌ Don’t

  • Confuse page coordinates with viewport coordinates
  • Assume the array never includes html / body
  • Use this when a single top element is enough
  • Expect text caret offsets (use caret APIs)
  • Forget IE lacks support (prefer modern Baseline browsers)

Key Takeaways

Knowledge Unlocked

Five things to remember about elementsFromPoint()

Full top-to-bottom Element stack at a viewport point.

5
Core concepts
📄02

Order

top→bottom

MDN
🎯03

Like

elementFromPoint

stack
🔎04

Index 0

topmost

usual
🛡05

Status

Baseline

2020

❓ Frequently Asked Questions

MDN: Document.elementsFromPoint() returns an array of all elements at the specified coordinates (relative to the viewport), ordered from the topmost to the bottommost box.
No. MDN marks Document.elementsFromPoint() as Baseline Widely available (since January 2020). It is not Deprecated, Experimental, or Non-standard.
MDN: it operates similarly to elementFromPoint(), but returns every element in the stack instead of only the topmost Element.
MDN: ordered from the topmost to the bottommost box of the viewport. Index 0 is usually the same element elementFromPoint() would return.
Yes. Like elementFromPoint, use viewport coordinates such as mouse event clientX and clientY.
MDN’s example checks if (document.elementsFromPoint) before calling. Modern Baseline browsers support it; a check still helps older environments.
Did you know?

MDN’s demo prints the stack with a < separator so it reads like “child under parent under parent” — a handy mental model when the array includes both overlapping siblings and ancestor containers such as body and html.

Next: enableStyleSheetsForSet()

Learn the deprecated non-standard API for enabling named alternate stylesheet sets.

enableStyleSheetsForSet() →

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