问题
Why this one calc(100vw/4) works but neither of the following do?
calc(100vw/4-(10-4))calc(100vw/4-(10px-4))calc(100vw/4-(10px-4px))calc(100vw/4-calc(10px-4px))
How do I manage the the expression calc(100vw/x-(10px-x)) where x gets replaced in a SASS loop to work?
回答1:
You need to add spaces between operators, it's a common mistake to forget them. We can also nest operation using calc as many as we want but they are equivalent to simple parentheses.
From the documentation:
Note: The
+and-operators must be surrounded by whitespace. For instance,calc(50% -8px)will be parsed as a percentage followed by a negative length—an invalid expression—whilecalc(50% - 8px)is a percentage followed by a subtraction operator and a length. Likewise,calc(8px + -50%)is treated as a length followed by an addition operator and a negative percentage.The
*and/operators do not require whitespace, but adding it for consistency is both allowed and recommended.Note: It is permitted to nest
calc()functions, in which case the inner ones are treated as simple parentheses.
.one {
background: red;
width: calc(100% - 150px);
margin-top: calc(20px + calc(40px * 2)); /*Same as calc(20px + (40px * 2))*/
height: calc(100px - 10px);
padding: calc(5% + 10px) calc(5% - 5px);
}
<div class="one">
</div>
来源:https://stackoverflow.com/questions/46931291/why-css-calc-function-is-not-working-with-complex-expression