For those who don't like plugins like FitText, I just played and calculated the font size so that it fit into the contained element, looking at the current average letter width (complexity: a few lines that are resolved right away, temporarily changing the CSS width) HTML
<div><h1><a><span>Title</span></a></h1></div>
And jQuery:
$("h1").each(function() { var l = $(this).text().length; var w = $(this).prop("offsetWidth"); //clientWidth doesn't always work var old_pos = $(this).find("a").css("position"); var old_w = $(this).find("a").css("width"); $(this).find("a").css("position", "absolute"); $(this).find("a").css("width", "1000px"); var w2 = $(this).find("span").prop("offsetWidth"); var h2 = $(this).find("span").prop("offsetHeight"); var c = 1.2*(w2/h2)/l; // Current proportion of font width, 1.2 is constant var fontsize = w/l/c; fontsize = (fontsize<10)?10:fontsize; //limit min //$(this).append(" "+c+"/"+fontsize); //test //Set font size to fill width of parent element $(this).css("font-size",fontsize+"px"); $(this).find("a").css("position", old_pos); $(this).find("a").css("width", old_w); });
This resizes my fonts in a masonry style grid
source share