document.caretPositionFromPoint() is an instance method that maps viewport coordinates to a text caret position. Learn the CaretPosition return value (offsetNode, offset), the shadowRoots option, when it returns null, MDN’s click-to-split example, and the caretRangeFromPoint fallback.
01
Kind
Instance method
02
Args
x, y, options
03
Returns
CaretPosition
04
Fields
offsetNode
05
Shadow
shadowRoots
06
Status
Baseline 2025
Fundamentals
Introduction
When a user clicks on text, you often need to know which character they clicked nearest to—not just which element. Rich text editors, custom selection tools, and inline formatting all depend on mapping pointer coordinates to a DOM text offset.
MDN: Document.caretPositionFromPoint(x, y) returns a CaretPosition object containing the DOM node, along with the caret and caret’s character offset within that node. Coordinates are viewport-relative (like clientX / clientY from a mouse event).
💡
Beginner tip
Use e.clientX and e.clientY from a click event directly. Always check for null — clicks on empty margins or invalid coordinates may not yield a caret position.
Document.caretPositionFromPoint() is Baseline 2025 on MDN (newly available since December 2025). Logos use the shared browser-image-sprite.png sprite. Older browsers may only support non-standard caretRangeFromPoint().
✓ Baseline 2025
Document.caretPositionFromPoint()
Standard caret-from-point API — use caretRangeFromPoint as fallback where needed.
BaselineNewly available
Google ChromeSupported (recent)
Yes
Mozilla FirefoxSupported (recent)
Yes
Microsoft EdgeSupported (recent)
Yes
Apple SafariCheck updates
Partial
OperaChromium mirror
Yes
Internet ExplorerNot supported
No
caretPositionFromPoint()100% supported
Bottom line: Feature-detect document.caretPositionFromPoint and fall back to document.caretRangeFromPoint on older WebKit-based browsers.
Wrap Up
Conclusion
document.caretPositionFromPoint(x, y) maps viewport coordinates to a CaretPosition with offsetNode and offset. MDN’s examples show click handlers, shadow DOM options, and splitting text at the caret. Always handle null and provide a caretRangeFromPoint fallback when needed.
Feature-detect and fall back to caretRangeFromPoint (MDN)
Pass shadowRoots for shadow DOM components
Verify nodeType === 3 before calling splitText
❌ Don’t
Assume every click returns a caret position
Use page coordinates without adjusting for scroll
Rely only on elementFromPoint when you need text offsets
Forget shadow root disclosure for web components
Skip fallback on browsers without Baseline 2025 support
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about caretPositionFromPoint()
Map clicks to text offsets with the standard CaretPosition API.
5
Core concepts
📝01
Input
x, y
viewport
🗂02
Returns
CaretPosition
MDN
🔗03
Fields
offsetNode
offset
👁04
Null
invalid coords
guard
🛡05
Status
baseline
2025
❓ Frequently Asked Questions
It returns a CaretPosition object (or null) for the text insertion point at viewport coordinates x and y — containing the DOM node and character offset within that node (MDN).
No. MDN marks Document.caretPositionFromPoint() as Baseline 2025 (newly available since December 2025). It is not Deprecated, Experimental, or Non-standard.
A CaretPosition with offsetNode and offset properties, or null if there is no viewport, coordinates are invalid, or no insertion point exists at that point (MDN).
caretPositionFromPoint() is the standard method returning CaretPosition. caretRangeFromPoint() is a non-standard WebKit fallback that returns a Range with startContainer/startOffset (MDN).
An optional array of ShadowRoot objects. When supplied, the method can return a caret position inside those shadow trees. Otherwise positions in undisclosed shadow DOM may remap to the shadow host (MDN).
MDN: when there is no viewport, x/y are negative or outside the viewport, or the point has no valid text insertion indicator.
Did you know?
MDN’s official demo uses caretPositionFromPoint() to split a text node at the clicked offset with splitText(), then inserts a <br> — the same pattern rich-text editors use for “click to insert line break” behavior.