Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Visually Long is a String?

Tags:

text

css

I'd love to know how long a string is when it appears on the screen. We're not using a fixed width font, so:

"Our mother's tummy makes dummy noises."

is much wider than:

"Lilly Leadbetter lives life leisurely."

Is there a way to tell how long something is by the characters? I don't need pixel perfect accuracy, just long enough to ellipse at roughly the right spot. CSS overflow won't help, because I can't attach the ellipse after CSS has determined how long it is.

like image 507
aronchick Avatar asked Sep 13 '26 16:09

aronchick


1 Answers

Here's a related question with at least a partial solution. Basically the technique is to render the text in a hidden <span> and then (using JavaScript) measure the pixel width of the span, then chop off characters until it fits in your target width.

And here's a jQuery plugin that encapsulates this sort of functionality, linked from this related question.

like image 104
Jordan Running Avatar answered Sep 16 '26 08:09

Jordan Running



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!