Skip to content

Instantly share code, notes, and snippets.

@benabraham
Last active December 9, 2024 20:51
Show Gist options
  • Select an option

  • Save benabraham/0480332e60d14bb98b4c17ffb81f4f51 to your computer and use it in GitHub Desktop.

Select an option

Save benabraham/0480332e60d14bb98b4c17ffb81f4f51 to your computer and use it in GitHub Desktop.
clampDimension
@use "sass:math";
@function clampDimension(
$minViewportWidthPx,
$minDimensionRem,
$maxViewportWidthPx,
$maxDimensionRem,
$remSize: 16px
) {
$minWidth: math.div($minViewportWidthPx, $remSize);
$maxWidth: math.div($maxViewportWidthPx, $remSize);
$slope: math.div(
math.div($maxDimensionRem - $minDimensionRem, 1rem),
$maxWidth - $minWidth
);
$yAxisIntersection: -$minWidth * $slope + $minDimensionRem;
@return clamp(
#{$minDimensionRem},
#{$yAxisIntersection} + #{$slope * 100}vw,
#{$maxDimensionRem}
);
}
.usage-basic {
font-size: clampDimension(400px, 1rem, 1200px, 2rem);
}
.usage-custom-rem-size {
font-size: clampDimension(400px, 1rem, 1200px, 2rem, 24px);
}
@benabraham
Copy link
Author

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