Forum Moderators: not2easy
When you look at this page in IE6, you'll see that em and % appear to do exactly the same thing:
[hpaa.com ]
They both set your font size to something relative to the browser default size.
They both inherit from the parent element(s).
So is there any real difference between % and em?
Does browser support vary (significantly)?
Which should we be using?
[hpaa.com ]
to demonstrate the claim that "1em = 100%". But when I look
at that same page, I seem to see that the page actually shows
the opposite!
I wasted a lot of time using ems that didn't work as expected,
reasoning about the CSS beginning from this claim. But
there does seem to be an easy way to make the claim become true.
Here's the full text of the sample page:
<HTML>
<HEAD>
<TITLE>Specifying type in ems and percent</TITLE>
<STYLE TYPE="text/css">
BODY {margin-left: 5% }
.m1 {font-size: 1em }
.m12 {font-size: 1.2em }
.m15 {font-size: 1.5em }
.m2 {font-size: 2em }
.m1p {font-size: 100% }
.m12p {font-size: 120% }
.m15p {font-size: 150% }
.m2p {font-size: 200% }
</STYLE>
</HEAD>
<BODY>
<H3>1em = 100% = font size of parent element.</H3>
<P>This page shows the value of relative sizing. If you change the default font size on your browser, the relative font sizes below should change relatively. The font at 2em and 200% should be exactly twice the size of the default font. IE 3.x and early 4.x Netscape Navigator versions do not display this page correctly.<BR>
<P>This is the default font at the default size.
<P class=m1>This is the default font at 1em.
<P class=m1p>This is the default font at 100%.
<P class=m12>This is the default font at 1.2em.
<P class=m12p>This is the default font at 120%.
<P class=m15>This is the default font at 1.5em.
<P class=m15p>This is the default font at 150%.
<P class=m2>This is the default font at 2em.
<P class=m2p>This is the default font at 200%.
</BODY>
</HTML>
HOWEVER, if I change the specification for BODY to
BODY {
font-size: 100%;
margin-left: 5% }
This is very mysterious, but it does seem to work. On a large
site, I set the font-size to 100% in the BODY, and then used
only ems for all the DIV's, and Hn's, and P's, and all the other
places I wanted font-sizes. With the BODY {font-size:100%} in
place, all the ems seemed to work and nest just as expected,
observing the desired "1em=100%".
Can anyone explain the effect of including BODY {font-size:100%}
to change the interpretation of em sizes throughout the page?
(Or, are there other factors that make the sample page look
different to different observers?)
FWIW, I fired up the Phoenix browser from Mozilla, and I don't see any size differences between em and % there, no matter what the body tag says.
WinXP, Mozilla 1.21.... : 1.0em = 100% OK
WinXP, IE6, ("quirks"). : 1.2em = 100% BROKEN
WinXP, IE6, (standards).: 1.2em = 100% BROKEN
WinXP, Opera 6.0.5......:~0.8em = 100% BROKEN
Win98, IE5..............: 1.2em = 100% BROKEN
Win98, Netscape 4.79....:~1.1em = 100% BROKEN
So unless there is something strange in my browsing,
almost no browser shows the sample as "1em = 100%".
I then made a copy of the source and added "font-size:100%"
to the BODY style, and tried the same experiments again:
WinXP, Mozilla 1.21.....: 1.0em = 100% OK
WinXP, IE6, ("quirks")..: 1.0em = 100% FIXED
WinXP, IE6, (standards).: 1.0em = 100% FIXED
WinXP, Opera 6.0.5......:~0.8em = 100% BROKEN
Win98, IE5..............: 1.0em = 100% FIXED
Win98, Netscape 4.79....:~1.1em = 100% BROKEN
So it appears that 1em = 100% is true on Mozilla (as tedster
reported), and can be fixed to be true on IE5 and IE6 by adding
the "100%" sizing to the BODY. But Opera 6 remains unmodified
(I mis-reported this earlier). NN4 changes the text sizes it
displays, but 1em still does not equal 100%.
[edited by: Nick_W at 1:23 am (utc) on Dec. 29, 2002]