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

后端 未结 8 1049
旧巷少年郎
旧巷少年郎 2020-12-16 09:13

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\' an

相关标签:
8条回答
  • 2020-12-16 09:35

    The CSS specification says (quote):

    After a zero length, the unit identifier is optional.

    And I've read many times that it is suggested to omit the unit, I can't remember where.

    So omit them.

    0 讨论(0)
  • 2020-12-16 09:36

    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

    0 讨论(0)
  • 2020-12-16 09:37

    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 讨论(0)
  • 2020-12-16 09:38

    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

    0 讨论(0)
  • 2020-12-16 09:40

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

    0 讨论(0)
  • 2020-12-16 09:42

    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.

    0 讨论(0)
提交回复
热议问题