I have CSS function like below:
width: min(calc(100vw - 51rem), calc(100vh - 2rem));
It's working fine when using a CSS file but not when used in SASS. How can I rewrite this function for SASS?
Functions are defined using the @function at-rule, which is written @function <name>(<arguments...>) { ... } . A function's name can be any Sass identifier. It can only contain universal statements, as well as the @return at-rule which indicates the value to use as the result of the function call.
You can use min() , max() , and clamp() on the right hand side of any CSS expression where it would make sense. For min() and max() , you provide an argument list of values, and the browser determines which one is either the smallest or largest, respectively.
First, Sass is easy to understand if you know CSS. Since it's a CSS preprocessor its syntax is similar. Also, if you use Sass, your CSS code will be compatible with all versions of browsers.
min() The min() CSS function lets you set the smallest (most negative) value from a list of comma-separated expressions as the value of a CSS property value.
When using SASS, min() is a function within SASS itself therefore your statement is not making it to your CSS when processed. There is a quick workaround since SASS is case sensitive and CSS is not. Simply capitalize the "M" in Min(). The Min() statement will not be recognized as the internal SASS min() function but will work in CSS.
width: Min(calc(100vw - 51rem), calc(100vh - 2rem));
You can wrap min
in a calc
and it works:
width: calc(min(calc(100vw - 51rem), calc(100vh - 2rem)));
In your case you can even simplify the code:
width: calc(min(100vw - 51rem, 100vh - 2rem));
You can use these Sass functions to force the CSS functions.
@function css-function( $function, $values... ) {
@return
$function
+ unquote( '(' )
+ $values
+ unquote( ')' )
;
}
@function css-min( $values... ) {
@return css-function( min, $values );
}
@function css-max( $values... ) {
@return css-function( max, $values );
}
@function css-clamp( $values... ) {
@return css-function( clamp, $values );
}
You might still need to use #{ } interpolation when using Sass variables and CSS variables combined.
Usage:
$width-first: 40px;
$width-second: 30px;
.my-selector {
width: css-min(#{$width-first}, #{$width-second});
}
Compiled:
.my-selector {
width: min(40px, 30px);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With