Skip to content

Instantly share code, notes, and snippets.

@itsjavi
Created March 5, 2013 15:12
Show Gist options
  • Select an option

  • Save itsjavi/5090973 to your computer and use it in GitHub Desktop.

Select an option

Save itsjavi/5090973 to your computer and use it in GitHub Desktop.
Fit font-size in container's width
$.fn.fitFontSize = function(minFontSize, containerWidth){
minFontSize = minFontSize || 12;
return this.each(function(){
var $this = $(this);
var sectionWidth = containerWidth || $this.parent().innerWidth();
var spanWidth = $this.width();
var newFontSize = (sectionWidth/spanWidth) * minFontSize;
$this.css({"font-size" : newFontSize, "line-height" : (newFontSize+2) + "px"});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment