interface

CaretPosition — CSS reference

Describes the node and offset at a caret location obtained from viewport coordinates. A null result is possible when no caret position is available.

Formal Syntax & Structure

css syntaxcss
document.caretPositionFromPoint(x, y)

Working Example

Example Snippetcss
<!doctype html><html lang="en"><meta charset="utf-8"><title>CaretPosition</title><style>body{margin:0;font:16px system-ui}#a{box-sizing:content-box;width:120px;height:60px;padding:8px;border:3px solid #475569;overflow:auto}#output{white-space:pre-wrap}
</style><body><div id="a">Example</div><pre id="output" aria-live="polite"></pre><script>const a=document.getElementById("a"); window.demoResult=(async()=>{const r=a.firstChild;const range=new Range();range.setStart(r,0);range.setEnd(r,1);const b=range.getBoundingClientRect();const c=document.caretPositionFromPoint(b.left+1,b.top+b.height/2);return [c instanceof CaretPosition,c.offsetNode===r,c.offset>=0];})();window.demoResult.then(v=>document.getElementById("output").textContent=JSON.stringify(v),e=>document.getElementById("output").textContent=String(e));</script></body></html>
CSS Reference