css - 带或不带 'px' 后缀的 CSS 中零像素的大小?

标签 css size stylesheet pixel zero

<分区>

谷歌搜索这个问题的答案被证明是困难的,所以我想这里有人应该知道。

在 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 的习惯,就不会发生这种情况。

关于css - 带或不带 'px' 后缀的 CSS 中零像素的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5359222/

相关文章:

html - CSS Image Slider 稍微错位

html - 为什么这段代码中的 Canvas 大小与预期不符?

objective-c - iOS获取磁盘上的文件大小

actionscript-3 - 设置 Sprite 宽度或高度而不缩放它的 child

css - Umbraco- 奇怪的样式表问题

html - 如何在 IE 中删除选择选项时的白色背景

wpf - WPF 星号是做什么的(宽度 ="100*")

css - 尝试拥有可以通过 onclick 按钮事件应用的 CSS 样式

html - div 后面的隐藏按钮仅显示提示

javascript - 如何设置元素相对于她的 parent 和她的 sibling 的高度?