I'm trying to set the spacing of my sidebar from the top equal to the height of the header. However, when I use clientHeight
in JavaScript to get the height and then try to apply it to other elements using marginTop
or top
values (with position
included), it doesn't work as expected. The value is correct when outputted with alert()
, but it's not affecting the layout.
Here's an example of the HTML:
<body onload="sidenav_height()">
...
</body>
And here's the JavaScript function being used:
function sidenav_height(){
var height = document.getElementById("header").clientHeight;
document.getElementById("sidenav").style.marginTop = height + "px";
}