设置根元素的字体大小为em单位是什么意思?

huangapple go评论69阅读模式
英文:

What does it mean to set the root element's font-size in em units?

问题

I've not been able to find the official definition of em units, but I have found this in the spec:

设置根元素的字体大小为em单位是什么意思?

I've also found a lot of other sources (listed below). This got me thinking, what does it mean to set the font size of the root element (i.e., <html>) to em units? Although a lot of my sources recommend using em, I couldn't find any that call out root element usage specifically.


html {
  font-size: 1em;
}

<html>

<body>
  <h1>html { font-size: 1em; }</h1>
  <p>Hello World!</p>
</body>

</html>



html {
  font-size: 1.5em;
}

<html>

<body>
  <h1>html { font-size: 1.5em; }</h1>
  <p>Hello World!</p>
</body>

</html>



html {
  font-size: 0.5em;
}

<html>

<body>
  <h1>html { font-size: 0.5em; }</h1>
  <p>Hello World!</p>
</body>

</html>

In these three above examples, I see the difference with my eyes, but what is the correct way to interpret what I'm seeing? In other words, what does html { font-size: <x>em; } mean?

Sources

  1. https://ux.stackexchange.com/questions/7820/font-size-for-mobile-sites
  2. https://w3c.github.io/csswg-drafts/css-values-4/#em
  3. https://www.w3.org/Style/Examples/007/units.en.html#font-size
  4. https://developer.mozilla.org/en-US/docs/Web/CSS/font-size
英文:

I've not been able to find the official definition of em units, but I have found this in the spec:

设置根元素的字体大小为em单位是什么意思?

I've also found a lot of other sources (listed below). This got me thinking, what does it mean to set the font size of the root element (i.e., &lt;html&gt;) to em units? Although a lot of my sources recommend using em, I couldn't find any that call out root element usage specifically.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

html {
  font-size: 1em;
}

<!-- language: lang-html -->

&lt;html&gt;

&lt;body&gt;
  &lt;h1&gt;html { font-size: 1em; }&lt;/h1&gt;
  &lt;p&gt;Hello World!&lt;/p&gt;
&lt;/body&gt;

&lt;/html&gt;

<!-- end snippet -->

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

html {
  font-size: 1.5em;
}

<!-- language: lang-html -->

&lt;html&gt;

&lt;body&gt;
  &lt;h1&gt;html { font-size: 1.5em; }&lt;/h1&gt;
  &lt;p&gt;Hello World!&lt;/p&gt;
&lt;/body&gt;

&lt;/html&gt;

<!-- end snippet -->

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

html {
  font-size: 0.5em;
}

<!-- language: lang-html -->

&lt;html&gt;

&lt;body&gt;
  &lt;h1&gt;html { font-size: 0.5em; }&lt;/h1&gt;
  &lt;p&gt;Hello World!&lt;/p&gt;
&lt;/body&gt;

&lt;/html&gt;

<!-- end snippet -->

In these three above examples, I see the difference with my eyes, but what is the correct way to interpret what I'm seeing? In other words, what does html { font-size: &lt;x&gt;em; } mean?

Sources

  1. https://ux.stackexchange.com/questions/7820/font-size-for-mobile-sites
  2. https://w3c.github.io/csswg-drafts/css-values-4/#em
  3. https://www.w3.org/Style/Examples/007/units.en.html#font-size
  4. https://developer.mozilla.org/en-US/docs/Web/CSS/font-size

答案1

得分: 1

规范中:

> 当用作元素的font-size属性值时,它们会相对于父元素的计算度量解析,或者相对于与font和line-height属性的初始值相对应的计算度量,如果元素没有父元素。

英文:

From the specification

> When used in the value of the font-size property on the element they refer to, the local font-relative lengths resolve against the computed metrics of the parent element—or against the computed metrics corresponding to the initial values of the font and line-height properties, if the element has no parent.

huangapple
  • 本文由 发表于 2023年2月24日 06:08:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75550818.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定