Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1014705 of Range.getBoundingClientRect()

  • Revision slug: Web/API/Range/getBoundingClientRect
  • Revision title: Range.getBoundingClientRect()
  • Revision id: 1014705
  • Created:
  • Creator: Nickolay
  • Is current revision? Yes
  • Comment explain how this is useful, link to related methods.

Revision Content

{{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.

Revision Source

<p>{{ApiRef("DOM")}}{{SeeCompatTable}}</p>

<p>The <strong><code>Range.getBoundingClientRect()</code></strong> method returns a {{ domxref("ClientRect") }}&nbsp;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.</p>

<p>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.</p>

<h2 id="Syntax" name="Syntax">Syntax</h2>

<pre class="syntaxbox">
<em>boundingRect</em> = <em>range</em>.getBoundingClientRect()
</pre>

<h2 id="Example" name="Example">Example</h2>

<pre class="brush: js">
range = document.createRange();
range.selectNode(document.getElementsByTagName("div").item(0));
rect = range.getBoundingClientRect();
</pre>

<h2 id="Specification" name="Specification">Specification</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('CSSOM View', '#dom-range-getboundingclientrect', 'Range.getBoundingClientRect()')}}</td>
   <td>{{Spec2('CSSOM View')}}</td>
   <td>Initial specification.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoDesktop("2.0")}}</td>
   <td>9.0</td>
   <td>15.0</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoMobile("2.0")}}</td>
   <td>{{CompatNo}}</td>
   <td>15.0</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{domxref("Range.getClientRects()")}} - finer-grained result for non-rectangular ranges (e.g. when the selection wraps onto the next line);</li>
 <li>{{domxref("Element.getBoundingClientRect()")}}</li>
 <li>{{domxref("Document.caretPositionFromPoint()")}} - to get the (node, offset) from viewport coordinates.</li>
</ul>
Revert to this revision