Sass - Check which kind of value a variable has

前端 未结 2 1217
星月不相逢
星月不相逢 2020-12-14 19:49

Suppose i have a variable:

$var: 5px;

but somewhere in code its value have changed in to possible color, number

2条回答
  •  旧时难觅i
    2020-12-14 20:31

    To be a little clearer, here's how you might use type-of:

    @if type-of($my-variable) == string {
        /* do something */
    }
    

    In addition to the types shown in the docs, type-of will also return 'map' if passed a SASS map object:

    $font-sizes: (
        small: rem-calc(18px),
        medium: rem-calc(20px),
        large: rem-calc(22px)
    );
    
    @if type-of($font-sizes) == map {
        /* do map-related thing */
    } @else {
        /* do other thing */
    }
    

提交回复
热议问题