I want to know what the em
unit is, and how much 1em is when converted to pixels (px
). I also read somewhere about some IE bug, to overcome which body font-size should be set to something, but couldn't follow much. Can somebody explain that in detail?
相关问题
- Adding a timeout to a render function in ReactJS
-
Why does the box-shadow property not apply to a
- Add animation to jQuery function Interval
- jQuery hover to slide?
- Issue with star rating css
em is almost the same as percentage, I suggest to read http://kyleschaeffer.com/best-practices/css-font-size-em-vs-px-vs-pt-vs/
this link can help you http://jontangerine.com/log/2007/09/the-incredible-em-and-elastic-layouts-with-css
As quoted from that article:
The M-principle that an em is based on the letter M and is dependent on font is an often stated myth!! W3c em documentation very succinctly describes exactly how ems and pixels relate. Using the letter M to compute font-sizes is at the very least overly complicated and unnecessary.
Here are the salient points.
Without ancestor magnification, 1em is exactly equal to the pixel font-size attribute.
Ancestor magnification with x-ems or x-percent means you just multiply by the obvious ratios x or x/100. Thus a simple java-script loop will calculate exact font sizes, assuming: (1) no C.S.S to frustrate java-script; and (2) some ancestor has it's font size set in absolute units. This is the computed value the documentation is talking about. A hand calculation can get around C.S.S., but an absolute unit must still be found in the ancestor chain.
Since ems measure width you can always compute the exact font size by creating a div that is 1000 ems long and dividing its client-Width property by 1000. Since ems are rounded to the nearest thousandth you need 1000 ems to avoid erroneous pixel truncation.
You probably can create a font where the M-principle fails since em is based on the font-size attribute not on the actual font. Suppose you have a weird font where M is 1/3 the size of the other characters and you have a font size of 10 pixels. Don't you think the pixel font-size guarantees maximal character height in some way and so the M will not be 10 pixels and all other characters 30 pixels?
Warning
From "The amazing em unit and other best practices" https://www.w3.org/WAI/GL/css2em.htm
EM is a unit of measurement proportional to the point size of a font.
em is a measurement used in printed typography originally (as per wikipedia). Here are some things to consider: if em is defined as 12pt type, then an em is 12/72 inch of a printed page; but if you define an em as 16px, its width in inch is dependent on the resolution of the media. (Note: 72ppi to 120ppi resolution used to be considered a "safe browser" standard before the advent of mobile devices.) So, when your users are on a 300px width device, 1em as 16px is a lot of "real estate". Best use of em measurements, in my opinion, are defining paragraphs of texts to balance white space effectively, not for page layout or positioning.