Sass is returning a list of negative numbers instead of subtracting them

前端 未结 2 2076
面向向阳花
面向向阳花 2020-12-12 03:55

I\'m trying to do math operation with lengths:

$itemWidth: 25px;
$item2Width: 80px;

element {
    width: $itemWidth/2-$item2Width/2-5px
}

相关标签:
2条回答
  • 2020-12-12 04:36

    The Sass 3.2 parser seems to be confused by your lack of whitespace between the operators (it thinks you mean negative 5px rather than subtract 5px):

    element {
        width: $itemWidth / 2 - $item2Width / 2 - 5px;
    }
    

    This is fixed in Sass 3.3, but you'll need to upgrade Compass to 1.0 (since 0.12 is incompatible with Sass 3.3).

    Note that Sass will always treat numbers immediately preceded by a dash/hyphen as negation. This means that 1 -2 is treated as a list of numbers containing the positive number one and the negative number two, rather than evaluating to the negative number 1.

    0 讨论(0)
  • 2020-12-12 04:48

    Wrap the calculation in parentheses. This tells SASS that the whole thing is part of a calculation.

    $itemWidth: 25px;
    $item2Width: 80px;
    
    element {
        width: ($itemWidth/2-$item2Width/2-5px);
    }
    
    0 讨论(0)
提交回复
热议问题