Margin and padding using em

后端 未结 2 991
难免孤独
难免孤独 2021-02-18 23:21

When 1em is applied to an element, it takes the default value of the browser (usually, 16px) or the font-size value of its parent, right?. But I noticed that if I use something

相关标签:
2条回答
  • 2021-02-19 00:05

    When 1em is applied to an element, it takes the default value of the browser (usually, 16px) or the font-size value of its parent, right?

    No, it takes its own font-size, computed based on its parent (or the default browser-supplied value). Since the browser-supplied font-size of h1 is 32 pixels, the resultant margin is 32 pixels.

    However, using something like font-size: 100%; solves the discrepancy.

    By setting font-size: 100%; or font-size: 1em; on an element, you're telling it to use 100% of the font size of its parent, so setting 1em as a length on anything else will follow that 100%.

    0 讨论(0)
  • 2021-02-19 00:13

    1em is equal to the font-size of the element in question. So when using it with margins, it will be equivalent to the font-size of the element you're applying the margin too.

    0 讨论(0)
提交回复
热议问题