I was reading now about how it’s a good practice to set the font on <body>
to 62.5%, so that later you can use the divide-by-10 conversion from pixel开发者_开发知识库 units.
But I was wondering: why not set <body>
to 6.25%? Then you can use the same dimensions for em units as for pixel units, assuming the default browser font size is 16 pixels.
E.g.
body {
font-size: 6.25%; /* 1px */
}
p {
font-size: 12em; /* 12px */
}
Two issues.
I seem to remember that if you set your initial font size quite small using a relative unit like ems, if the user resizes the text Internet Explorer, the font size will change quite a lot between the settings.
It’s an odd phenomenon, and I’m not sure if it still occurs in IE, nor if you’re worried about users in IE who change the font size.
You’re potentially condemning yourself to re-setting the font size every time you nest elements.
An answer I wrote to another question tries its best to explain this, but in short, if you’re doing your font sizes in ems, you’re better off using
font-size
as little as possible, rather than making it ever-so-slightly easier to usefont-size
when you do.For example: say you want all
<li>
s on the site to use a font size of 12 pixels.li { font-size: 12em;/* 12px */ }
If you then use the following HTML:
<ul> <li> <ul> <li> ...
Then you need to do this:
li li { font-size: .083em;/* 12px */ }
Otherwise the inner
<li>
s will be 12 pixels * 12 pixels = 144 pixels!Ems aren’t pixels. Ems refer to a percentage of the nearest ancestor’s font size, whereas pixels refer to actual pixels. In my opinion, trying to turn ems into pixels is more confusing than the alternative. You’re better off setting
<body>
to the most commonly used font size on the site, only changing from that when you need to, and putting the intended size in pixels in a comment after your em-based declaration. (That way, it’s easier to tell later if you’ve got something wrong.)(Of course, the only reason we avoid pixels is because IE doesn’t change the size of text sized in pixels when the user changes the font size in the browser. If you’re not worried about that, then just use pixels.)
Short answer: Don't do this. Use px
to set font sizes. If you need a bigger font, use a larger number of px
.
Long answer:
The idea behind setting the base font is make it obvious how big a em
s and ex
s are. If your base font size is 10px
, then 1.0em
is 10px
, 1.2em
is 12px
, etc. It seems simple enough. The idea breaks down as soon as a container changes the font size from the base. If you're in a div
with the font size set to 20px
, then all the sudden 1em
is twice as big as it used to be.
It gets worse. People started suggesting that 62.5%
should be used rather than 10px
.
(Note that 16 * 0.625 = 10
). This was suggested because specifying a percentage for font size is a workaround for an issue with old versions of Internet Explorer. The end user couldn't "zoom in" unless you used a percentage.
However, %
(when applied as the default font) is an absolute unit. It may seem like a relative unit, but here, it's a percentage of the user agent's default font size, and font sizes are specified in points. This introduces a subtle and incorrect assumption about the user agent, namely that the screen resolution is 96 dpi. This assumption often results in text which looks like this:
Hey check me out, I'm too small to read!
To sum up:
Don't use hacks for old versions of IE (4 years is plenty of time to maintain backward compatibility; IE7 is four years old this month).
Don't make assumptions about your user agent's resolution, and don't set the body font size to a percentage.
If you need more precise typographical control, use a CSS framework like Baseline.
If you are doing this, you will have to make sure you resize every font on the page. If you miss anything, it will be tiny by default (1px).
Resizing every font may be more daunting then you imagine, because when you use relative font sizes, you have to be very specific.
精彩评论