Created
March 9, 2015 22:30
-
-
Save GeoffCox/3a60afca4c16849f02a5 to your computer and use it in GitHub Desktop.
Scale font-size until text fits within element
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function scaleFontToFit(element) { | |
if (element) { | |
// for an element | |
var $element = $(element); | |
if ($element.css("overflow") == "hidden") { | |
var elementHeight = $element.height(); | |
var fontSize = parseInt($element.css("font-size"), 10); | |
var $fullElement = $(element).clone().css('overflow', 'visible').height('auto'); | |
$element.after($fullElement); | |
if ($fullElement.height() > elementHeight) { | |
while ($fullElement.height() > elementHeight) { | |
fontSize -= 1; | |
$fullElement.css("font-size", fontSize); | |
} | |
$element.css("font-size", fontSize) | |
} | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment