Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
150
rated 0 times [  153] [ 3]  / answers: 1 / hits: 90946  / 14 Years ago, fri, november 12, 2010, 12:00:00

In JavaScript/jQuery, how can I scale a varying-length line of text inside a fixed-width Div so that the one line always fits inside the Div (as one line)?


More From » jquery

 Answers
3

This is somewhat of a hack, but will do what you want.



<div id=hidden-resizer style=visibility: hidden></div>


Place this at the bottom of your page, where it will not be moving other elements on the page.



Then do this:



var size;
var desired_width = 50;
var resizer = $(#hidden-resizer);

resizer.html(This is the text I want to resize.);

while(resizer.width() > desired_width) {
size = parseInt(resizer.css(font-size), 10);
resizer.css(font-size, size - 1);
}

$(#target-location).css(font-size, size).html(resizer.html());

[#94988] Wednesday, November 10, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
payton

Total Points: 307
Total Questions: 96
Total Answers: 83

Location: Saint Vincent and the Grenadines
Member since Sat, Sep 11, 2021
3 Years ago
;