I apologise in advance as I know this question has come up many times before but I just can't seem to find the right solution (and believe me I've tried a few!)
Basically it's the old "Fit text perfectly inside a div without affecting the size of the div". And unless I'm a complete numpty, I believe CSS has no way of doing this. So what I mean basically is rather than doing something like:
#someDiv {
font-size: 12px;
}
or...
#someDiv {
font-size: 1em;
}
...I want to be able to do something like this:
#someDiv {
font-size: fluid;
}
...meaning that whatever text this div contains, scale it to fit perfectly from left to right and top to bottom with no overflow or whitespace.
After trawling through countless websites looking for this CSS solution, I've now accepted that CSS isn't capable of this ...so, enter jQuery.
I've found several jQuery solutions online but they will only scale the text to fit the width, but I want it to scale to the height as well. So effectively I want to say to jQuery:
"jQuery, find $(this) div and whatever text is inside it I want you to scale it so that it fills the entire height and width of the div as tightly as possible".
In case I haven't explained myself very well, I've attached a graphic explaining the problem I'm facing and the solution I'm looking for.
Any help would be much appreciated. Thank you.
I got the same problem and the solution is basically use javascript to control font-size. Check this example on codepen:
Here's a slightly modified version of the given answers.
Requirements :
What changes in this version ?
Note:
I don't claim the code is production ready. But if you find something you can enhance, please share it with the community.
Sorry late answer but can saves time of many, I write this code which works perfectly during resize of window.
Where text-wrapper is parent and aligned-text is child div. You must specify height and width of parent element. In resize you can set height and width of parent element by using jquery then call this function.
I write this code because I tried my many plugins but they don't support resizing window.
I was wanting something similar myself recently:
And
Fiddler at jsfiddle.net/mn4rr/1/.
Here's the same answer, but in Javascript
By the way...if you ever need to convert coffeescript to javascript, just go to js2coffee.org
I solved this by making a jQuery plugin, it's here: http://jsfiddle.net/c9YNz/2/ (Updated to deal with resizing windows)
The code for the plugin just shrinks the text down to 0.01em size and then grows it to fit, here's the plugin code:
Then if you have this html:
You call it just like this:
It's not the best way to do it, but it's enough for you to be going on with, I would imagine (plus it works).