Skip to content

Instantly share code, notes, and snippets.

@Cleecanth
Last active February 1, 2023 20:20
Show Gist options
  • Save Cleecanth/19a5b561699d1b1e3527dc328c497743 to your computer and use it in GitHub Desktop.
Save Cleecanth/19a5b561699d1b1e3527dc328c497743 to your computer and use it in GitHub Desktop.
Font Clamp calculator
@use 'sass:math';
/**
* Sass function, adapted from:
* @link https://chrisburnell.com/clamp-calculator/
*/
@function fs-clamp($min-fs, $max-fs, $min-vw, $max-vw) {
$x: div(
($max-fs - $min-fs),
($max-vw - $min-vw)
);
$y: $max-fs - $max-vw * $x;
$calc: calc(#{rem($y)} + #{strip($x) * 100vw});
@return clamp(#{$min-fs}, #{$calc}, #{$max-fs});
}
/** sass math.div fallback **/
@function div($a, $b) {
@if (function-exists("div", "math")){ @return math.div($a, $b); }
@return $a / $b;
}
/** basic rem function **/
@function rem($size, $base: 16px) {
@return strip(div($size, $base)) * 1rem;
}
/** strip units **/
@function strip($v) {
@return if(type-of($v) == 'number' and not unitless($v), div($v, ($v * 0 + 1)), $v);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment