Element.scrollHeight

  • Revision slug: DOM/element.scrollHeight
  • Revision title: element.scrollHeight
  • Revision id: 347883
  • Created:
  • Creator: fusionchess
  • Is current revision? No
  • Comment

Revision Content

{{ DomRef() }}

This property will round the value to an integer. If you need a fractional value, use {{ domxref("element.getBoundingClientRect()") }}.

scrollHeight

Height of the scroll view of an element; it includes the element padding but not its margin.

Syntax and values

var intElemScrollHeight = document.getElementById(id_attribute_value).scrollHeight;

intElemScrollHeight is a variable storing an integer corresponding to the scrollHeight pixel value of the element. scrollHeight is a read-only property.

Description

An element's scrollHeight is a measurement of the height of an element's content including content not visible on the screen due to overflow.

If the element's content generated a vertical scrollbar, the scrollHeight value is equal to the minimum clientHeight the element would require in order to fit all the content in the viewpoint without using a vertical scrollbar. When an element's content does not generate a vertical scrollbar, then its scrollHeight property is equal to its clientHeight property. This can mean either the content is too short to require a scrollbar or that the the element has CSS style overflow value of visible (non-scrollable).

Example

padding-top

Gentle, individualistic and very loyal, Birman cats fall between Siamese and Persian in character. If you admire cats that are non aggressive, that enjoy being with humans and tend to be on the quiet side, you may well find that Birman cats are just the felines for you.

Image:BirmanCat.jpgAll Birmans have colorpointed features, dark coloration of the face, ears, legs and tail.

Cat image and text coming from www.best-cat-art.com

padding-bottom

LeftTopRightBottommargin-topmargin-bottomborder-topborder-bottom{{ mediawiki.external('if IE') }}><span id="MrgLeft" style="position: _fckstyle="position: _fckstyle="position: absolute; left: 8px; top: 65px; font: bold 13px Arial, sans-serif !important; writing-mode: tb-rl;">margin-left</span><span id="BrdLeft" style="position: absolute; left: 33px; top: 65px; color: white; font: bold 13px Arial, sans-serif !important; writing-mode: tb-rl;">border-left</span><span id="PdgLeft" style="position: absolute; left: 55px; top: 65px; font: bold 13px Arial, sans-serif !important; writing-mode: tb-rl;">padding-left</span><span id="PdgRight" style="position: absolute; left: 275px; top: 60px; color: black; font: bold 13px Arial, sans-serif !important; writing-mode: tb-rl; white-space: nowrap;">padding-right</span><span id="BrdRight" style="position: absolute; left: 310px; top: 65px; color: white; font: bold 13px Arial, sans-serif !important; writing-mode: tb-rl;">border-right</span><span id="MrgRight" style="position: absolute; left: 340px; top: 65px; font: bold 13px Arial, sans-serif !important; writing-mode: tb-rl;">margin-right</span><!{{ mediawiki.external('endif') }}

Image:scrollHeight.png

Solutions

Determine if an element has been totally scrolled

The following equivalence returns true if an element is at the end of its scroll, false if it isn't.

element.scrollHeight - element.scrollTop === element.clientHeight

Associated with the onscroll event, this equivalence could be useful to determine whether a user has read a text or not (see also element.scrollTop and element.clientHeight). See the following example:

<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>MDN Example</title>
<script type="text/javascript">
function checkReading () {
  if (checkReading.read) { return; }
  checkReading.read = this.scrollHeight - this.scrollTop === this.clientHeight;
  document.registration.accept.disabled = document.getElementById("nextstep").disabled = !checkReading.read;
  checkReading.noticeBox.innerHTML = checkReading.read ?
    "Thank you." :
    "Please, scroll and read the following text.";
}

onload = function () {
  var oToBeRead = document.getElementById("rules");
  checkReading.noticeBox = document.createElement("span");
  document.registration.accept.checked = false;
  checkReading.noticeBox.id = "notice";
  oToBeRead.parentNode.insertBefore(checkReading.noticeBox, oToBeRead);
  oToBeRead.parentNode.insertBefore(document.createElement("br"), oToBeRead);
  oToBeRead.onscroll = checkReading;
  checkReading.call(oToBeRead);
}

</script>
<style type="text/css">

#notice {
  display: inline-block;
  margin-bottom: 12px;
  border-radius: 5px;
  width: 600px;
  padding: 5px;
  border: 2px #7FDF55 solid;
}

#rules {
  width: 600px;
  height: 130px;
  padding: 5px;
  border: #2A9F00 solid 2px;
  border-radius: 5px;
}
</style>
</head>
 
<body>

  <form name="registration">

    <p>
      <textarea id="rules">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum at laoreet magna. Aliquam erat volutpat. Praesent molestie, dolor ut eleifend aliquam, mi ligula ultrices sapien, quis cursus neque dui nec risus. Duis tincidunt lobortis purus eu aliquet. Quisque in dignissim magna. Aenean ac lorem at velit ultrices consequat. Nulla luctus nisi ut libero cursus ultrices. Pellentesque nec dignissim enim.
 
Phasellus ut quam lacus, sed ultricies diam. Vestibulum convallis rutrum dolor, sit amet egestas velit scelerisque id. Proin non dignissim nisl. Sed mi odio, ullamcorper eget mattis id, malesuada vitae libero. Integer dolor lorem, mattis sed dapibus a, faucibus id metus. Duis iaculis dictum pulvinar. In nisi nibh, dapibus ac blandit at, porta at arcu.
 
Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Praesent dictum ipsum aliquet erat eleifend sit amet sollicitudin felis tempus. Aliquam congue cursus venenatis. Maecenas luctus pellentesque placerat. Mauris nisl odio, condimentum sed fringilla a, consectetur id ligula. Praesent sem sem, aliquet non faucibus vitae, iaculis nec elit. Nullam volutpat, lectus et blandit bibendum, nulla lorem congue turpis, ac pretium tortor sem ut nibh. Donec vel mi in ligula hendrerit sagittis. Donec faucibus viverra fermentum. Fusce in arcu arcu. Nullam at dignissim massa. Cras nibh est, pretium sit amet faucibus eget, sollicitudin in ligula. Vivamus vitae urna mauris, eget euismod nunc.
 
Aenean semper gravida enim non feugiat. In hac habitasse platea dictumst. Cras eleifend nisl volutpat ante condimentum convallis. Donec varius dolor malesuada erat consequat congue. Donec eu lacus ut sapien venenatis tincidunt. Quisque sit amet tellus et enim bibendum varius et a orci. Donec aliquet volutpat scelerisque.
 
Proin et tortor dolor. Ut aliquet, dolor a mattis sodales, odio diam pulvinar sem, egestas pretium magna eros vitae felis. Nam vitae magna lectus, et ornare elit. Morbi feugiat, ipsum ac mattis congue, quam neque mollis tortor, nec mollis nisl dolor a tortor. Maecenas varius est sit amet elit interdum quis placerat metus posuere. Duis malesuada justo a diam vestibulum vel aliquam nisi ornare. Integer laoreet nisi a odio ornare non congue turpis eleifend. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Cras vulputate libero sed arcu iaculis nec lobortis orci fermentum.</textarea>
    </p>

    <p>
      <input type="checkbox" name="accept" id="agree" />
      <label for="agree">I agree</label>
      <input type="submit" id="nextstep" value="Next" />
    </p>

  </form>

</body>
</html>

View this example in action

Specification

scrollHeight is part of the MSIE's DHTML object model. scrollHeight is not part of any W3C specification or technical recommendation.

Browser compatibility

Browser Lowest version
Internet Explorer 8.0
Firefox (Gecko) 3.0 (1.9)
Opera ?
Safari | Chrome | WebKit 4.0 | 4.0 | ?

Notes

scrollHeight is a property of the DHTML object model which was first introduced by MSIE. It is referred to as the height of an element's physical scrolling view.

References

  • {{ MSDN("ms534615", "scrollHeight Property") }}
  • {{ MSDN("ms530302", "Measuring Element Dimension and Location") }}

See Also

{{ languages( { "fr": "fr/DOM/element.scrollHeight", "pl": "pl/DOM/element.scrollHeight" } ) }}

Revision Source

<p>{{ DomRef() }}</p>
<div class="note">
  <p>This property will round the value to an integer. If you need a fractional value, use {{ domxref("element.getBoundingClientRect()") }}.</p>
</div>
<h3 id="scrollHeight" name="scrollHeight">scrollHeight</h3>
<p>Height of the scroll view of an element; it includes the element padding but <strong>not</strong> its margin.</p>
<h3 id="Syntax_and_values" name="Syntax_and_values">Syntax and values</h3>
<pre class="eval">
var <em>intElemScrollHeight</em> = document.getElementById(<em>id_attribute_value</em>).scrollHeight;
</pre>
<p><em>intElemScrollHeight</em> is a variable storing an integer corresponding to the scrollHeight pixel value of the element. scrollHeight is a read-only property.</p>
<h3 id="Description" name="Description">Description</h3>
<p>An element's <strong>scrollHeight</strong> is a measurement of the height of an element's content including content not visible on the screen due to overflow.</p>
<p>If the element's content generated a vertical scrollbar, the <code>scrollHeight</code> value is equal to the minimum <code>clientHeight</code> the element would require in order to fit all the content in the viewpoint without using a vertical scrollbar. When an element's content does not generate a vertical scrollbar, then its <code>scrollHeight</code> property is equal to its <code>clientHeight</code> property. This can mean either the content is too short to require a scrollbar or that the the element has CSS style overflow value of visible (non-scrollable).</p>
<h3 id="Example" name="Example">Example</h3>
<div id="offsetContainer" style="margin: 26px 0px; background-color: rgb(255, 255, 204); border: 4px dashed black; color: black; position: absolute; left: 260px;">
  <div id="idDiv" style="margin: 24px 29px; border: 24px black solid; padding: 0px 28px; width: 199px; height: 102px; overflow: auto; background-color: white; font-size: 13px!important; font-family: Arial, sans-serif;">
    <p id="PaddingTopLabel" style="text-align: center; font-style: italic; font-weight: bold; font-size: 13px!important; font-family: Arial, sans-serif; margin: 0px;">padding-top</p>
    <p>Gentle, individualistic and very loyal, Birman cats fall between Siamese and Persian in character. If you admire cats that are non aggressive, that enjoy being with humans and tend to be on the quiet side, you may well find that Birman cats are just the felines for you.</p>
    <p><span style="float: right;"><img alt="Image:BirmanCat.jpg" class="internal" src="/@api/deki/files/44/=BirmanCat.jpg" /></span>All Birmans have colorpointed features, dark coloration of the face, ears, legs and tail.</p>
    <p>Cat image and text coming from <a class="external" href="http://www.best-cat-art.com/">www.best-cat-art.com</a></p>
    <p id="PaddingBottomLabel" style="text-align: center; font-style: italic; font-weight: bold; font-size: 13px!important; font-family: Arial, sans-serif; margin: 0px;">padding-bottom</p>
  </div>
  <span style="position: absolute; left: -32px; top: 85px; color: blue; font-weight: bold; font-size: 13px!important; font-family: Arial, sans-serif;">Left</span><span style="position: absolute; left: 170px; top: -24px; color: blue; font-weight: bold; font-size: 13px!important; font-family: Arial, sans-serif;">Top</span><span style="position: absolute; left: 370px; top: 85px; color: blue; font-weight: bold; font-size: 13px!important; font-family: Arial, sans-serif;">Right</span><span style="position: absolute; left: 164px; top: 203px; color: blue; font-weight: bold; font-size: 13px!important; font-family: Arial, sans-serif;">Bottom</span><span style="position: absolute; left: 143px; top: 5px; font-style: italic; font-weight: bold; font-size: 13px!important; font-family: Arial, sans-serif;">margin-top</span><span style="position: absolute; left: 138px; top: 175px; font-style: italic; font-weight: bold; font-size: 13px!important; font-family: Arial, sans-serif;">margin-bottom</span><span style="position: absolute; left: 143px; top: 27px; color: white; font-style: italic; font-weight: bold; font-size: 13px!important; font-family: Arial, sans-serif;">border-top</span><span style="position: absolute; left: 138px; top: 153px; color: white; font-style: italic; font-weight: bold; font-size: 13px!important; font-family: Arial, sans-serif;">border-bottom</span><span class="comment">{{ mediawiki.external('if IE') }}&gt;&lt;span id="MrgLeft" style="position: _fckstyle="position: _fckstyle="position: absolute; left: 8px; top: 65px; font: bold 13px Arial, sans-serif&nbsp;!important; writing-mode: tb-rl;"&gt;margin-left&lt;/span&gt;&lt;span id="BrdLeft" style="position: absolute; left: 33px; top: 65px; color: white; font: bold 13px Arial, sans-serif&nbsp;!important; writing-mode: tb-rl;"&gt;border-left&lt;/span&gt;&lt;span id="PdgLeft" style="position: absolute; left: 55px; top: 65px; font: bold 13px Arial, sans-serif&nbsp;!important; writing-mode: tb-rl;"&gt;padding-left&lt;/span&gt;&lt;span id="PdgRight" style="position: absolute; left: 275px; top: 60px; color: black; font: bold 13px Arial, sans-serif&nbsp;!important; writing-mode: tb-rl; white-space: nowrap;"&gt;padding-right&lt;/span&gt;&lt;span id="BrdRight" style="position: absolute; left: 310px; top: 65px; color: white; font: bold 13px Arial, sans-serif&nbsp;!important; writing-mode: tb-rl;"&gt;border-right&lt;/span&gt;&lt;span id="MrgRight" style="position: absolute; left: 340px; top: 65px; font: bold 13px Arial, sans-serif&nbsp;!important; writing-mode: tb-rl;"&gt;margin-right&lt;/span&gt;&lt;!{{ mediawiki.external('endif') }}</span></div>
<p style="margin-top: 270px;"><img alt="Image:scrollHeight.png" class="internal" src="/@api/deki/files/840/=ScrollHeight.png" /></p>
<h3 id="Specification" name="Specification">Solutions</h3>
<h4 id="Determine_if_an_element_has_been_totally_scrolled">Determine if an element has been totally scrolled</h4>
<p>The following equivalence returns <code>true</code> if an element is at the end of its scroll, <code>false</code> if it isn't.</p>
<pre class="syntaxbox">
element.scrollHeight - element.scrollTop === element.clientHeight</pre>
<p><code><code><code><code>Associated with the <a href="/en-US/docs/DOM/element.onscroll" title="en-US/docs/DOM/element.onscroll"><code>onscroll</code></a><code><code> event, this equivalence could be useful to determine whether a user has read a text or not </code></code></code></code></code></code>(see also <a href="/en-US/docs/DOM/element.scrollTop" title="en-US/docs/DOM/element.scrollTop"><code>element.scrollTop</code></a><code><code> and <a href="/en-US/docs/DOM/element.clientHeight" title="en-US/docs/DOM/element.clientHeight"><code>element.clientHeight</code></a><code><code>)</code></code></code></code><code><code><code><code><code><code>. See the following example:</code></code></code></code></code></code></p>
<div style="height: 500px; margin-bottom: 12px; overflow: auto;">
  <pre class="brush: html">
<code><code><code><code><code><code>&lt;</code></code></code></code></code></code>!doctype html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
&lt;title&gt;MDN Example&lt;/title&gt;
&lt;script type="text/javascript"&gt;
function checkReading () {
&nbsp; if (checkReading.read) { return; }
&nbsp; checkReading.read = this.scrollHeight - this.scrollTop === this.clientHeight;
&nbsp; document.registration.accept.disabled = document.getElementById("nextstep").disabled = !checkReading.read;
&nbsp; checkReading.noticeBox.innerHTML = checkReading.read ?
&nbsp;&nbsp;&nbsp; "Thank you." :
&nbsp;&nbsp;&nbsp; "Please, scroll and read the following text.";
}

onload = function () {
&nbsp; var oToBeRead = document.getElementById("rules");
&nbsp; checkReading.noticeBox = document.createElement("span");
&nbsp; document.registration.accept.checked = false;
&nbsp; checkReading.noticeBox.id = "notice";
&nbsp; oToBeRead.parentNode.insertBefore(checkReading.noticeBox, oToBeRead);
&nbsp; oToBeRead.parentNode.insertBefore(document.createElement("br"), oToBeRead);
&nbsp; oToBeRead.onscroll = checkReading;
&nbsp; checkReading.call(oToBeRead);
}

&lt;/script&gt;
&lt;style type="text/css"&gt;

#notice {
&nbsp; display: inline-block;
&nbsp; margin-bottom: 12px;
&nbsp; border-radius: 5px;
&nbsp; width: 600px;
&nbsp; padding: 5px;
&nbsp; border: 2px #7FDF55 solid;
}

#rules {
&nbsp; width: 600px;
&nbsp; height: 130px;
&nbsp; padding: 5px;
&nbsp; border: #2A9F00 solid 2px;
&nbsp; border-radius: 5px;
}
&lt;/style&gt;
&lt;/head&gt;
&nbsp;
&lt;body&gt;

&nbsp; &lt;form name="registration"&gt;

&nbsp;&nbsp;&nbsp; &lt;p&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;textarea id="rules"&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum at laoreet magna. Aliquam erat volutpat. Praesent molestie, dolor ut eleifend aliquam, mi ligula ultrices sapien, quis cursus neque dui nec risus. Duis tincidunt lobortis purus eu aliquet. Quisque in dignissim magna. Aenean ac lorem at velit ultrices consequat. Nulla luctus nisi ut libero cursus ultrices. Pellentesque nec dignissim enim.
&nbsp;
Phasellus ut quam lacus, sed ultricies diam. Vestibulum convallis rutrum dolor, sit amet egestas velit scelerisque id. Proin non dignissim nisl. Sed mi odio, ullamcorper eget mattis id, malesuada vitae libero. Integer dolor lorem, mattis sed dapibus a, faucibus id metus. Duis iaculis dictum pulvinar. In nisi nibh, dapibus ac blandit at, porta at arcu.
&nbsp;
Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Praesent dictum ipsum aliquet erat eleifend sit amet sollicitudin felis tempus. Aliquam congue cursus venenatis. Maecenas luctus pellentesque placerat. Mauris nisl odio, condimentum sed fringilla a, consectetur id ligula. Praesent sem sem, aliquet non faucibus vitae, iaculis nec elit. Nullam volutpat, lectus et blandit bibendum, nulla lorem congue turpis, ac pretium tortor sem ut nibh. Donec vel mi in ligula hendrerit sagittis. Donec faucibus viverra fermentum. Fusce in arcu arcu. Nullam at dignissim massa. Cras nibh est, pretium sit amet faucibus eget, sollicitudin in ligula. Vivamus vitae urna mauris, eget euismod nunc.
&nbsp;
Aenean semper gravida enim non feugiat. In hac habitasse platea dictumst. Cras eleifend nisl volutpat ante condimentum convallis. Donec varius dolor malesuada erat consequat congue. Donec eu lacus ut sapien venenatis tincidunt. Quisque sit amet tellus et enim bibendum varius et a orci. Donec aliquet volutpat scelerisque.
&nbsp;
Proin et tortor dolor. Ut aliquet, dolor a mattis sodales, odio diam pulvinar sem, egestas pretium magna eros vitae felis. Nam vitae magna lectus, et ornare elit. Morbi feugiat, ipsum ac mattis congue, quam neque mollis tortor, nec mollis nisl dolor a tortor. Maecenas varius est sit amet elit interdum quis placerat metus posuere. Duis malesuada justo a diam vestibulum vel aliquam nisi ornare. Integer laoreet nisi a odio ornare non congue turpis eleifend. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Cras vulputate libero sed arcu iaculis nec lobortis orci fermentum.&lt;/textarea&gt;
&nbsp;&nbsp;&nbsp; &lt;/p&gt;

&nbsp;&nbsp;&nbsp; &lt;p&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;input type="checkbox" name="accept" id="agree" /&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;label for="agree"&gt;I agree&lt;/label&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;input type="submit" id="nextstep" value="Next" /&gt;
&nbsp;&nbsp;&nbsp; &lt;/p&gt;

&nbsp; &lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;</pre>
</div>
<p><a href="https://developer.mozilla.org/files/4589/readme-example.html" title="readme-example.html">View this example in action</a></p>
<h3 id="Specification" name="Specification"><code><code><code><code><code><code>Specification</code></code></code></code></code></code></h3>
<p><code><code><code><code><code><code><code>scrollHeight</code> is part of the MSIE's <abbr title="Dynamic HyperText Markup Language">DHTML</abbr> object model. <code>scrollHeight</code> is not part of any W3C specification or technical recommendation.</code></code></code></code></code></code></p>
<h3 id="Supported" name="Supported"><code><code><code><code><code><code>Browser compatibility</code></code></code></code></code></code></h3>
<table class="standard-table">
  <tbody>
    <tr>
      <th><code><code><code><code><code><code>Browser</code></code></code></code></code></code></th>
      <th><code><code><code><code><code><code>Lowest version</code></code></code></code></code></code></th>
    </tr>
    <tr>
      <td><code><code><code><code><code><code>Internet Explorer</code></code></code></code></code></code></td>
      <td><code><code><code><code><code><code><strong>8.0</strong></code></code></code></code></code></code></td>
    </tr>
    <tr>
      <td><code><code><code><code><code><code>Firefox (Gecko)</code></code></code></code></code></code></td>
      <td><code><code><code><code><code><code><strong>3.0</strong> (1.9)</code></code></code></code></code></code></td>
    </tr>
    <tr>
      <td><code><code><code><code><code><code>Opera</code></code></code></code></code></code></td>
      <td><code><code><code><code><code><code>?</code></code></code></code></code></code></td>
    </tr>
    <tr>
      <td><code><code><code><code><code><code>Safari | Chrome | WebKit</code></code></code></code></code></code></td>
      <td><code><code><code><code><code><code><strong>4.0</strong> | <strong>4.0</strong> | ?</code></code></code></code></code></code></td>
    </tr>
  </tbody>
</table>
<h3 id="Notes"><code><code><code><code><code><code>Notes</code></code></code></code></code></code></h3>
<p><code><code><code><code><code><code><code>scrollHeight</code> is a property of the <abbr title="Dynamic HyperText Markup Language">DHTML</abbr> object model which was first introduced by MSIE. It is referred to as the height of an element's physical scrolling view.</code></code></code></code></code></code></p>
<h3 id="References" name="References"><code><code><code><code><code><code>References</code></code></code></code></code></code></h3>
<ul>
  <li><code><code><code><code><code><code>{{ MSDN("ms534615", "scrollHeight Property") }}</code></code></code></code></code></code></li>
  <li><code><code><code><code><code><code>{{ MSDN("ms530302", "Measuring Element Dimension and Location") }}</code></code></code></code></code></code></li>
</ul>
<h3 id="See_Also" name="See_Also"><code><code><code><code><code><code>See Also</code></code></code></code></code></code></h3>
<ul>
  <li><code><code><code><code><code><code><a href="/en/DOM/element.clientHeight" title="en/DOM/element.clientHeight">DOM:element.clientHeight</a></code></code></code></code></code></code></li>
  <li><code><code><code><code><code><code><a href="/en/DOM/element.offsetHeight" title="en/DOM/element.offsetHeight">DOM:element.offsetHeight</a></code></code></code></code></code></code></li>
  <li><code><code><code><code><code><code><a href="/en/Determining_the_dimensions_of_elements" title="en/Determining_the_dimensions_of_elements">Determining the dimensions of elements</a></code></code></code></code></code></code></li>
</ul>
<p><code><code><code><code><code><code>{{ languages( { "fr": "fr/DOM/element.scrollHeight", "pl": "pl/DOM/element.scrollHeight" } ) }}</code></code></code></code></code></code></p>
Revert to this revision