Skip to content

Instantly share code, notes, and snippets.

@crazy4groovy
Last active March 28, 2023 22:40
Show Gist options
  • Save crazy4groovy/ffcc197b0d806e8645319caf61e00b0b to your computer and use it in GitHub Desktop.
Save crazy4groovy/ffcc197b0d806e8645319caf61e00b0b to your computer and use it in GitHub Desktop.
logic to compress / expand a text element to fit on one line (JavaScript)
function fitText(el, { maxFontSizePx, minFontSizePx, compressor = 1 } = {}) {
const s1 = el.clientWidth / (compressor * 10);
const s2 = parseFloat(maxFontSizePx);
const s3 = parseFloat(minFontSizePx);
el.style.fontSize = Math.max(Math.min(s1, s2), s3) + 'px';
};
@crazy4groovy
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment