Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 987
Uri Avatar asked Oct 27 '11 22:10

Uri


People also ask

Can 0 have units?

In the case of zero, in the mathematics of integer numbers or real numbers or any mathematical frame, no units are necessary. Mathematically the number zero is completely defined.

What units should I use in CSS?

There's no real right or wrong, but as a rule of thumb: For anything you want a certain, fixed size, use PX. For anything you want to scale with font-size, use EM. For anything you want to scale to the available space in the window/container, use %

What is 0em in CSS?

0 and 0em are the same, but any value other than 0 for x and xem are not the same. Follow this answer to receive notifications.


1 Answers

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.

like image 137
Madara's Ghost Avatar answered Oct 17 '22 05:10

Madara's Ghost