Skip to content

Instantly share code, notes, and snippets.

@pigeonfresh
Last active January 3, 2022 11:59
Show Gist options
  • Save pigeonfresh/42fe76f646bcb0fd7ebcdcab7a41ff96 to your computer and use it in GitHub Desktop.
Save pigeonfresh/42fe76f646bcb0fd7ebcdcab7a41ff96 to your computer and use it in GitHub Desktop.
A SCSS function to create a fluid value that is clamped between a min and max value.
@import "utils/fluid";
.element {
// Create a fluid value between the viewport widths 375px and 1920px (which are defined as defaults in the function)
padding: fluid(10px, 40px);
// Create a fluid value between 375px and 1024px
margin: fluid(10px, 40px, 375px, 1024px);
}
$min-viewport-width: 375px;
$max-viewport-width: 1920px;
@function strip-unit($value) {
@return $value / ($value * 0 + 1);
}
@function fluid(
$start-value,
$end-value,
$min-vw: $min-viewport-width,
$max-vw: $max-viewport-width
) {
$min-value: $start-value;
$max-value: $end-value;
@if $end-value < $start-value {
$min-value: $end-value;
$max-value: $start-value;
}
@return clamp(
#{$min-value},
calc(
#{$start-value} + ((1vw - #{$min-viewport-width / 100}) * #{(
(strip-unit($end-value - $start-value) / strip-unit($max-vw - $min-vw)) * 100
)})
),
#{$max-value}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment