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
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.
I also like to put units by my zeros because ...
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
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.
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
I always omit as it seems easier to read, the zero pops out and is easily distinguishable from the with-unit values around it.
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.