tags:

views:

499

answers:

10

Is there any difference between

margin: 0;

and

margin: 0em;

I didn't notice anything, but my teacher keeps telling me that it's not the same.

edit: I've shown my teacher your answers, and his response was like "uhm, but you should use 0em; because it saves you time when you want to change it to something like 0.5em; and that it's a good practice and I should do it", and I was like "yeah whatever..."

+31  A: 

There's no difference when the value is 0. If it were 1 and 1em, yes, there's a difference, but 0 is 0, no matter the unit.

Chris Doggett
and no difference with 0 "whatever". Every units in CSS are defined so that 0 is nothing: 0%, 0cm, 0in, 0pt, etc.
Vincent Robert
This is true for apples and elephants too.
DrG
So if you have a value of zero you should always omit the units because they are redundant.
Matthew James Taylor
@Matthew: In CSS, yes, unless size is no concern and you really want to be specific. It's really a matter of preference. Anywhere but CSS, like Euro said in the comments to the original question, you can get into trouble if you omit the units.
Chris Doggett
+4  A: 

Zero is zero is zero. Regardless of whether the units are different. The derived value in pixels is always same: zero.

Gumbo
+8  A: 

It's like saying that 0 bananas is not the same as 0 apples. So in a way, he's correct, but zero is zero, so either way you'll end up hungry!

PatrikAkerstrand
Well, to be picky -- normally, 0 bananas IS NOT the same as 0 apples (oh, for example: var a = new List<Banana>() is not the same as var b = new List<Apple>() ). But in this case they are the same because we're not really counting apples or bananas, nor em's or px's; we're counting DISTANCE in UNITS of em's or px's. It's like if the question was "what's the capacity of a crate in banana units or apple units. Then, '0' really is the same as '0 bananas' and '0 apples'.
Euro Micelli
I do say that he's correct...
PatrikAkerstrand
+12  A: 

The file is bigger with 0em.


The way to prove to your professor that there is no difference is to set something to 0 and something else to 0em and inspect the DOM with Firebug or a similar tool. If I recall correctly, you'll see that both are now 0. So the only difference is in the source. (But it's been a while since I checked. I might be thinking of getting and setting styles from jQuery. So check it out before you take my word.)

Nosredna
Hehe, true. An "em" splattered css file is slightly larger than the plain "0" one.
Kriem
+3  A: 

Your teacher should be fired

Andrew G. Johnson
+5  A: 

I wonder if the teacher is trying to highlight, not so much that 0 and 0em are not the same value but perhaps, that the default unit in CSS may not be em (I believe the spec demands that non-zero values have units attached but often assume px).

On the specific example obviously they are equal.

Lazarus
+3  A: 

Your teacher, it appears, is an idiot.

But maybe appearances are deceptive: you should ask your teacher what they meant.

I have to ask, why didn't you ask your teacher what they meant when they first said it? Zero is zero, no matter what the units.

AmbroseChapel
Yeah I'd love to know what the prof was trying to get at. Report back!
Nosredna
see edit for teacher's response :)
Darth
A: 

0 and 0em is the same but any value other than 0 for x and xem is not the same

Rony
+1  A: 

your teacher is being stubborn in that he or she is unwilling to admit to being wrong. 'whatever: 0' sets all possible units, inherited or otherwise, to zero. 0 is most definitely zero.

+1  A: 

There certainly is a difference between a '0' with or without an associated sizing unit (em, px, etc.).

While CSS, in most cases, interprets '0' without a unit size as a default zero-unit for that attribute, it is not always the case (font-size, for example) and some validators will object to properties which require unit sizes in their specification when they encounter a parameter without that unit included.

The difference is that '0', without a unit designation may be interpreted as an expression error in properties where unit size is required. You may not notice the difference since the expression will simply be regarded as 'bad syntax' and the interpreter will skip it; as it normally does with any bad syntax in CSS. So if you specified the '0' as a unit size and the default is '0em' or '0%' etc. then the result will remain '0' for that attribute. 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'.

This is a client-side process and you have no control how a particular browser, now or in the future, may handle '0' and default interpreting.

So, besides telling whomever reads your code that you definitely intended to have a '0' sized element, and what type of unit is appropriate, it may actually effect the result. Better to always use the unit designation with your zeros. If you're really fussy (as I am) or design vls systems and code (as I have), you may also wish to put a leading zero (0.xx...) in any values less than 0. This permits later readers to know that your fraction was deliberate and not simply a typo.

As for those who complain about 'extra typing' - I suggest you go take a typing class. One of the first things I do when interviewing designers or coders for a job is give them a typing test. There's nothing like a good typist to save money and produce fewer errors!