简体   繁体   English

为什么 rems 并不总是以相同的结果计算

[英]How come rems are not always computed with the same result

Can anyone explain this behavior to me?谁能向我解释这种行为? https://codepen.io/anon/pen/BrRpeB https://codepen.io/anon/pen/BrRpeB

I don't understand how the computed font-size is larger for the inner <span> element than the outer <code> element...我不明白内部<span>元素的计算字体大小如何大于外部<code>元素...

 :root { font-size: 62.5%; /* font-size 1em = 10px on default browser settings */ } span, code, div { font-size: 1.6rem; }
 <code>Outer <span>inner</span> outer</code>

REM as I'm sure you know stands for root em, and em's are based on the font-size of the parent element.我相信您知道 REM 代表根 em,而 em 是基于父元素的字体大小。 Since the font size for each element in your example is a percentage, as in a percentage of the element size, the differently sized elements cause different font sizes to be produced.由于示例中每个元素的字体大小都是百分比,如元素大小的百分比,不同大小的元素会导致生成不同的字体大小。 If your original root font-size was a set amount like pixels, the result would be elements containing the same sized font, such as the code snippet below.如果您的原始根字体大小是像像素这样的固定数量,则结果将是包含相同大小字体的元素,例如下面的代码片段。

 :root { font-size: 16px; /* font-size 1em = 10px on default browser settings */ } span, code, div { font-size: 1.6rem; }
 <code>Outer <span>inner</span> outer</code>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM