我喜欢在创建网站时使用em
。因此,我为body
元素设置了默认的font-size
为100.01%
。
我的问题是,我应该将默认的font-size
设置在body
还是html
元素上?两者之间是否有优缺点?
现在rem
单位开始流行起来,建议在根元素(html标签)上设置基础字体大小(rem
代表root em)。
px
或者em
作为备选方案,只需要添加一条额外的声明即可,确保备选值在rem
值之前。当然,这里的难点在于需要计算出绝对值,否则使用rem
单位时不需要这样做。 - BoltClockhtml {
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% */
}
body
定义,但您可以使用包含所有标签的定义,例如在 HTML 重置中经常看到的定义方式。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;
}
我不建议进行此重置。标准的制定是为了帮助您避免这种诡计。
在IT技术中,重置通常指恢复设备或系统的默认设置。然而,作者并不推荐这种做法,并提醒人们要遵守相关标准规定,切勿使用不当的方法进行操作。<p>
元素内部的<a>
元素变小75%,这(可能)不是你想要的。其他嵌套元素类似。您可以使用“rem”进行修复,但我不同意首先进行此全标签“重置”。只需设置<body>
标记,并可能添加font-size: inherit;
以进行重置。 - nvreez<body>
的font-size
设置为em单位而不是rem单位?我猜结果是一样的,因为<html>
是<body>
的父元素,而<html>
也是根元素,但如果你最终使用rems作为主要大小单位,使用rems可能更清晰。谢谢! - Alex MMrem
定义,rem
单位(也称为根元素em
)与根元素大小相关,根元素大小是<html>
标签。我只想知道你这样做的原因,但我相信在这种情况下,无论是em
还是rem
都会产生相同的结果。 - Alex MM我认为在 html
或 body
中设置基本的 font-size
来适应使用 ems 的大小没有任何优势或劣势;它们都会产生相同的效果。
与问题无关:
不过,我建议使用不同的默认 font-size
。 我会将其设置为:
body {
font-size: 62.5%;
}
这样做会将默认的font-size
从16px
降至10px
,这样选择font-size
就更容易了,因为不需要进行复杂的计算。这意味着1em
等于10px
,所以计算px
大小只需要乘以10:
1.0em
=10px
1.4em
=14px
1.8em
=18px
2.2em
=22px
html { font-size: 62.5% } body { font-size: 160% }
这样,您仍然可以使 1rem == 10px
,但是在文档正文中,1em 的大小仍然从相同的值开始(即 1em == 16px
)。两全其美? - Stephen M Irving
100.01%
?如果我没记错的话,这修复了一个只存在于某些旧版Opera中的错误,而这些版本现在也已经不再使用了。 - thirtydot