在 CSS 中指定 0 值时,我应该明确标记单位还是省略单位?
这更多的是一个“哲学”论证,但我想知道这里推荐的做法是什么。我还没有将其设置为 Wiki,以防有“官方”答案。
显然,0px 和 0em 或其他什么之间没有区别,因此可以简单地指定 0 并且单位是多余的(请参阅 0 和 0em 之间的 CSS 差异)。一些回答该问题的人认为应该始终省略单位。
然而,在我看来,省略单位更容易出错,因为以后的更改可能会意外地省略单位。它与文档中其他地方的非零元素也不太一致。
This is more of a 'philosophy' argument, but I'd like to know what the recommended practice here. I'm not setting it up as a Wiki yet in case there is an 'official' answer.
Obviously, there is no difference between 0px and 0em or whatever, so one could simply specify 0 and the units are redundant (see CSS difference between 0 and 0em). Some of the folks who answered that question argued that one should always omit the units.
However, it seems to me that omitting the unit is more error-prone, since a later change may accidentally omit the unit. It is also less consistent with non-zero elements elsewhere in the document.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我认为你也应该省略单位。
从程序员的角度来看,
0 == null == none == false
,其中仅0px == 0px
。这意味着如果您指定
0
的边框宽度,则不会有边框,但如果您指定0px
边框,则会创建 0 px 的边框(这就是其背后的想法,实际上0px
给出的结果与0
完全相同)。进一步的点
0
使其更易于阅读,因为它很容易与正常的单位值区分开来。结论:省略 0 中的单位。它们不是必需的,而且会造成混淆。
I argue you should also omit the units.
From a programmer's perspective,
0 == null == none == false
, where0px == 0px
only.Which means that if you specify a border width of
0
then no border will be there, but if you specify a0px
border, then a border of 0 px will be created (that's the idea behind it, in reality0px
gives the exact same result like0
).Further Points
0
makes it easier to read as it is easily distinguishable from normal unit'ed values.Conclusion: Omit the units in 0. They're not needed and confusing.
作为这个问题的注释:无单位 0 作为长度在 calc() 和其他数学函数中不起作用。您必须写
0px
。如果省略 0 的单位是从某个 css 变量中获取的,事情会更加麻烦。规范:
我建议在编写 CSS 变量时始终使用
0px
。因此,当您和其他人尝试在某些 calc() 函数中使用该变量时,它不会让您和其他人感到困惑。As a note to this question: Unitless 0 as length won't work in
calc()
and other math functions. You have to write0px
. And things would be more buggy if the unit omitted 0 is taken from some css variable.Specification:
I would suggest always use
0px
when you are writing CSS variables. So it won't make you and others confuse when they are trying to use the variable in somecalc()
functions.CSS 规范 说(引用):
我已经读过很多次建议省略该单位,我不记得在哪里了。
所以省略它们。
The CSS specification says (quote):
And I've read many times that it is suggested to omit the unit, I can't remember where.
So omit them.
我还喜欢将单位放在零前面,因为...
<块引用>
但是,如果您打算更改级联中的先前值,您会注意到它。 '0em' 表示“将先前的值更改为 '0em”,但零可能只是表示“忽略此规则”并保持先前的规则有效。这可能根本不是你对赤裸裸的“0”的初衷。 原始文章链接
I also like to put units by my zeros because ...
我说省略单位,这将有助于压缩 CSS 并提高性能。虽然不多,但每一点都有帮助。
I say omit the unit, this will help when you compress the CSS and increase performance. It isn't much, but every little bit helps.
在当前的浏览器开发人员工具中,您可以使用上/下光标键轻松调整实时值,但如果您指定不带单位的
0
,那么它将不起作用,因为浏览器不知道什么单位你喜欢吗,设置1
,2
... 没有单位没有意义。这就是为什么我现在也总是将单位指定为零值。任何 CSS 缩小器都会在缩小时将其更改为
0
,因此您甚至不需要关心它。In current browsers developer tools you can easily tweak the values live with the up/down cursor keys, but if you specify
0
without a unit, then it will not work, as the browser will not know what unit do you prefer, and setting1
,2
... without a unit has no meaning.That is why I am always specifying the unit nowadays on zero values too. Any CSS minifier will change that to
0
on minification so you don't even need to care about it.我总是省略,因为它看起来更容易阅读,零会弹出,并且很容易与它周围的带单位值区分开来。
I always omit as it seems easier to read, the zero pops out and is easily distinguishable from the with-unit values around it.
我逐渐开始相信零和单位。
我遵循风格指南,要求使用无单位零。我也觉得他们更好看。我同意这里所有支持无单位的论点。但在我的职业生涯中,有两次错误是由无单位零引起的。 (一个与
calc
有关,另一个与从0到某物的转换有关 - 但我不记得细节了。)等式一侧的“导致错误”可以是任何无论美学上多么令人愉悦或合乎逻辑,支持无单位的论证都会相互竞争吗?
顺便说一句,我最初将无单位零样式归因于 Airbnb 的指南,但在重读之后,它实际上并没有明确说明,但很接近: https://stackoverflow.com/a/55391061/2836695
I'm gradually starting to believe in zeros with units.
I follow a style guide that says to use unitless zeros. I also think they look better. I agree with all the pro-unitless arguments on here. But twice in my career a bug has been caused by unitless zeros. (one having to do with
calc
and one having to do with a transition from 0 to something - but I can't remember the details.)With "causes bugs" on one side of the equation can any pro-unitless argument compete no matter how aesthetically pleasing or logical?
BTW, I originally attributed the unitless zero style to Airbnb's guide, but after rereading it, it doesn't actually say that explicity, but comes close: https://stackoverflow.com/a/55391061/2836695