Why em instead of px?

后端 未结 15 1267
闹比i
闹比i 2020-11-22 06:38

I heard you should define sizes and distances in your stylesheet with em instead of in pixels. So the question is why should I use em instead of px when defining styles in c

15条回答
  •  执笔经年
    2020-11-22 07:32

    Because the em (http://en.wikipedia.org/wiki/Em_(typography)) is directly proportional to the font size currently in use. If the font size is, say, 16 points, one em is 16 points. If your font size is 16 pixels (note: not the same as points), one em is 16 pixels.

    This leads to two (related) things:

    1. it's easy to keep proportions, if you choose to edit your font sizes in your CSS later on.
    2. Many browsers support custom font sizes, overriding your CSS. If you design everything in pixels, your layout might break in these cases. But, if you use ems, these overridings should mitigate these problems.

提交回复
热议问题