In Javascript, I have a specific line, and I would like to somehow measure how much space (in pixels) it will take within a specific element.
Basically, I have an element that will pop up above everything else (e.g. a tooltip), and I need to set its width manually through Javascript so that it adjusts the text inside. I can’t make it “automatically grow”, naturally, as an inline element will grow horizontally to support its children.
Windows has APIs that do this. Is there a way to do the same in Javascript?
If there is no decent way, what approach do you think is possible? (For example, try a different width and check the height to make sure that it did not pass a certain threshold).
The smaller the "pixel values", the better, I can make hardcode in my JS.
javascript css text layout
Daniel Magliola
source share