CSS difference between 0 and 0em

2020-03-08 08:56发布

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.

标签: css
13条回答
太酷不给撩
2楼-- · 2020-03-08 09:19

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.

查看更多
可以哭但决不认输i
3楼-- · 2020-03-08 09:21

Google's own Chrome browser in the 'Computed' style includes units with zero values which aligns with my personal preference to just include it.

Include Units With Zero Values

查看更多
不美不萌又怎样
4楼-- · 2020-03-08 09:22

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.)

查看更多
地球回转人心会变
5楼-- · 2020-03-08 09:24

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.

查看更多
该账号已被封号
6楼-- · 2020-03-08 09:25

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!

查看更多
小情绪 Triste *
7楼-- · 2020-03-08 09:25

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.

查看更多
登录 后发表回答