Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Math with interpolated variables?

Tags:

Consider the following sass:

$font-size: 18;                  $em: $font-size;  $column: $font-size * 3;        // The column-width of my grid in pixels $gutter: $font-size * 1;        // The gutter-width of my grid in pixels  $gutter-em: #{$gutter / $em}em; // The gutter-width in ems. $column-em: #{$column / $em}em; // The column-width in ems;  $foo = $gutter-em / 2; // This results in a value like "1em/2". :( $bar = ($gutter-em / 2); // this doesn't work either, same as above. 

How can I generate a $foo that works, and that I can reuse further in other expressions?

like image 412
Idan Gazit Avatar asked Nov 24 '11 09:11

Idan Gazit


People also ask

How do you interpolate between two variables?

In mathematics, bilinear interpolation is a method for interpolating functions of two variables (e.g., x and y) using repeated linear interpolation.

Is interpolation more accurate than regression?

Interpolation can be used to find the approximate value (or the missing value) of y in the domain x=[a,b] with better accuracy than regression technique.

Which strings do interpolate variables?

String interpolation is common in many programming languages which make heavy use of string representations of data, such as Apache Groovy, Julia, Kotlin, Perl, PHP, Python, Ruby, Scala, Swift, Tcl and most Unix shells.


1 Answers

Sass cannot perform arithemetic operations on strings, only concatenation. When you use interpolation, what you've created is a string that looks like a number:

@debug type-of(#{10px});         // string @debug type-of('10px');          // string @debug type-of(unquote('10px')); // string @debug type-of(10px);            // number 

If you want a number, do not use interpolation and do not use quotes. For converting an integer (eg. 10) to a length (eg. 10px), use multiplication:

$gutter-em: ($gutter / $em) * 1em; @debug type-of($gutter-em);      // number 
like image 92
Hauleth Avatar answered Sep 28 '22 22:09

Hauleth