Skip to content

Instantly share code, notes, and snippets.

@dmitry-valkov
Created September 18, 2019 18:55
Show Gist options
  • Save dmitry-valkov/1d2977d619dbb20c22ec48d9f46d69a6 to your computer and use it in GitHub Desktop.
Save dmitry-valkov/1d2977d619dbb20c22ec48d9f46d69a6 to your computer and use it in GitHub Desktop.
SCSS mixin for generate min/max and responsive width based on screen size.
/// Remove the unit of a length
/// @param {Number} $number - Number to remove unit from
/// @return {Number} - Unitless number
@function stripUnit($number) {
@if type-of($number) == 'number' and not unitless($number) {
@return $number / ($number * 0 + 1);
}
@return $number;
}
$baseScreenWidth: 1920;
$minScreenWidth: 1024;
$cardWidthStd: 327;
@function getVWSizeFromPX($pxSize, $screenSize) {
@return round($pxSize / $screenSize * 100) * 1vw;
}
@function getPXSizeFromVW($vwSize, $screenSize) {
@return ($screenSize * ( stripUnit($vwSize) / 100 )) * 1px ;
}
@mixin screen-dependent-sizes($width) {
$width: getVWSizeFromPX($width, $baseScreenWidth);
width: $width;
min-width: getPXSizeFromVW($width, $minScreenWidth);
max-width: getPXSizeFromVW($width, $baseScreenWidth); ;
}
.card {
@include screen-dependent-sizes($cardWidthStd);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment