Skip to content

Instantly share code, notes, and snippets.

View andrewbranch's full-sized avatar

Andrew Branch andrewbranch

View GitHub Profile
@andrewbranch
andrewbranch / autoshrink.js
Last active July 31, 2019 22:07
Responds to window resize events. Max font size taken from initial font size (specify with CSS).
(function($) {
function getTextWidth($element) {
var tester = $("<div/>").text($element.text())
.css({ "position": "absolute", "float": "left", "white-space": "nowrap", "visibility": "hidden", "font": $element.css("font"), "text-transform": $element.css("text-transform"), "letter-spacing": $element.css("letter-spacing") })
.appendTo($element.parent()),
width = tester.innerWidth();
tester.remove();
return width;
}