4

When defining size in margin/padding etc in CSS I often miss out 'px' for the value '0' e.g.

.myClass {
  5px 0 0 5px
}

I have recently been told by someone I work with to include 'px'

.myClass {
  5px 0px 0px 5px
}

Is there any advantages or disadvantages or either approach? or is this just preference?

Lizard
  • 43,732
  • 39
  • 106
  • 167

2 Answers2

5

No disadvantages, you can use 0 without "px".

A zero length may be represented instead as the ‘0’. (In other words, for zero lengths the unit identifier is optional.)

http://www.w3.org/TR/css3-values/#lengths

Edit: also this question have been asked already

Community
  • 1
  • 1
antejan
  • 2,594
  • 12
  • 15
  • I would agree with this. There is no difference. I personally however actually do use `0px` in most cases (i find it makes it easy to work with rendered pages in tools like Firebug where I might want to pop a 1 or something in front of it when editing in the CSS panel, when trying out various margins, padding, etc. – Mike Brant Jan 14 '13 at 22:59
  • 1
    @MikeBrant This might be true, you have some developing comfort, if you do only have to change the value, but for a final version, i would use a tool like http://refresh-sf.com/yui/ to compress the css (this tool does remove units from zero-values by the way) – r3bel Jan 14 '13 at 23:06
  • @r3bel Good comment. Always a good idea to use a CSS compressor, particularly if you are working on an application where very byte matters. – Mike Brant Jan 15 '13 at 00:34
0

Here is a brief overview about units in css, very useful as i think:

css units (w3c)

in general it is good to declare which unit you are using, while 0 is always the same, so you dont have to write the unit if you you have a 0-Value (zero) and there is no advantage if you do write the unit

(to the contrary, your css file is 2 bytes larger for each unnessecary unit you write, if not optimized :D)

r3bel
  • 1,350
  • 8
  • 12