admin管理员组

文章数量:1415421

I have a div with fixed height and width and inside I have text that is changing. Sometimes it can be a word or two and sometimes it can be a sentence. What I need is to shrink the font size so the text fits to that div.

I have a div with fixed height and width and inside I have text that is changing. Sometimes it can be a word or two and sometimes it can be a sentence. What I need is to shrink the font size so the text fits to that div.

Share Improve this question edited May 21, 2019 at 3:28 Andrew Myers 2,7865 gold badges35 silver badges41 bronze badges asked Dec 15, 2010 at 11:16 emirkljucaninemirkljucanin 8241 gold badge9 silver badges22 bronze badges 2
  • Do you have a monospace font within the div? – Joel Etherton Commented Dec 15, 2010 at 11:17
  • you should check my answer for a better performance – Hoffmann Commented Nov 15, 2012 at 11:38
Add a ment  | 

6 Answers 6

Reset to default 2

i had an idea and it worked :) here is my code

        $('li').each(function () {
            while ($(this).outerHeight() > 25) {
                var currentFontSize = $(this).css("font-size");
                $(this).css("font-size", (parseFloat(currentFontSize) - 1) + "px");
            }

        });

I had a similar issue, which made me write my own plugin for this. One solution is to use the shrink-to-fit-approach, as described by user54316. However if you have to fit multiple items or are concerned with performance, e.g., on window resize, have a look at jquery-quickfit.

It meassures and calculates a size invariant meassure for each letter of the text to fit and uses this to calculate the next best font-size which fits the text into the container.

The calculations are cached, which makes it very fast (there is virtually no performance hit from the 2nd resize on forward) when dealing with multiple texts or having to fit a text multiple times, like e.g., on window resize. I think it would work perfect in your case.

You'd just have to call

$('#yourid').quickfit()

after you changed the text.

Production example, fitting 14x16x2 texts

Try giving the font-size in em:

http://clagnut./blog/348/

http://kyleschaeffer./best-practices/css-font-size-em-vs-px-vs-pt-vs/

There is a jQuery plugin for that: FitText.js

Here the URL: https://github./davatron5000/FitText.js

Here's an example: http://jsfiddle/martinschaer/sRvB9/

The function fitText() receives a parameter, with which you need to "play" in order to get the results you want. Also, it resized the text when the window is resized; if you need to have the text resized when (for example) a div is resized, you should add a pair of JS lines for that ;)

$text.css('font-size', "100px");
$text.css('line-height', "100px");
var foo= $div.width()    / $text.width();
var bar= $div.height()   / $text.height();

if(foo < bar) {
    foo=Math.floor(foo*100) +"px";
    $text.css('font-size', foo);
    $text.css('line-height', foo);
} else {
    bar=Math.floor(bar*100) +"px";
    $text.css('font-size', bar);
    $text.css('line-height', bar);
}

//centralizing text, top and left are defined as 50% on the CSS, optional
$text.css('margin-left', -$text.width()  /2 + "px");
$text.css('margin-top',  -$text.height() /2 + "px");

One small note is that you need to call this function if the div itself changes size. You can bind this code to the resize eventof the div.

you can do it by defining the font size like......

.small {font-size: 0.8em}

you can see effect here working demo

本文标签: javascriptFit text to a divStack Overflow