Reputation: 89859
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.
Upvotes: 50
Views: 14712
Reputation: 4758
As a note to this question: Unitless 0 as length won't work in calc()
and other math functions. You have to write 0px
. And things would be more buggy if the unit omitted 0 is taken from some css variable.
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(300px + 0px); border-color: #f00; }
.b { width: calc(300px + 0); border-color: #330; }
<div class="a">width: calc(300px + 0px);</div>
<div class="b">width: calc(300px + 0);</div>
I would suggest always use 0px
when you are writing CSS variables. So it won't make you and others confuse when they are trying to use the variable in some calc()
functions.
Upvotes: 23
Reputation: 372
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
Upvotes: 1
Reputation: 5594
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.
Upvotes: 15
Reputation: 3247
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.
Upvotes: 2
Reputation: 443
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
Upvotes: 3
Reputation: 175098
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
).
0
makes it easier to read as it is easily distinguishable from normal unit'ed values.Conclusion: Omit the units in 0. They're not needed and confusing.
Upvotes: 71
Reputation: 11100
I always omit as it seems easier to read, the zero pops out and is easily distinguishable from the with-unit values around it.
Upvotes: 1
Reputation: 7863
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.
Upvotes: 2