Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS min() function to SASS

Tags:

css

sass

css-calc

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?

like image 489
Yosef Avatar asked Jun 09 '20 10:06

Yosef


People also ask

How do we call a function in 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.

Can CSS use min?

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.

Can you use CSS in Sass?

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.

Which CSS function uses the smallest value?

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.


3 Answers

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));
like image 197
user1565824 Avatar answered Oct 16 '22 10:10

user1565824


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));
like image 35
Maurici Abad Avatar answered Oct 16 '22 09:10

Maurici Abad


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);
}
like image 3
Lrnt Avatar answered Oct 16 '22 10:10

Lrnt