CSS calc() 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14967421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 21:42:36  来源:igfitidea点击:

CSS calc() not working

css

提问by itsmikem

Is there a limit to how many operands you can have within a CSS calc()function?

在一个 CSScalc()函数中可以有多少个操作数有限制吗?

This works:

这有效:

div {
    left:calc((100%/54)*26);
    left:-webkit-calc((100%/54)*26);
}

This does NOT work:

这不起作用:

div {
    left:calc(((100%/54)*14)-140px);
    left:-webkit-calc(((100%/54)*14)-140px);
}

Of course, the latter is what I need, because I need to offset a few pixels, but as soon as I try to do that, the value seems to just go to zero. Any insight is appreciated!

当然,后者是我需要的,因为我需要偏移几个像素,但是一旦我尝试这样做,该值似乎就会变为零。任何见解表示赞赏!

回答by Ryan Kinal

To quote MDN

引用MDN

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. The *and /operators do not require whitespace, but adding it for consistency is allowed, and recommended.

+-运营商必须始终由空格包围。calc(50% -8px)例如,的操作数将被解析为一个百分比,后跟一个负长度,一个无效的表达式,而 的操作数calc(50% - 8px)是一个百分比,后跟一个减号和一个长度。在*/运营商不需要的空白,但添加它的一致性是允许的,并建议。

Space your stuff out, and it will probably work.

把你的东西隔开,它可能会起作用。