Suppose i have a variable:
$var: 5px;
but somewhere in code its value have changed in to possible color, number, em, rm etc.
Is there any function to detect which type of value it has?
i.e
@if is-color($var) { //do something }
I know there is no is-color function in sass but are there other methods to do this or function?
SCSS is compiled to CSS during compile time, and SCSS variables are replaced with resolved value during compile time, which means there is no way to change the variable during run time. However, CSS variables just sits there during run time, and you can dynamically CRUD them during run time with JavaScript (Web API).
The basic syntax for defining a variable is simple: Just use a $ before the variable name and treat its definition like a CSS rule: Sass Variable Syntax: $<variable name>:<value>; The following declares a variable named large-font.
Sass variables are all compiled away by Sass. CSS variables are included in the CSS output. CSS variables can have different values for different elements, but Sass variables only have one value at a time.
Sass uses $ to distinguish variables (such as $highlight-color , $sidebar-width ). The dollar sign was chosen because it's visually distinctive, it's aesthetically pleasing, and it's not used elsewhere in CSS and thus doesn't come into conflict with any present or future CSS syntax.
From the Sass documentation:
type_of($value)
Returns the type of a value.
Examples:
type-of(100px) => number type-of(asdf) => string type-of("asdf") => string type-of(true) => bool type-of(#fff) => color type-of(blue) => color
http://sass-lang.com/documentation/Sass/Script/Functions.html#type_of-instance_method
(note that -
and _
is interchangeable in Sass functions).
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 */
}
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