Why em instead of px?

后端 未结 15 1260
闹比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

    There is a simple solution if you want to use px to specify font size, but still want the usability that em's provide by placing this in your CSS file:

    body {
      font-size: 62.5%;
    }
    

    Now specify you p (and other) tags like this:

    p {
      font-size: 0.8em; /* This is equal to 8px */
      font-size: 1.0em; /* This is equal to 10px */
      font-size: 1.2em; /* This is equal to 12px */
      font-size: 2.0em; /* This is equal to 20px */
    }
    

    And so on.

提交回复
热议问题