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!)
Ba
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:
$.fn.resizeText = function () {
var width = $(this).innerWidth();
var height = $(this).innerHeight();
var html = $(this).html();
var newElem = $("", {
html: html,
style: "display: inline-block;overflow:hidden;font-size:0.1em;padding:0;margin:0;border:0;outline:0"
});
$(this).html(newElem);
$.resizeText.increaseSize(10, 0.1, newElem, width, height);
$(window).resize(function () {
if ($.resizeText.interval)
clearTimeout($.resizeText.interval)
$.resizeText.interval = setTimeout(function () {
elem.html(elem.find("div.createdResizeObject").first().html());
elem.resizeText();
}, 300);
});
}
$.resizeText = {
increaseSize: function (increment, start, newElem, width, height) {
var fontSize = start;
while (newElem.outerWidth() <= width && newElem.outerHeight() <= height) {
fontSize += increment;
newElem.css("font-size", fontSize + "em");
}
if (newElem.outerWidth() > width || newElem.outerHeight() > height) {
fontSize -= increment;
newElem.css("font-size", fontSize + "em");
if (increment > 0.1) {
$.resizeText.increaseSize(increment / 10, fontSize, newElem, width, height);
}
}
}
};
Then if you have this html:
Insert text from slipsum here.
You call it just like this:
$(document).ready(function () {
$(".resizeText").resizeText();
});
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).