Skip to content

Instantly share code, notes, and snippets.

@NazarK
Created January 18, 2019 09:28
Show Gist options
  • Save NazarK/7f44785035aeaa9c49e7855ad3af3d18 to your computer and use it in GitHub Desktop.
Save NazarK/7f44785035aeaa9c49e7855ad3af3d18 to your computer and use it in GitHub Desktop.
.text-fit class to fit text into div
function text_fit() {
$(".text-fit").each(function() {
var square = $(this).width()*$(this).height()
var chars = $(this).text().trim().length
var letter_square = square/chars;
var letter_height = Math.sqrt(letter_square)*4/3
$(this).css({"line-height": letter_height+"px", "font-size": letter_height+"px"})
})
}
$(document).on("turbolinks:load",text_fit)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment