There is a great article on MDN that explains the underlying theory of these concepts: https://developer.mozilla.org/en-US/docs/Web/API/CSS_Object_Model/Determining_the_dimensions_of_elements.
It also explains the important conceptual difference between the width/height of boundingClientRect and offsetWidth/offsetHeight.
To verify whether this theory is correct, you need some tests. This is what I did here: https://github.com/lingtalfi/dimensions-cheatsheet.
It tests Chrome 53, Firefox 49, Safari 9, Edge 13, and Internet Explorer 11.
The test results confirm that the theory is generally correct. For the tests, I created 3 divs, each containing 10 lorem ipsum paragraphs. Some CSS is applied to them:
.div1{
width: 500px;
height: 300px;
padding: 10px;
border: 5px solid black;
overflow: auto;
}
.div2{
width: 500px;
height: 300px;
padding: 10px;
border: 5px solid black;
box-sizing: border-box;
overflow: auto;
}
.div3{
width: 500px;
height: 300px;
padding: 10px;
border: 5px solid black;
overflow: auto;
transform: scale(0.5);
}
The results are as follows:
-
Section 1
offsetWidth: 530 (Chrome 53, Firefox 49, Safari 9, Edge 13, IE 11)
offsetHeight: 330 (Chrome 53, Firefox 49, Safari 9, Edge 13, IE 11)
boundingClientRect width: 530 (Chrome 53, Firefox 49, Safari 9, Edge 13, IE 11)
boundingClientRect height: 330 (Chrome 53, Firefox 49, Safari 9, Edge 13, IE 11)
clientWidth: 505 (Chrome 53, Firefox 49, Safari 9)
clientWidth: 508 (Edge 13)
clientWidth: 503 (IE 11)
clientHeight: 320 (Chrome 53, Firefox 49, Safari 9, Edge 13, IE 11)
scrollWidth: 505 (Chrome 53, Safari 9, Firefox 49)
scrollWidth: 508 (Edge 13)
scrollWidth: 503 (IE 11)
scrollHeight: 916 (Chrome 53, Safari 9)
scrollHeight: 954 (Firefox 49)
scrollHeight: 922 (Edge 13, IE 11)
-
Section 2
offsetWidth: 500 (Chrome 53, Firefox 49, Safari 9, Edge 13, IE 11)
offsetHeight: 300 (Chrome 53, Firefox 49, Safari 9, Edge 13, IE 11)
boundingClientRect width: 500 (Chrome 53, Firefox 49, Safari 9, Edge 13, IE 11)
boundingClientRect height: 300 (Chrome 53, Firefox 49, Safari 9)
boundingClientRect height: 299.9999694824219 (Edge 13, IE 11)
clientWidth: 475 (Chrome 53, Firefox 49, Safari 9)
clientWidth: 478 (Edge 13)
clientWidth: 473 (IE 11)
clientHeight: 290 (Chrome 53, Firefox 49, Safari 9, Edge 13, IE 11)
scrollWidth: 475 (Chrome 53, Safari 9, Firefox 49)
scrollWidth: 478 (Edge 13)
scrollWidth: 473 (IE 11)
scrollHeight: 916 (Chrome 53, Safari 9)
scrollHeight: 954 (Firefox 49)
scrollHeight: 922 (Edge 13, IE 11)
-
Section 3
offsetWidth: 530 (Chrome 53, Firefox 49, Safari 9, Edge 13, IE 11)
offsetHeight: 330 (Chrome 53, Firefox 49, Safari 9, Edge 13, IE 11)
boundingClientRect width: 265 (Chrome 53, Firefox 49, Safari 9, Edge 13, IE 11)
boundingClientRect height: 165 (Chrome 53, Firefox 49, Safari 9, Edge 13, IE 11)
clientWidth: 505 (Chrome 53, Firefox 49, Safari 9)
clientWidth: 508 (Edge 13)
clientWidth: 503 (IE 11)
clientHeight: 320 (Chrome 53, Firefox 49, Safari 9, Edge 13, IE 11)
scrollWidth: 505 (Chrome 53, Safari 9, Firefox 49)
scrollWidth: 508 (Edge 13)
scrollWidth: 503 (IE 11)
scrollHeight: 916 (Chrome 53, Safari 9)
scrollHeight: 954 (Firefox 49)
scrollHeight: 922 (Edge 13, IE 11)
Therefore, except for the boundingClientRect height value in Edge 13 and IE 11 (299.9999694824219 instead of the expected 300), the results confirm that the underlying theory is valid.
From there, here is my definition of these concepts:
offsetWidth/offsetHeight: The dimensions of the layout border box
boundingClientRect: The dimensions of the rendered border box
clientWidth/clientHeight: The dimensions of the visible part of the layout padding box (excluding scrollbars)
scrollWidth/scrollHeight: The dimensions of the layout content box (if not constrained by scrollbars)
Note: The default vertical scrollbar width is 12px in Edge 13, 15px in Chrome 53, Firefox 49, and Safari 9, and 17px in Internet Explorer 11 (measured via screenshots in Photoshop and confirmed by test results).
However, in some cases, your application may not use the default vertical scrollbar width.
Therefore, considering the definitions of these concepts, the vertical scrollbar width should equal (pseudo-code):
- Layout size:
offsetWidth - clientWidth - (borderLeftWidth + borderRightWidth)
- Render size:
boundingClientRect.width - clientWidth - (borderLeftWidth + borderRightWidth)
Note that if you are not familiar with layout and render, please read the MDN article.
Additionally, if you have other browsers (or if you want to view the test results yourself), you can see my test page here: http://codepen.io/lingtalfi/pen/BLdBdL