Setting the font-size of a body tag
我一直在尝试配置一个基本的CSS模板,可以用来开始使用REM。我看到了设置以下
我不确定我是否喜欢这种方式,因为如果10px的字体大小不是我的基线,则必须指定
以及
标签的大小。
我一直在尝试一些不同的方法,并且对
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <html> <body> <header> Testing h1 Testing h2 </header> <main> <p> Random paragraph </p> </main> </body> </html> |
考虑以下css:
1 2 3 | body { font-size: 100%; } |
我希望浏览器将字体显示为其默认大小,即16px。
标记中的所有内容仍为16px,但是
还原为浏览器样式表中指定的原始大小。为什么未将所有文本设置为16px?
另一个例子:
1 2 3 | body { font-size: 12px; } |
为什么
不采用12px 的这种字体大小? -
确实变小了(12/16 = 0.75降低了75%)。我希望h1为12px或保持相同的默认值,因为在用户代理样式表中为h1-h6指定的值是特定于更多的。
规则在指定百分比方面如何工作?设置样式表以便可以使用REM的最佳实践是什么?
对
因此,如果在主体中指定
您可以使用相同的逻辑为其他标题找到
1 2 3 | body { font-size:100%; } |
1 | this a text |
如果在正文中指定
1 2 3 | body { font-size:12px; } |
1 | this a text |
并且指定
1 2 3 | body { font-size:62.5%; } |
1 | this a text |
而且,由于
您可以使用开发工具轻松地在"计算"选项卡中检查这些值:
从文档中:
The size of an em value is dynamic. When defining the font-size
property, an em is equal to the size of the font that applies to the
parent of the element in question. If you haven't set the font size
anywhere on the page, then it is the browser default, which is often
16px
对于百分比:
A positive
value, relative to the parent element's font size.
Why doesn't
- take this font size of 12px?
当您更改其祖先的字体大小时,
- does get smaller (by 75% as 12/16 = 0.75). I'd expect h1 to be either 12px or stay the same default value because the value specified for h1-h6 in the user agent stylesheets is MORE specific.
在浏览器样式表中指定的值(至少在我的浏览器中)使用
How do the rules work in terms of specifying percentages?
用于祖先元素(
1 | HTML {font-size: 62.5%; } /* Now `rem` is equivalent to `10px` */ |
代替
1 | BODY {font-size: 62.5%; } /* Does NOT affect `rem` */ |
HTML字体大小可能会影响滚动速度
请注意,至少在某些浏览器中,
重置所有元素的字体大小
要重置所有元素的字体大小,可以使用通用选择器:
1 | * {font-size: 100%; } |
要么:
1 | * {font-size: 1em; } |
正确的
另外,请确保在HTML文档的开头具有正确的
1 | <!DOCTYPE html> |
在我这边工作得很好。唯一的可能是,您的HTML中缺少指向CSS文件的链接... :)
否则,只是为了确保对同一属性尝试内联样式。