When specifying a 0 value in CSS, should I explicitly mark the units or omit?

核能气质少年 提交于 2019-11-27 17:06:22

问题


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.


回答1:


I argue you should also omit the units.

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

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).

Further Points

  • unit-less 0 makes it easier to read as it is easily distinguishable from normal unit'ed values.
  • It makes sense to remove the units as they have no point in being there (0 could mean size, color, etc.).

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




回答2:


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.

So omit them.

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




回答3:


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.




回答4:


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. 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.
  3. 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.
  4. 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




回答5:


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.

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.




回答6:


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




回答7:


As a note to this question: unitless 0 as length won't work in calc(), you have to write 0px instead.

Specification:

Note: Because <number-token>s are always interpreted as <number>s or <integer>s, "unitless 0" <length>s aren’t supported in math functions. That is, width: calc(0 + 5px); is invalid, because it’s trying to add a <number> to a <length>, even though both width: 0; and width: 5px; are valid.

.a, .b { border: 10px solid; height: 30px; }
.a { width: calc(200px + 0px); border-color: #f00; }
.b { width: calc(200px + 0); border-color: #330; }
<div class="a"></div>
<div class="b"></div>

And things would be more buggy if the unit omitted 0 is taken from some css variable.



来源:https://stackoverflow.com/questions/7923042/when-specifying-a-0-value-in-css-should-i-explicitly-mark-the-units-or-omit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!