Skip to content

Instantly share code, notes, and snippets.

@tphalp
Forked from jesstelford/jquery.textfill.js
Last active December 14, 2015 13:28
Show Gist options
  • Save tphalp/5093228 to your computer and use it in GitHub Desktop.
Save tphalp/5093228 to your computer and use it in GitHub Desktop.
Re-sizes text font to fill container element.
(function($) {
$.fn.textfill = function(maxFontSizePx, minFontSizePx, element) {
maxFontSizePx = parseInt(maxFontSizePx, 10);
minFontSizePx = parseInt(minFontSizePx, 10);
if (maxFontSizePx > 0 && minFontSizePx > maxFontSizePx) {
minFontSizePx = maxFontSizePx;
}
element = typeof(element) == "string" ? element : "span";
return this.each(function(){
var ourText = $(element, this),
parent = ourText.parent(),
maxHeight = parent.height(),
maxWidth = parent.width(),
fontSize = parseInt(ourText.css("fontSize"), 10),
multiplier = maxWidth/ourText.width(),
newSize = (fontSize*(multiplier-0.1));
if (maxFontSizePx > 0 && newSize > maxFontSizePx) {
newSize = maxFontSizePx;
} else if(minFontSizePx > 0 && newSize < minFontSizePx) {
newSize = minFontSizePx;
}
ourText.css("fontSize",newSize);
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment