Font size in CSS - % or em?

后端 未结 8 1800
走了就别回头了
走了就别回头了 2020-11-28 02:24

When setting the size of fonts in CSS, should I be using a percent value (%) or em? Can you explain the advantage?

相关标签:
8条回答
  • 2020-11-28 02:32

    There's a really good article on web typography on A List Apart.

    Their conclusion:

    Sizing text and line-height in ems, with a percentage specified on the body (and an optional caveat for Safari 2), was shown to provide accurate, resizable text across all browsers in common use today. This is a technique you can put in your kit bag and use as a best practice for sizing text in CSS that satisfies both designers and readers.

    0 讨论(0)
  • 2020-11-28 02:36

    The real difference comes apparent when you use it not for font-sizes. Setting a padding of 1em is not the same as 100%. em is always relative to the font-size. But % might be relative to font-size, width, height and probably some other things I don't know about.

    0 讨论(0)
  • 2020-11-28 02:39

    Given that (nearly?) all browsers now resize the page as a whole, rather than just the text, previous issues with px vs. % vs. ems in terms of accessible font resizing are rather moot.

    So, the answer is that it probably doesn't matter. Use whatever works for you.

    % is nice because it allows for relative resizing.

    px is nice because it's fairly easy to manage expectations when using it.

    em can be useful when also used for layout elements as it can allow for proportional sizing related to the text size.

    0 讨论(0)
  • 2020-11-28 02:47

    Both adjust the font-size relative to what it was. 1.5em is the same as 150%. The only advantage seems to be readability, choose whichever you are most comfortable with.

    0 讨论(0)
  • 2020-11-28 02:47

    Regarding the difference between the css units % and em.

    As far as I understand (at least theoretically/conceptually, but possibly not how these two units might be implemented in browsers) these two units are equivalent, i.e. if you multiply your em value with 100 and then replace em with % it should be the same thing ?

    If there actually is some real difference between em and % then can someone explain it (or provide a link to an explanation) ?

    (I wanted to add this comment of mine where it would belong, i.e. indented just below the answer by "Liam, answered Sep 25 '08 at 11:21" since I also want to know why his answer was downvoted, but I could not figure out how to put my comment there and therefore had to write this "thread global" reply)

    0 讨论(0)
  • 2020-11-28 02:49

    From http://archivist.incutio.com/viewlist/css-discuss/1408

    %: Some browsers doesn't handle percent for font-size but interprets 150% as 150px. (Some NN4 versions, for instance.) IE also has problems with percent on nested elements. It seems IE uses percent relative to viewport instead of relative to parent element. Yet another problem (though correct according to the W3C specs), in Moz/Ns6, you can't use percent relative to elements with no specified height/width.

    em: Sometimes browsers use the wrong reference size, but of the relative units it's the one with least problems. You might find it interpreted as px sometimes though.

    pt: Differs greatly between resolutions, and should not be used for display. It's quite safe for print use though.

    px: The only reliable absolute unit on screen. It might be wrongly interpreted in print though, as one point usually consist of several pixels, and thus everything becomes ridiculously small.

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