Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Element height and scroll position: offsetHeight, clientHeight, and scrollTop

Distinguish the height of an element’s outer box, the height of its inner viewing area, and its current vertical scroll position. Learn to detect the bottom without relying on exact numerical equality.

Version: ECMAScript 2024 and the current web platform; runtime support varies

Syntax

element.offsetHeight       // HTMLElement, read-only
 element.clientHeight       // Element, read-only
 element.scrollTop          // Element, read
 element.scrollTop = y      // Element, write a vertical offset in CSS pixels

These are browser DOM properties, not part of ECMAScript. offsetHeight belongs to HTMLElement; clientHeight and scrollTop belong to Element and are inherited by HTML elements. Access them on an element instance, such as el.clientHeight. All these measurements use CSS pixels.

offsetHeight is a read-only integer layout height. It normally includes padding, borders, and any rendered horizontal scrollbar, but not margins. It ignores CSS transforms; use el.getBoundingClientRect().height if you need the transformed bounding rectangle’s height.

clientHeight is a read-only integer inner height. It includes padding but excludes borders, margins, and any horizontal scrollbar. Both height properties return 0 when display: none on the element or an ancestor leaves the element without a layout box. On the document root (body in quirks mode), clientHeight measures the viewport height excluding the horizontal scrollbar.

scrollTop reads or sets the vertical scroll offset from the scroll origin. It is usually 0 at the top, but it can be fractional or negative, for example with flex-direction: column-reverse. Safari’s overscroll bounce can temporarily report values outside the normal range. Assigning el.scrollTop = y can scroll content with overflow: hidden, but not with overflow: clip.

scrollHeight is the integer height of all content, including overflow out of view, plus padding. For a normal downward-scrolling container without overscroll, detect the bottom with Math.abs(el.scrollHeight - el.clientHeight - el.scrollTop) <= 1. Use a one-CSS-pixel tolerance rather than === because scrollTop can be fractional. Do not reuse this test unchanged for reversed layouts.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Element height and scroll position</title>
</head>
<body>
  <script>
    (() => {
      const box = document.createElement('div');
      box.style.cssText = 'position:fixed;top:0;left:0;width:160px;height:100px;' +
        'box-sizing:content-box;padding:0;border:4px solid;overflow:hidden;' +
        'scroll-behavior:auto;writing-mode:horizontal-tb;';
      const content = document.createElement('div');
      content.style.height = '300px';
      box.append(content);
      document.body.append(box);

      try {
        box.scrollTop = 40;
        console.log(`offsetHeight=${box.offsetHeight} clientHeight=${box.clientHeight} scrollHeight=${box.scrollHeight} scrollTop=${box.scrollTop}`);

        box.scrollTop = 1000;
        const atBottom = Math.abs(box.scrollHeight - box.clientHeight - box.scrollTop) <= 1;
        console.log(`scrollTop=${box.scrollTop} atBottom=${atBottom}`);
      } finally {
        box.remove();
      }
    })();
  </script>
</body>
</html>

Result

offsetHeight=108 clientHeight=100 scrollHeight=300 scrollTop=40
scrollTop=200 atBottom=true

Save the example as an HTML file, open it in a browser, and check the developer console for the output. The borders add 8 px to `offsetHeight`, but not to `clientHeight`. The request to scroll to 1000 stops at 200, the bottom limit.

Related lessons

  • JavaScript · Lesson 11 · Advanced · Version: 4

Sources