Should I set the default font-size on the body or html element?
Solution 1
Now that the rem
unit is starting to become popular, setting the base font-size on the root element (html tag) is advised (rem
stands for root em).
Solution 2
I don't believe there is any advantage or disadvantage to setting the base font-size
on either html
or body
to allow for sizing with ems; they will both have the same effect.
Not related to the question:
I would however suggest using a different default font-size
. I would set it as:
body {
font-size: 62.5%;
}
Doing this reduces the default font-size
from 16px
down to 10px
. This then makes choosing font-size
much easier as there is no need for difficult calculations. It means that 1em
is equal to 10px
and so calculating the px
size is a matter of multiplying by 10:
1.0em
=10px
1.4em
=14px
1.8em
=18px
2.2em
=22px
Solution 3
If you really want to follow the rules and still keep flexibility, you should consider this:
html
's font-size is the root font-size, which means it will be used as a base for rem calculation, but that's it, nothing else. It shouldn't be used for real text size calculation: it's just a kind of trick for some browsers.body
's font-size is the text's default font-size: all your text should have this size, unless overriding definition- special elements should have sizes in rem, with a fallback in pixels
So that is how it looks in CSS:
html {
font-size: 100% /* or 62.5% or whatever you like, it doesn't matter, it's just a browser fix, however "rem" units will be based on that value, so make it confortable for calculations */
}
body {
font-size: 0.75em; /* That is your text's default font size. Here i chose 12px */
}
h1 { /* or whatever element you want to change the font size of */
font-size: 20px; /* for browsers that don't understand the "rem" unit */
font-size: 1.25rem; /* which equals to 20px if html's font-size was set to 100% */
}
Note that, while all page's elements should inherit from the body
definition, you could use an all-tag-inclusive definition instead, like often seen in HTML resets:
a,
html, body, div, span, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
abbr, address, cite, code,
del, dfn, em, img, ins, kbd, q, samp,
small, strong, sub, sup, var,
b, i,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, figcaption, figure,
footer, header, hgroup, menu, nav, section, summary,
time, mark, audio, video {
font-size: 0.75rem;
}
I don't recommend this reset however. Standards are made to help you avoid this kind of tricks.
Comments
-
joshnh over 4 years
I like to work in
em
s when creating websites. Therefore I set a defaultfont-size
of100.01%
on thebody
element.My question is should I set the default
font-size
on thebody
or thehtml
element? What are the pros and cons (if any) of both? -
joshnh almost 13 yearsThe mathematics isn't the issue, no matter what the body font-size is the numbers are still going to get messy when it comes to line-height and margin-bottom. Thanks for the answer though.
-
tw16 almost 13 years@joshuanhibbert: The maths part was a recommendation as an addition to my main answer i.e. there is no advantage/disadvantage to choosing one over the other.
-
joshnh almost 13 yearsJust a little bit of info: if sizing in rems you will need to set the default font-size on the html element, as rems uses the root.
-
tw16 almost 13 years@joshuanhibbert: That is correct, but as you only asked about ems I decided to stay away from talking about rems. However, using the above maths trick in conjunction with rems is even better, as the figures are far more consistent. If you do chose to use rems though, make sure to set a similar px
font-size
first in the css rule (much easier to work out with the maths above), because if rems are not supported it will fall back to the pxfont-size
. -
Bill Rosmus almost 12 yearsCareful with REM and IE9. I tried using it on more than just fonts and IE did not recognize REM the same way that all the other browsers did. Looked terrible.
-
bernk over 11 yearsJust out of curiosity, why would you set the body font-size to 62.5% to get 10px assuming the browser's default is in fact 16px...when you could just set it to 10px? Then you could still use rems based on this 10px. No?
-
BoltClock over 10 yearsRemember that if you need to provide a fallback in
px
orem
, it's as simple as specifying an extra declaration such that you have the fallback value preceding therem
value. Of course, the complexity lies in having to calculate the absolute value you would need, which would otherwise have been obviated by the use of therem
unit. -
Satya Prakash over 10 years@bernk wont that will make it unresponsive with px value?
-
notapatch over 10 yearsCSS Wizard wrote an article against using 62.5% unless you actually want a base font of 10px. "The main reason, I feel, behind using the 62.5% method is laziness, and that’s a good thing. Good developers are lazy. However that laziness is misguided; it’s actually causing you more work. You have to define font-sizes on all elements rather than just once and letting them inherit and you have to tackle those horrible inheritance issues when an explicitly sized element is placed inside another one."
-
armen almost 10 yearsi would be grateful if there was a provided example.
-
Ninj over 9 yearsI'm convinced CSS Wizard is wrong here. See my answer for explanations.
-
nvreez over 9 yearsThis last all-tag suggestion makes
<a>
elements inside<p>
elements 75% smaller, which is (probably) not what you want. Similar behaviour for other nested elements. You could fix it withrem
, but I don't agree doing this all-tag "reset" in the first place. Just only set the<body>
tag and maybe addfont-size: inherit;
for the reset. -
Ninj about 9 yearsOops, had to be "rem". Note that i neither like this all-tag reset, but it's a good workaround sometimes for browser with strange behaviours (who said IE?). I'm correcting it, thank you.
-
Alex MM about 8 yearsHi @Ninj, short question, is there any reason why you set the
<body>
font-size
in em units instead of in rem units? I guess the result is the same cause<html>
is the parent of<body>
and<html>
is also de root element, but it may be more clear to use rems if you finally are using rems as your main size unit. Thanks! -
Ninj about 8 years@AlexMM As far as i remember (that's an old case), it's because "rem" is, precisely, refering to the body's font size, which would look like an infinite loop (even though it probably work in practice)
-
Alex MM about 8 years@Ninj hi!, according to the first paragraph of your explanation and also to the
rem
definition found in the MDN CSS length data type docu and also in the MDN font-size docu therem
unit (aka rootem
) is related to the root element size, which is the<html>
tag. It was just to know the reason why you did it in that way, but i believe in that case bothem
andrem
would make same result. -
Stephen M Irving over 4 yearswhat about this....
html { font-size: 62.5% } body { font-size: 160% }
This way, you can still make1rem == 10px
, but the size of 1em still starts off the same (at1em == 16px
) in the body of the document. Best of both worlds?