DEV Community

Erhie
Erhie

Posted on

how to get height of div in pixels?

Hello!
For a DIV with variable text I hoped to get the height in pixels with the function getComputedStyle, but it delivers auto also if I delay the detection.
Has you a suggestion how to fix it ?
my javascript code snippet:

this.slInfDetail = this.slInfo.appendChild( document.createElement('div') );
s = this.slInfDetail.style;
s.position = "relative";
s.fontSize = `calc(${fontsize} / 2.5 )`
s.textAlign = "left";
s.backgroundColor = "rgba(0,0,0,0.35)";
s.color = "rgb(255,255,255)"
s.zIndex = 999;
this.slInfDetail.innerHTML = "Additional info comes<br>here";
this.slInfComputedHeight;
setTimeout(() => {
    let sComputed = getComputedStyle(this.slInfDetail);
    this.slInfComputedHeight = sComputed.height; // is 'auto' 
});
Enter fullscreen mode Exit fullscreen mode

Top comments (0)