As part of my Google Summer of Code 2011 I have to write a client side visualization library in JavaScript and of course textual elements are part of this visualizations. To calculate the dimensions of any text you can make use of an HTML DIV element. With the help of some specific styles it will then auto adapt its size to the text. That’s how you do it.
Add the following to your stylesheet (CSS).
This snippet adds a CSS class named textDimensionCalculation which we will apply to the DIV for the calculation of the text dimensions. The next listing contains the interesting part. I’m using jQuery to simplify things in this example.
With the help of this little snippet we can now calculate the text dimensions like this.
That's me, Ben.
Hey, I am Ben Ripkens (bripkens)
and this is my blog.
I live in
Düsseldorf
(Germany)
and I am employed by the
codecentric AG as a Software Engineer.
Web application frontends are my main area of expertise, but you may also
find some other interesting articles on this blog.