Calculate text pixel width pre-render

Working out widths of a text element before the element has rendered in the DOM takes a little trickery, but is relatively easy.

I thought through a few different ways to accomplish this and settled on rendering a span element outside of the viewable page. The throw away element, once rendered, has any required classes applied to it before the width is taken and it is destroyed.

This post has been viewed 1303 times.

Leave a Reply

Your email address will not be published. Required fields are marked *