问题描述
我学习了CSS函数calc()和它的真棒。我试着使用它:
I learned about the CSS function calc() and its awesome. I tried to use it like:
#abc{width:calc(100%-20px)}
但是这个函数的问题是它不工作。我测试了这个代码IE9和Safari并且没有工作。
But the problem with this function is that it doesn't work. I tested this code IE9 and Safari and it didn't work.
为什么不工作?
推荐答案
运算符 - 必须用空格包围:
The operator "-" must be surrounded by spaces:
#abc{width:calc(100% - 20px)}
引用:注意: +和 - 运算符必须始终包含空格。例如,calc的操作数(50%-8px)将被解析为百分比,接着是负长度,无效的表达式,而calc(50%-8px)的操作数是百分比,其后是负号和长度。
Quoting MDN info on calc()
: "Note: The + and - operators must always be surrounded by whitespace. The operand of calc(50% -8px) for instance will be parsed as a percentage followed by a negative length, an invalid expression, while the operand of calc(50% - 8px) is a percentage followed by a minus sign and a length."
关于此的正式声明在的CSS值和单元模块第3级CR。
The formal statement on this is in clause 8.1.1 of the CSS Values and Units Module Level 3 CR.
这篇关于为什么CSS calc()函数不适用于我?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!