0

I found this question to get the height of an element that has not height Attribute. But is it possible to get or calculate the height before it is rendert?

for example I have this Code:

var element = createElement("div");
element.style.width = "20px";
element.innerHTML = "XXXXXXXXXXXXXXXXXXXXXXXXX";

If I know the fontsize and other stuff like padding etc. , is it possible to get the height it would have if it gets rendert?

I need this because I need to know how heigh the element is before it gets rendert to change things that happens after that.

Nico.E
  • 82
  • 11
  • 1
    Short answer, no. You cannot get the height of an element that isn't part of the DOM. A quick fix is to append it to the DOM in an absolute position that's well off the screen, eg `left: -9999px` and then get its size. Be aware that this element won't inherit any styling from its parents though, if that's relevant to your situation – Rory McCrossan Sep 03 '18 at 07:43
  • But it would be possible to calculate how many rows it would have if I dont need the exact height, right? – Nico.E Sep 03 '18 at 07:49
  • What do you mean by 'rows'? You're appending a div, not a table. Did you mean lines of text? If so, yes. That would be `height / line height` – Rory McCrossan Sep 03 '18 at 07:51
  • Yes I mean lines of text. But how can I do `height / line height` if I dont have the height of the div? – Nico.E Sep 03 '18 at 07:55
  • You can get the height of the div through the method I described in my first comment...? – Rory McCrossan Sep 03 '18 at 07:56
  • Yeah but I mean without your workaround to Display it somewhere. My current solution is now just counting the chars. – Nico.E Sep 03 '18 at 09:30

0 Answers0