HOWTO · JavaScript

How to Get Height of the Div Element in JavaScript

This tutorial introduces how to get the height of a div element inside JavaScript.

On this page

To get a <div> element’s height in browser JavaScript, select it and read the measurement that matches your need: clientHeight for the inner box, offsetHeight for the box including borders, scrollHeight for content including overflow, or getBoundingClientRect().height for its rendered rectangle. These properties measure different parts of the element, so their values can differ.

The runnable example below models a 50px content box with 4px padding and a 1px border. It runs in Node without a browser DOM and prints the corresponding measurements. In a browser, read these same properties from the selected DOM element.

const element = {
  contentHeight: 50,
  padding: 4,
  border: 1,
  get clientHeight() {
    return this.contentHeight + 2 * this.padding;
  },
  get offsetHeight() {
    return this.clientHeight + 2 * this.border;
  },
  get scrollHeight() {
    return this.clientHeight;
  },
  getBoundingClientRect() {
    return { height: this.offsetHeight };
  }
};

console.log('clientHeight:', element.clientHeight);
console.log('offsetHeight:', element.offsetHeight);
console.log('scrollHeight:', element.scrollHeight);
console.log('bounding height:', element.getBoundingClientRect().height);
clientHeight: 58
offsetHeight: 60
scrollHeight: 58
bounding height: 60

get div element height in JavaScript

Measure a div’s height in JavaScript

Use the DOM to select an existing element by its ID, then choose one of the measurements below. The Node example above models the readings for the stated box; when measuring your page, replace element with the result of document.getElementById('container') or another selector. Read its dimensions after the element is in the document and its layout is available.

Choose the measurement by the box you need: use clientHeight for the inner box, offsetHeight when borders belong in the result, scrollHeight when you need the full content height for scrolling, and getBoundingClientRect().height when the rendered rectangle matters. None includes margins; measure the relevant layout gap separately if spacing is part of your calculation. These values are in CSS pixels, not physical screen pixels.

clientHeight

clientHeight is the inner height in CSS pixels: it includes top and bottom padding, but excludes borders and margins. For the sample’s 50px content and 4px padding on each side, the value is 58. The property returns an integer.

offsetHeight

offsetHeight includes the element’s content, padding, borders, and any horizontal scrollbar. It excludes margins. In the sample, that is 50 + 8 + 2 = 60 pixels. This property also returns an integer.

scrollHeight

scrollHeight measures the content’s full height, including the portion extending beyond the visible area when content overflows. It includes padding but excludes borders and margins. It is 58 for the sample because its content does not overflow; on a page with overflowing content, it can be greater than clientHeight. It is an integer measurement, and it should not be assumed to equal clientHeight in every layout.

getBoundingClientRect()

getBoundingClientRect() returns a rectangle describing the element’s rendered size and position relative to the viewport. Read its height property for the rendered height. It includes padding and borders, so the sample measures 60 pixels. Unlike the integer dimension properties above, this height can be fractional, for example when layout or transforms produce subpixel dimensions. Use this method when you need the rendered rectangle rather than the content’s scrollable height.