Why CSS calc() function is not working with complex expression?

和自甴很熟 提交于 2020-03-22 09:12:39

问题


Why this one calc(100vw/4) works but neither of the following do?

  1. calc(100vw/4-(10-4))
  2. calc(100vw/4-(10px-4))
  3. calc(100vw/4-(10px-4px))
  4. 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—while calc(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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!