Element Size / Scrolling in the DOM
Browser Document: Element Size / Scrolling
Why should you consider the scrollbar when assessing content width?
View Answer:
The width may vary between devices and browsers.
Can you explain what the offsetParent is concerning an HTML element?
View Answer:
<main style="position: relative" id="main">
<article>
<div id="example" style="position: absolute; left: 180px; top: 180px">
...
</div>
</article>
</main>
<script>
console.log(example.offsetParent.id); // main
console.log(example.offsetLeft); // 180 (note: a number, not a string "180px")
console.log(example.offsetTop); // 180
</script>
Can you explain what the offsetWidth is about in an HTML element?
View Answer:
Syntax: let intElemOffsetWidth = element.offsetWidth;
Can you explain what the offsetHeight is concerning an HTML element?
View Answer:
Syntax: let intElemOffsetHeight = element.offsetHeight;
How do geometry properties work on elements when they are zero or null?
View Answer:
function isHidden(elem) {
return !elem.offsetWidth && !elem.offsetHeight;
}
Can you explain what the clientTop is concerning an HTML element?
View Answer:
Syntax: let top = element.clientTop;
Can you explain what the clientLeft is concerning an HTML element?
View Answer:
Syntax: let left = element.clientLeft;
These are not border widths and heights, but relative coordinates of the inner and outer sides. This behavior is apparent when the scrollbar is on the left, and RTL (right to left) languages, such as Hebrew or Arabic. The scrollbar gets pushed to the left as part of their specification.
Can you explain what the clientWidth is concerning an HTML element?
View Answer:
Syntax: let intElemClientWidth = element.clientWidth;
Can you explain what the clientHeight is concerning an HTML element?
View Answer:
Syntax: let intElemClientHeight = element.clientHeight;
Can you explain what the scrollWidth is concerning an HTML element?
View Answer:
Syntax: let xScrollWidth = element.scrollWidth
The width gets measured the same way as clientWidth: it includes the element's padding but not its border, margin, or vertical scrollbar (if present). It can also include the width of pseudo-elements such as ::before or ::after. If the element's content can fit without a need for a horizontal scrollbar, its scrollWidth is equal to clientWidth.
Can you explain what the scrollLeft is concerning an HTML element?
View Answer:
Syntax: let sLeft = element.scrollLeft;
Can you explain what the scrollTop is concerning an HTML element?
View Answer:
Syntax: let intElemScrollTop = someElement.scrollTop;
Why should we use geometry properties versus the width/height from CSS using getComputedStyle?
View Answer:
The described difference is only about reading getComputedStyle(...) width from JavaScript. Visually, everything is correct.