Skip to content

Instantly share code, notes, and snippets.

@vaicine
Created May 13, 2013 09:21
Show Gist options
  • Save vaicine/5567141 to your computer and use it in GitHub Desktop.
Save vaicine/5567141 to your computer and use it in GitHub Desktop.
Textfill that actually works
(function($) {
$.fn.textfill = function(maxFontSize) {
maxFontSize = parseInt(maxFontSize, 10);
return this.each(function(){
var ourText = $("a", 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));
ourText.css(
"fontSize",
(maxFontSize > 0 && newSize > maxFontSize) ?
maxFontSize :
newSize
);
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment