{{ApiRef("DOM")}}{{SeeCompatTable}}
The Range.getBoundingClientRect()
method returns a {{ domxref("ClientRect") }} object that bounds the contents of the range; this a rectangle enclosing the union of the bounding rectangles for all the elements in the range.
This is method is useful for determining the viewport coordinates of the cursor or selection inside a text box. See {{domxref("Element.getBoundingClientRect()")}} for the details on the returned value.
Syntax
boundingRect = range.getBoundingClientRect()
Example
range = document.createRange(); range.selectNode(document.getElementsByTagName("div").item(0)); rect = range.getBoundingClientRect();
Specification
Specification | Status | Comment |
---|---|---|
{{SpecName('CSSOM View', '#dom-range-getboundingclientrect', 'Range.getBoundingClientRect()')}} | {{Spec2('CSSOM View')}} | Initial specification. |
Browser compatibility
{{CompatibilityTable}}
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown}} | {{CompatGeckoDesktop("2.0")}} | 9.0 | 15.0 | {{CompatUnknown}} |
Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown}} | {{CompatGeckoMobile("2.0")}} | {{CompatNo}} | 15.0 | {{CompatUnknown}} |
See also
- {{domxref("Range.getClientRects()")}} - finer-grained result for non-rectangular ranges (e.g. when the selection wraps onto the next line);
- {{domxref("Element.getBoundingClientRect()")}}
- {{domxref("Document.caretPositionFromPoint()")}} - to get the (node, offset) from viewport coordinates.