2

Does it matter if left is specified as left:0; or left:0px;? As far as I can tell it works either way without Firefox's error console complaining, but just wondering if they have the exact same meaning.

I'm writing some JavaScript that will be moving items and also making them expand / contract. I'm guessing that the answer to the above will also answer if height:0; is the same as height:0px;.

asimes
  • 5,749
  • 5
  • 39
  • 76

4 Answers4

2

Well, it's the same, but only in case of zero. Zero is zero, no matter what the unit.

If you specify dimension of the non-zero value, you have to specify a unit as well.

This is the left property spec:

'left'
Value:      <length> | <percentage> | auto | inherit

As you can see, it has a value of <length>.

Now, for the length (from the spec):

Lengths refer to distance measurements and are denoted by in the property definitions. A length is a dimension. A zero length may be represented instead as the ‘0’. (In other words, for zero lengths the unit identifier is optional.)

kamituel
  • 34,606
  • 6
  • 81
  • 98
2

If you put 0 inside some property in CSS. 0 hasn't unit. It's just zero and still be interpreted as 0 by browsers.

But if you put value > 0 you have to specify unit.

Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
1

For CSS, whatever the unit 0 is always 0. There is no reason to add this unit.

The spec makes it clear :

After a zero length, the unit identifier is optional.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • Thanks, asking because in the JavaScript I would be writing `myElement.style.left = foo+"px";`. – asimes Apr 02 '13 at 07:35
0

No it doesn't matter.

left:0px;
left:0%;

Is all equal to 0.

Johan Sundén
  • 447
  • 4
  • 9