在CSS中指定0值时,我应该明确标记单位还是省略?

时间:2022-09-10 23:39:23

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.

这更像是一个“哲学”论点,但我想知道这里的推荐做法。我没有将它设置为Wiki,以防有“官方”答案。

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.

显然,0px和0em之间没有区别,所以可以简单地指定0并且单位是冗余的(参见0和0em之间的CSS差异)。一些回答这个问题的人认为应该总是省略单位。

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.

但是,在我看来,省略该单元更容易出错,因为稍后的更改可能会意外地忽略该单元。它与文档中其他地方的非零元素也不太一致。

6 个解决方案

#1


56  

I argue you should also omit the units.

我认为你也应该省略单位。

From a programmer's perspective, 0 == null == none == false, where 0px == 0px only.

从程序员的角度来看,0 == null == none == false,其中0px == 0px。

Which means that if you specify a border width of 0 then no border will be there, but if you specify a 0px border, then a border of 0 px will be created (that's the idea behind it, in reality 0px gives the exact same result like 0).

这意味着如果你指定边框宽度为0,那么没有边框,但如果你指定一个0px边框,那么将创建一个0 px的边框(这就是它背后的想法,实际上0px给出了完全相同的结果喜欢0)。

Further Points

  • unit-less 0 makes it easier to read as it is easily distinguishable from normal unit'ed values.
  • 无单位0使其更容易阅读,因为它很容易与正常的单位值区分开来。
  • It makes sense to remove the units as they have no point in being there (0 could mean size, color, etc.).
  • 删除单位是有意义的,因为它们没有任何意义(0可能意味着大小,颜色等)。

Conclusion: Omit the units in 0. They're not needed and confusing.

结论:将单位省略为0.它们不需要和混淆。

#2


12  

Css specifications says that 0<unit> can be written 0. And I've read many times that it is suggested to omit the unit, I can't remember where.

Css规范说0 可以写成0.而且我已多次读过它建议省略单位,我不记得在哪里。

So omit them.

所以省略它们。

See http://www.w3.org/TR/CSS2/syndata.html#length-units

请参见http://www.w3.org/TR/CSS2/syndata.html#length-units

#3


2  

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.

我说省略单位,这将有助于压缩CSS并提高性能。它并不多,但每一点点都有帮助。

#4


2  

I also like to put units by my zeros because ...

我也喜欢用我的零点放单位因为......

  1. IDEs like to have the values there so they can increment them properly, as Istvan mentioned.
  2. 正如Istvan所说,IDE喜欢在那里拥有值,以便他们可以正确地增加它们。
  3. I don't have to type the unit later if I put another value in. And I don't have to try and remember to remove the unit if it's zero or put it back if it's not.
  4. 如果我放入另一个值,我不必在以后输入单位。而且我不必尝试记住如果它为零则移除单位或者如果不是则将其取回。
  5. One poster said the naked zero was more readable. I find that it's not. When there is a unit, you have context, and that makes it more readable. For example, if I come across a percent sign, I know I use that in different ways than the "px" unit, so I instantly know some of the things it could be.
  6. 一张海报说裸露的零点更具可读性。我发现事实并非如此。当有一个单元时,你有上下文,这使它更具可读性。例如,如果我遇到百分号,我知道我以不同于“px”单位的方式使用它,所以我立即知道它可能是的一些东西。
  7. Another guy, "red", had this to say, which looks like good info. Here's a snippet...

    However, you will notice it if you intended to change a prior value in the cascade. a '0em' says, 'change the prior value to '0em', but a zero may simply say, 'disregard this rule' and leave the prior rule in effect. This may not be at all what you intended with your naked '0'. original article link

    但是,如果您打算更改级联中的先前值,您会注意到它。 '0em'表示,'将先前值更改为'0em',但零可以简单地说,'忽略此规则'并使先前的规则生效。这可能与你裸体'0'的意图完全不同。原文链接

#5


2  

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 setting 1, 2... without a unit has no meaning.

在当前的浏览器开发人员工具中,您可以使用向上/向下光标键轻松调整值,但如果您指定0而没有单位,那么它将无法工作,因为浏览器不知道您更喜欢哪个单位,并设置1 ,2 ......没有单位没有意义。

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.

这就是为什么我现在总是在零值上指定单位。任何CSS缩小器都会在缩小时将其更改为0,因此您甚至不需要关心它。

#6


1  

I always omit as it seems easier to read, the zero pops out and is easily distinguishable from the with-unit values around it.

我总是省略,因为它似乎更容易阅读,零弹出并且很容易与周围的单位值区分开来。

#1


56  

I argue you should also omit the units.

我认为你也应该省略单位。

From a programmer's perspective, 0 == null == none == false, where 0px == 0px only.

从程序员的角度来看,0 == null == none == false,其中0px == 0px。

Which means that if you specify a border width of 0 then no border will be there, but if you specify a 0px border, then a border of 0 px will be created (that's the idea behind it, in reality 0px gives the exact same result like 0).

这意味着如果你指定边框宽度为0,那么没有边框,但如果你指定一个0px边框,那么将创建一个0 px的边框(这就是它背后的想法,实际上0px给出了完全相同的结果喜欢0)。

Further Points

  • unit-less 0 makes it easier to read as it is easily distinguishable from normal unit'ed values.
  • 无单位0使其更容易阅读,因为它很容易与正常的单位值区分开来。
  • It makes sense to remove the units as they have no point in being there (0 could mean size, color, etc.).
  • 删除单位是有意义的,因为它们没有任何意义(0可能意味着大小,颜色等)。

Conclusion: Omit the units in 0. They're not needed and confusing.

结论:将单位省略为0.它们不需要和混淆。

#2


12  

Css specifications says that 0<unit> can be written 0. And I've read many times that it is suggested to omit the unit, I can't remember where.

Css规范说0 可以写成0.而且我已多次读过它建议省略单位,我不记得在哪里。

So omit them.

所以省略它们。

See http://www.w3.org/TR/CSS2/syndata.html#length-units

请参见http://www.w3.org/TR/CSS2/syndata.html#length-units

#3


2  

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.

我说省略单位,这将有助于压缩CSS并提高性能。它并不多,但每一点点都有帮助。

#4


2  

I also like to put units by my zeros because ...

我也喜欢用我的零点放单位因为......

  1. IDEs like to have the values there so they can increment them properly, as Istvan mentioned.
  2. 正如Istvan所说,IDE喜欢在那里拥有值,以便他们可以正确地增加它们。
  3. I don't have to type the unit later if I put another value in. And I don't have to try and remember to remove the unit if it's zero or put it back if it's not.
  4. 如果我放入另一个值,我不必在以后输入单位。而且我不必尝试记住如果它为零则移除单位或者如果不是则将其取回。
  5. One poster said the naked zero was more readable. I find that it's not. When there is a unit, you have context, and that makes it more readable. For example, if I come across a percent sign, I know I use that in different ways than the "px" unit, so I instantly know some of the things it could be.
  6. 一张海报说裸露的零点更具可读性。我发现事实并非如此。当有一个单元时,你有上下文,这使它更具可读性。例如,如果我遇到百分号,我知道我以不同于“px”单位的方式使用它,所以我立即知道它可能是的一些东西。
  7. Another guy, "red", had this to say, which looks like good info. Here's a snippet...

    However, you will notice it if you intended to change a prior value in the cascade. a '0em' says, 'change the prior value to '0em', but a zero may simply say, 'disregard this rule' and leave the prior rule in effect. This may not be at all what you intended with your naked '0'. original article link

    但是,如果您打算更改级联中的先前值,您会注意到它。 '0em'表示,'将先前值更改为'0em',但零可以简单地说,'忽略此规则'并使先前的规则生效。这可能与你裸体'0'的意图完全不同。原文链接

#5


2  

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 setting 1, 2... without a unit has no meaning.

在当前的浏览器开发人员工具中,您可以使用向上/向下光标键轻松调整值,但如果您指定0而没有单位,那么它将无法工作,因为浏览器不知道您更喜欢哪个单位,并设置1 ,2 ......没有单位没有意义。

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.

这就是为什么我现在总是在零值上指定单位。任何CSS缩小器都会在缩小时将其更改为0,因此您甚至不需要关心它。

#6


1  

I always omit as it seems easier to read, the zero pops out and is easily distinguishable from the with-unit values around it.

我总是省略,因为它似乎更容易阅读,零弹出并且很容易与周围的单位值区分开来。