document.elementFromPoint() and document.elementsFromPoint()
The elementFromPoint() method, available on the Document object, returns the topmost Element at the specified coordinates (relative to the viewport).
If the element at the specified point belongs to another document (for example, the document of an <iframe>), that document's parent element is returned (the <iframe> itself). If the element at the given point is anonymous or XBL generated content, such as a textbox's scroll bars, then the first non-anonymous ancestor element (for example, the textbox) is returned.
Elements with pointer-events set to none will be ignored, and the element below it will be returned.
If the method is run on another document (like an <iframe>'s subdocument), the coordinates are relative to the document where the method is being called.
If the specified point is outside the visible bounds of the document or either coordinate is negative, the result is null.
If you need to find the specific position inside the element, use Document.caretPositionFromPoint().
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 1 | 12 | 3 | 4 | 18 | 3.2 | |
| The elementsFromPoint() method of the Document interface returns an array of all elements at the specified coordinates (relative to the viewport). The elements are ordered from the topmost to the bottommost box of the viewport. | 43 | 79 | 46 | 11.1 | 43 | 11.3 |
- Before Chrome 66, this method returned `null` when the element was a child of a host node. See bug 40537452.
- Before Chrome Android 66, this method returned `null` when the element was a child of a host node. See bug 40537452.
- Before Chrome 66, this method returned `null` when the element was a child of a host node. See bug 40537452.
- This feature was removed in a later browser version (79)
- Available with a vendor prefix: ms (12)
- Returns a `NodeList` instead of an array. See the MSDN documentation. Returns `null` when the point provided has no elements beneath it (e.g., when given a point outside the document).
- Before Chrome Android 66, this method returned `null` when the element was a child of a host node. See bug 40537452.
Syntax
elementFromPoint(x, y) Use cases
-
Using document.elementFromPoint() and document.elementsFromPoint()
The elementFromPoint()
Cautions
- May not be supported in older browsers.
Implementation notes
- Some APIs require a secure context (HTTPS) or user activation. Check the requirements before use.