0

I have a div with fixed with and height and a big text. For example: http://jsfiddle.net/gEjTz/ I want to reseze text to appear in this div. How to do this? Thanks!

<div style="height: 20px; border: 1px solid black;">
    <span style="display: inline-block; font-size: 50px;">This is my text that I want to fit ssssssssssssssssssssssssssssssss s</span>
</div>
$(function() {
    var span = $('span');
    var fontSize = parseInt(span.css('font-size'));

    do {
        fontSize--;
        span.css('font-size', fontSize.toString() + 'px');
    } while (span.height() >= 20);
});

2 Answers2

0

don't know if you wanted to keep that span ... i set it up to work without that ... sorry if you needed it

using an idea from Check with jquery if div has overflowing elements, i've set this jsfiddle for you.

you just need to check if the element overflow is triggered, and while that happens reduce the font-size.

But for the user's sake, i would suggest you keep a constant font size and add overflow: auto; to the parent div

Community
  • 1
  • 1
0

If you want to have fixed dimensions of the box, you cant set the font-size. If you want to set the font-size, you cant set dimensions. you need to do this:

<div style="display:inline; border: 1px solid black;">
<span style="font-size: 50px;">This is my text that I want to fit ssssssssssssssssssssssssssssssss s</span>

Watch here: JSFiddle

EDIT: JSFiddle changed

Daniele94
  • 113
  • 8