offsetHeight
The offsetHeight property is a crucial aspect of the DOM (Document Object Model) in web development, providing the height of an element. This property includes the element's padding and borders but excludes the margins. Understanding and utilizing offsetHeight is essential for accurately measuring and manipulating the dimensions of elements on a webpage.
Definition
The offsetHeight property returns the height of an element in pixels. This measurement includes the element's padding and borders, which are part of the element's box model, but it does not account for any margins applied to the element. This makes offsetHeight particularly useful when you need to know the total space an element occupies within its parent, excluding any external spacing.
Practical Implementation
To effectively use offsetHeight, consider the following example, which demonstrates how to grab the dimensions of both visible and hidden elements. This example is part of a larger script that measures the dimensions of images on a webpage.
Example: Grabbing the Dimensions of Hidden Elements
<div>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Suspendisse congue facilisis dignissim. Fusce sodales,
odio commodo accumsan commodo, lacus odio aliquet purus,
<img src="../images/ninja-with-pole.png" id="withPole" alt="ninja pole"/>
<img src="../images/ninja-with-shuriken.png"
id="withShuriken" style="display:none" alt="ninja shuriken" />
vel rhoncus elit sem quis libero. Cum sociis natoque
penatibus et magnis dis parturient montes, nascetur
ridiculus mus. In hac habitasse platea dictumst. Donec
adipiscing urna ut nibh vestibulum vitae mattis leo
rutrum. Etiam a lectus ut nunc mattis laoreet at
placerat nulla. Aenean tincidunt lorem eu dolor commodo
ornare.
</div>
<script>
(function(){
const PROPERTIES = {
position: "absolute",
visibility: "hidden",
display: "block"
};
window.getDimensions = element => {
const previous = {};
for (let key in PROPERTIES) {
previous[key] = element.style[key];
element.style[key] = PROPERTIES[key];
}
const result = {
width: element.offsetWidth,
height: element.offsetHeight
};
for (let key in PROPERTIES) {
element.style[key] = previous[key];
}
return result;
};
})();
document.addEventListener("DOMContentLoaded", () => {
setTimeout(() => {
const withPole = document.getElementById('withPole'),
withShuriken = document.getElementById('withShuriken');
assert(withPole.offsetWidth === 41,
"Pole image width fetched; actual: " +
withPole.offsetWidth + ", expected: 41");
assert(withPole.offsetHeight === 48,
"Pole image height fetched: actual: " +
withPole.offsetHeight + ", expected 48");
assert(withShuriken.offsetWidth === 36,
"Shuriken image width fetched; actual: " +
withShuriken.offsetWidth + ", expected: 36");
assert(withShuriken.offsetHeight === 48,
"Shuriken image height fetched: actual: " +
withShuriken.offsetHeight + ", expected 48");
const dimensions = getDimensions(withShuriken);
assert(dimensions.width === 36,
"Shuriken image width fetched; actual: " +
dimensions.width + ", expected: 36");
assert(dimensions.height === 48,
"Shuriken image height fetched: actual: " +
dimensions.height + ", expected 48");
},3000);
});
</script>
Explanation
In the example above, a function getDimensions is defined to measure the dimensions of elements, even if they are hidden. This function temporarily changes the element's style properties to make it visible and measurable, then restores the original styles after obtaining the dimensions. The offsetHeight property is used to fetch the height of both visible and hidden elements, ensuring accurate measurements.
The script includes assertions to verify that the dimensions are fetched correctly, demonstrating the practical use of offsetHeight in testing and debugging scenarios. This approach is particularly useful when dealing with elements that are not immediately visible on the page, such as those hidden with display: none.