Skip to content

Instantly share code, notes, and snippets.

@ejfox
Created May 5, 2024 18:42
Show Gist options
  • Save ejfox/d8e45a6a6b8b14274e8cef27ee3692a6 to your computer and use it in GitHub Desktop.
Save ejfox/d8e45a6a6b8b14274e8cef27ee3692a6 to your computer and use it in GitHub Desktop.
An extremely simple mechanism for adjusting the font-size of some text based on its length, with tailwindcss classes
function stringLengthToFontSize(string) {
const length = string.length
if (length < 5) {
return 'text-8xl'
} else if (length < 6) {
return 'text-7xl'
} else if (length < 7) {
return 'text-6xl'
} else if (length < 8) {
return 'text-5xl'
} else if (length < 9) {
return 'text-4xl'
} else if (length < 10) {
return 'text-3xl'
} else if (length < 11) {
return 'text-2xl'
} else if (length < 12) {
return 'text-xl'
} else if (length < 13) {
return 'text-lg'
} else if (length < 14) {
return 'text-base'
} else if (length < 15) {
return 'text-sm'
} else if (length < 16) {
return 'text-xs'
} else {
return 'text-xxs'
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment