Skip to content

Instantly share code, notes, and snippets.

@Patrick64
Last active January 25, 2019 09:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Patrick64/b5abab784ba2afb99509c2e7501af9fd to your computer and use it in GitHub Desktop.
Save Patrick64/b5abab784ba2afb99509c2e7501af9fd to your computer and use it in GitHub Desktop.
/**
* Set font size so the text fits the width of the container
*/
function setupTitleTextFill(container, textElement) {
// make element inline block and wrap a div so it still does a line break
$(container + " " + textElement).css("display","inline-block").wrap('<div class="title-text-fill-wrapper"></div>');
resizeTitleTextFill(container, textElement);
$(window).resize(function() {
resizeTitleTextFill(container, textElement);
});
}
function resizeTitleTextFill(container, textElement) {
var maxFontSize = 80; //px
var minFontSize = 25; //px
$(container).each(function() {
var $heading = $(this).find(textElement);
$heading.css("font-size","10px");
var maxWidth = $(this).width();
var widthAt10px = $heading.width();
var newFontSize = Math.min(Math.max(10 * (maxWidth / widthAt10px),minFontSize),maxFontSize);
$heading.css("font-size",newFontSize + "px");
})
}
$( document ).ready(function() {
setupTitleTextFill('.title-banner-content','h3');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment