REM字体大小未调整到低于任意阈值

Ste*_*ins 8 css blink

在Mac Mojave 10.14.2上的Safari 12.0.2和Chrome 71.0.3578.98中,设置font-size使用rem单位时,实际大小不会低于此值9px.

看这个例子:

https://codepen.io/stephenjwatkins/pen/OrbGxL

渲染字体大小

我的浏览器的字体大小设置为默认值(16px),最小字体大小设置为6px:

浏览器字体设置

设置text-size-adjustnone不会影响问题.Firefox正确渲染大小.

我发现解决问题的唯一方法是设置font-size: 0;父元素.例如,如果您添加font-size: 0;.container,正确的字体大小呈现.

有谁知道它为什么不尊重rem某个阈值以下的尺寸?

bis*_*hop 13

Chrome及其Blink渲染引擎似乎有一些非显而易见的字体缩放规则.我不知道任何官方的综合文档,所以让我们去看看源代码.

(请注意,我不是一般的Chromium内部专家或者特别是Blink渲染器的专家.我刚刚查看了源代码并推测了最可能的问题答案.)

在我看来,引擎在重绘期间调用FontBuilder.这个类有各种调度方法,可以将DOM,缩放和其他相关因素传递给看似关键的方法:FontSize :: getComputedSizeFromSpecifiedSize.在那种方法中,我们看到一些多汁的评论,解决了你提出的观点:

1.为什么设置font-size: 0;父元素会修复它?

  // Text with a 0px font size should not be visible and therefore needs to be
  // exempt from minimum font size rules. Acid3 relies on this for pixel-perfect
  // rendering. This is also compatible with other browsers that have minimum
  // font size settings (e.g. Firefox).
Run Code Online (Sandbox Code Playgroud)

2.为什么不尊重rem尺寸低于某个阈值?

  // We support two types of minimum font size. The first is a hard override
  // that applies to all fonts. This is "minSize." The second type of minimum
  // font size is a "smart minimum" that is applied only when the Web page can't
  // know what size it really asked for, e.g., when it uses logical sizes like
  // "small" or expresses the font-size as a percentage of the user's default
  // font setting.

  // With the smart minimum, we never want to get smaller than the minimum font
  // size to keep fonts readable. However we always allow the page to set an
  // explicit pixel size that is smaller, since sites will mis-render otherwise
  // (e.g., http://www.gamespot.com with a 9px minimum).
Run Code Online (Sandbox Code Playgroud)

3.对于好奇的人,给定相对单位(例如x-small)时这些最小值是多少?

// Strict mode table matches MacIE and Mozilla's settings exactly.
static const int strictFontSizeTable[fontSizeTableMax - fontSizeTableMin +
                                     1][totalKeywords] = {
    {9, 9, 9, 9, 11, 14, 18, 27},    {9, 9, 9, 10, 12, 15, 20, 30},
    {9, 9, 10, 11, 13, 17, 22, 33},  {9, 9, 10, 12, 14, 18, 24, 36},
    {9, 10, 12, 13, 16, 20, 26, 39},  // fixed font default (13)
    {9, 10, 12, 14, 17, 21, 28, 42}, {9, 10, 13, 15, 18, 23, 30, 45},
    {9, 10, 13, 16, 18, 24, 32, 48}  // proportional font default (16)
};
// HTML       1      2      3      4      5      6      7
// CSS  xxs   xs     s      m      l     xl     xxl
//                          |
//                      user pref
Run Code Online (Sandbox Code Playgroud)

有趣的是,除了一点之外,FontBuilder该类调度到TextAutosizer :: computeAutosizedFontSize以缩放字体大小.此方法使用硬编码值和可变缩放因子:

  // Somewhat arbitrary "pleasant" font size.
  const float pleasantSize = 16;
  // Multiply fonts that the page author has specified to be larger than
  // pleasantSize by less and less, until huge fonts are not increased at all.
  // For specifiedSize between 0 and pleasantSize we directly apply the
  // multiplier; hence for specifiedSize == pleasantSize, computedSize will be
  // multiplier * pleasantSize. For greater specifiedSizes we want to
  // gradually fade out the multiplier, so for every 1px increase in
  // specifiedSize beyond pleasantSize we will only increase computedSize
  // by gradientAfterPleasantSize px until we meet the
  // computedSize = specifiedSize line, after which we stay on that line (so
  // then every 1px increase in specifiedSize increases computedSize by 1px).
  const float gradientAfterPleasantSize = 0.5;
Run Code Online (Sandbox Code Playgroud)

从这些事实来看,我们看到有大量硬编码像素值,9并且16通常涉及相关代码.这些硬代码,将字体缩小到极限的几个规则的存在,以及使用font-size覆盖的能力似乎都与观察结果相符,并表明它的行为符合预期 - 如果不一定是直观的.


另外,我发现Chrome bug#319623中发布的最新评论非常类似于您的报告.

可能相关:当在html标签上使用相对单位时,在其他地方定义的基于rem的值将具有9px的下限.

请参阅CodePen:http://codepen.io/larrybotha/pen/wKYYXE

解决方法:html上的绝对单位,正文上的em单位.到处都是.

谨慎观察这个进一步发展的错误,尽管可能没有屏住呼吸.最后一次更新是在2015年.