var div = document.getElementById('foo');
console.log(div);
console.log(div.scrollHeight);
When I click on the DOM of div returned by the first log in the console, its scrollHeight is x, while the second log prints out y; x != y.
The HTMLElement. offsetHeight read-only property returns the height of an element, including vertical padding and borders, as an integer. Typically, offsetHeight is a measurement in pixels of the element's CSS height, including any borders, padding, and horizontal scrollbars (if rendered).
The clientHeight property returns the viewable height of an element in pixels, including padding, but not the border, scrollbar or margin. The clientHeight property is read-only.
clientHeight can be calculated as: CSS height + CSS padding - height of horizontal scrollbar (if present). When clientHeight is used on the root element (the <html> element), (or on <body> if the document is in quirks mode), the viewport's height (excluding any scrollbar) is returned.
To get height of a div try-
1. JavaScript:
var clientHeight = document.getElementById('divId').clientHeight;
or
var offsetHeight = document.getElementById('divId').offsetHeight;
2. jQuery:
var height= $("#divId").height();
3. vanilla JS:
var clientHeight = document.querySelector('#divId').clientHeight;
or
var offsetHeight = document.querySelector('#divId').offsetHeight;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With