Skip to content

Instantly share code, notes, and snippets.

@hjJunior
Created February 7, 2022 18:15
Show Gist options
  • Save hjJunior/8bd2ff6ae0ba5b69df4990f7f3b50bf8 to your computer and use it in GitHub Desktop.
Save hjJunior/8bd2ff6ae0ba5b69df4990f7f3b50bf8 to your computer and use it in GitHub Desktop.
Adjust the root text to be automatically responsible
function clampBuilder( minWidthPx, maxWidthPx, minFontSize, maxFontSize ) {
const root = document.querySelector( "html" );
const pixelsPerRem = Number( getComputedStyle( root ).fontSize.slice( 0,-2 ) );
const minWidth = minWidthPx / pixelsPerRem;
const maxWidth = maxWidthPx / pixelsPerRem;
const slope = ( maxFontSize - minFontSize ) / ( maxWidth - minWidth );
const yAxisIntersection = -minWidth * slope + minFontSize
return `clamp( ${ minFontSize }rem, ${ yAxisIntersection }rem + ${ slope * 100 }vw, ${ maxFontSize }rem )`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment