CSS Calc数除法 [英] css calc number division
问题描述
如果我使用计算器,则2/3
是0.6666666667
,大约是67%
.但是,如果我尝试对css calc执行相同的操作,则会收到错误消息.
If I use a calculator, 2/3
is 0.6666666667
which is about 67%
. However if I try to do the same thing with css calc I get an error.
width: calc(2 / 3);
有没有一种可行的方法?
Is there a working way for this?
我觉得像0.666666666667
这样写看起来不太好.任何想法都欢迎.
I don't think it looks that good writing it like 0.666666666667
. Any ideas are welcome.
推荐答案
calc(2 / 3)
的问题是,您只会获得不带单位的数字. CSS不能仅显示数字作为宽度.这就像设置width: 3
显然不起作用一样.
The problem is with calc(2 / 3)
you will just get a number without an unit. CSS can't display just a number as width. This would be like if you set width: 3
which obviously doesn't work.
如果您想要百分比,则需要将其成倍增加100%
If you want the percentage you will need to muliply it by 100%
width: calc(2 / 3 * 100%);
如果您真的希望结果以像素为单位乘以1px
and if you really want the result in pixels multiply it by 1px
width: calc(2 / 3 * 1px);
这篇关于CSS Calc数除法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!