谷歌搜索这个问题的答案被证明是困难的,所以我想这里有人应该知道。
在 CSS 中,我看到零像素被简单地声明为“0”,但也被声明为“0px”。
mystyle { width: 0; }
anotherstyle { width: 0px; }
“0”的一个小问题是,如果您将其更改为某个非零值,您可能会忘记添加“px”。并且在将值设置为“0”时,您可能会忘记删除“px”。我希望我的代码完全一致,像这样的小事让我抓狂。
这真的归结为个人偏好,例如在哪里缩进吗?
它们似乎都有效,但哪种方式更好,为什么?
谢谢。
为清楚起见编辑:
我写了“像这样的小事让我发疯”。
没有“px”表示 0 不是我所指的。
让我抓狂的是,有两种不同的方法可以做同样简单的事情。
就个人而言,尽管有忘记在适当的地方添加/删除“px”的小问题,但我会继续单独使用“0”,因为我知道它与其他方式一样可以接受。
它们是相同的。
使用width: 0
,因为它更短且更易读。
特别是当你有,例如:
padding: 0px 0px 9px 8px
对比
padding: 0 0 9px 8px
See the specs :
The format of a length value (denoted
by <length> in this specification) is
a <number> (with or without a decimal
point) immediately followed by a unit
identifier (e.g., px, em, etc.).
After a zero length, the unit identifier is optional.
The minor problem with '0' is that if
you change it to some non-zero value,
you might forget to add the 'px'. And
when making a value '0', you may
forget to remove the 'px'
一旦您养成了不带单位标识符编写 0
的习惯,就不会发生这种情况。