[javascript] What is offsetHeight, clientHeight, scrollHeight?

Thought of explaining what is the difference between offsetHeight, clientHeight and scrollHeight or offsetWidth, clientWidth and scrollWidth?

One must know this difference before working on the client side. Otherwise half of their life will be spent in fixing the UI.

Fiddle, or inline below:

_x000D_
_x000D_
function whatis(propType) {_x000D_
  var mainDiv = document.getElementById("MainDIV");_x000D_
  if (window.sampleDiv == null) {_x000D_
    var div = document.createElement("div");_x000D_
    window.sampleDiv = div;_x000D_
  }_x000D_
  div = window.sampleDiv;_x000D_
  var propTypeWidth = propType.toLowerCase() + "Width";_x000D_
  var propTypeHeight = propType + "Height";_x000D_
_x000D_
  var computedStyle = window.getComputedStyle(mainDiv, null);_x000D_
  var borderLeftWidth = computedStyle.getPropertyValue("border-left-width");_x000D_
  var borderTopWidth = computedStyle.getPropertyValue("border-top-width");_x000D_
_x000D_
  div.style.position = "absolute";_x000D_
  div.style.left = mainDiv.offsetLeft + Math.round(parseFloat((propType == "client") ? borderLeftWidth : 0)) + "px";_x000D_
  div.style.top = mainDiv.offsetTop + Math.round(parseFloat((propType == "client") ? borderTopWidth : 0)) + "px";_x000D_
  div.style.height = mainDiv[propTypeHeight] + "px";_x000D_
  div.style.lineHeight = mainDiv[propTypeHeight] + "px";_x000D_
  div.style.width = mainDiv[propTypeWidth] + "px";_x000D_
  div.style.textAlign = "center";_x000D_
  div.innerHTML = propTypeWidth + " X " + propTypeHeight + "( " +_x000D_
    mainDiv[propTypeWidth] + " x " + mainDiv[propTypeHeight] + " )";_x000D_
_x000D_
_x000D_
_x000D_
  div.style.background = "rgba(0,0,255,0.5)";_x000D_
  document.body.appendChild(div);_x000D_
_x000D_
}_x000D_
document.getElementById("offset").onclick = function() {_x000D_
  whatis('offset');_x000D_
}_x000D_
document.getElementById("client").onclick = function() {_x000D_
  whatis('client');_x000D_
}_x000D_
document.getElementById("scroll").onclick = function() {_x000D_
  whatis('scroll');_x000D_
}
_x000D_
#MainDIV {_x000D_
  border: 5px solid red;_x000D_
}
_x000D_
<button id="offset">offsetHeight & offsetWidth</button>_x000D_
<button id="client">clientHeight & clientWidth</button>_x000D_
<button id="scroll">scrollHeight & scrollWidth</button>_x000D_
_x000D_
<div id="MainDIV" style="margin:auto; height:200px; width:400px; overflow:auto;">_x000D_
  <div style="height:400px; width:500px; overflow:hidden;">_x000D_
_x000D_
  </div>_x000D_
</div>
_x000D_
_x000D_
_x000D_

This question is related to javascript html dom offsetheight

The answer is


My descriptions for the three:

  • offsetHeight: How much of the parent's "relative positioning" space is taken up by the element. (ie. it ignores the element's position: absolute descendents)
  • clientHeight: Same as offset-height, except it excludes the element's own border, margin, and the height of its horizontal scroll-bar (if it has one).
  • scrollHeight: How much space is needed to see all of the element's content/descendents (including position: absolute ones) without scrolling.

Then there is also:


* offsetHeight is a measurement in pixels of the element's CSS height, including border, padding and the element's horizontal scrollbar.

* clientHeight property returns the viewable height of an element in pixels, including padding, but not the border, scrollbar or margin.

* scrollHeight value is equal to the minimum height the element would require in order to fit all the content in the viewport without using a vertical scrollbar. The height is measured in the same way as clientHeight: it includes the element's padding, but not its border, margin or horizontal scrollbar.

Same is the case for all of these with width instead of height.


Offset Means "the amount or distance by which something is out of line". Margin or Borders are something which makes the actual height or width of an HTML element "out of line". It will help you to remember that :

  • offsetHeight is a measurement in pixels of the element's CSS height, including border, padding and the element's horizontal scrollbar.

On the other hand, clientHeight is something which is you can say kind of the opposite of OffsetHeight. It doesn't include the border or margins. It does include the padding because it is something that resides inside of the HTML container, so it doesn't count as extra measurements like margin or border. So :

  • clientHeight property returns the viewable height of an element in pixels, including padding, but not the border, scrollbar or margin.

ScrollHeight is all the scrollable area, so your scroll will never run over your margin or border, so that's why scrollHeight doesn't include margin or borders but yeah padding does. So:

  • scrollHeight value is equal to the minimum height the element would require in order to fit all the content in the viewport without using a vertical scrollbar. The height is measured in the same way as clientHeight: it includes the element's padding, but not its border, margin or horizontal scrollbar.

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment

Examples related to dom

How do you set the document title in React? How to find if element with specific id exists or not Cannot read property 'style' of undefined -- Uncaught Type Error adding text to an existing text element in javascript via DOM Violation Long running JavaScript task took xx ms How to get `DOM Element` in Angular 2? Angular2, what is the correct way to disable an anchor element? React.js: How to append a component on click? Detect click outside React component DOM element to corresponding vue.js component

Examples related to offsetheight

What is offsetHeight, clientHeight, scrollHeight?