13

In Safari 12.0.2 and Chrome 71.0.3578.98 on Mac Mojave 10.14.2, when setting the font-size using rem units, the actual size won't go below 9px.

See this example:

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

Rendered font size

My browser's font size is set to the default (16px) with a minimum font size set to 6px:

Browser font settings

Setting text-size-adjust to none doesn't affect the problem. Firefox renders the size correctly.

The only thing that I've found to fix the problem is setting font-size: 0; to a parent element. For instance, if you add font-size: 0; to .container, the correct font size is rendered.

Does anyone know why it's not honoring the rem size below a certain threshold?

bishop
  • 37,830
  • 11
  • 104
  • 139
Stephen Watkins
  • 25,047
  • 15
  • 66
  • 100
  • When you add the vendor prefix of `-webkit-text-size-adjust: none` still no love? – Chris W. Dec 19 '18 at 18:06
  • Still no love with the vendor prefixed `-webkit-text-size-adjust: none;`. – Stephen Watkins Dec 19 '18 at 18:09
  • this is a known feature, I will try to find the duplicate question – Temani Afif Dec 19 '18 at 19:24
  • Ok @Temani Afif. Let me know. – Stephen Watkins Dec 20 '18 at 00:16
  • not able to find it :/ will try again later – Temani Afif Dec 20 '18 at 00:26
  • 1
    I believe this is a duplicate of https://stackoverflow.com/questions/21302069/disable-chrome-minimum-font-size-10px but I can't close vote due to the bounty. – zzzzBov Jan 02 '19 at 19:49
  • Another similar question: https://stackoverflow.com/questions/41578321/decimal-rem-based-font-size-not-working-as-expected – skyline3000 Jan 02 '19 at 19:50
  • Aside from curiosity about browser quirks, is there a reason you want text smaller than 9px? I'd imagine the restriction is there as a feature to prevent text becoming illegible, because anything smaller isn't readable in a practical way. If you do need it for some reason, seems like using javascript to set `px` values is your only recourse. – jmcgriz Jan 02 '19 at 20:01
  • Use some hacks like @media all and (-webkit-min-device-pixel-ratio:0) and (min-resolution: .001dpcm) { .text {font-size: 6px;} } see more at http://browserhacks.com/ – iazaran Jan 02 '19 at 20:07
  • 1
    This is not a duplicate of https://stackoverflow.com/questions/21302069/disable-chrome-minimum-font-size-10px. The minimum font size on the browser is set to `6px`, but it isn't being honored with `rem` units. For instance, I can set the font size to `6px` if I use `px` explicitly, but not lower than `6px`, as expected. This is a different problem. – Stephen Watkins Jan 02 '19 at 21:52
  • Great! This response deserves more... – Manuel Torre Aug 12 '21 at 04:03

1 Answers1

21

Chrome and its Blink rendering engine seem to have some non-obvious font-scaling rules. I'm unaware of any official comprehensive documentation, so let's go to the source.

(Note that I'm not an expert on Chromium internals generally or Blink renderer particularly. I've just been tracing through the source code and speculating on the most probable answers to the questions as posed.)

It seems to me that the engine calls upon the FontBuilder class during a redraw. This class has various dispatch methods that pass the DOM, zoom, and other relevant factors into what appears to be the crucial method: FontSize :: getComputedSizeFromSpecifiedSize. In that method, we see some juicy comments that address the points you've raised:

1. Why does setting font-size: 0; to a parent element fix it?

  // 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).

2. Why is it not honoring the rem size below a certain threshold?

  // 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).

3. For the curious, what are these minimum values when given relative units (eg 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

Interestingly, and a bit of an aside, the FontBuilder class dispatches to TextAutosizer :: computeAutosizedFontSize to scale the font size. This method uses hard coded values and a variable scaling factor:

  // 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;

From these facts, we see there are a good number of hard-coded pixel values, with 9 and 16 being commonly sprinkled about the relevant code. These hard-codes, the presence of several rules to scale the font down to a limit, with the ability to override using font-size all seem to match the observations and suggest it's behaving as intended -- if not necessarily intuitively.


Also, I've found that the most recent comment posted in Chrome bug #319623 very much resembles your report.

Possibly related: when using relative units on the html tag, rem-based values defined elsewhere will have a lower bound of 9px.

See CodePen: http://codepen.io/larrybotha/pen/wKYYXE

Workaround: absolute unit on html, em unit on body. rems everywhere else.

It may be prudent to watch that bug for further developments, though maybe not with breath held. The last update was in 2015.

bishop
  • 37,830
  • 11
  • 104
  • 139
  • 1
    Good research and write-up. While I can't say that I fully grok Blink's logic, based on the problem and the code you shared here, I agree with your conclusion. – Stephen Watkins Jan 04 '19 at 17:51
  • Glad it helped, @StephenWatkins. I hope this analysis is at least in the ball-park, but I'd love a Chrome expert to phone in: this all seems a bit hacky to me. – bishop Jan 04 '19 at 18:08
  • The workaround works only for font sizes on text. Unfortunately not if you are using rem on containers like divs for width/height ... – Robin Wieruch May 27 '19 at 07:59
  • 1
    Earlier this year, in Chrome 73, there were issues that arose relative to min font size and rem units. As a result, Chrome now allows a min font size of zero, and they changed the settings UI too. But if you set your min font-size to non-zero, it can scale your rem units to be huge. The UI jumps from zero to 6px as the available min sizes, nothing in between. I too would love to see a Chrome expert explain this in further detail. – Sideways S Sep 07 '19 at 14:48
  • 1
    Another hack that works for me in safari (for both text as well as for sizes): Set the font-size to e.g. `max(6px, 37.5%)` on the root element for a root font size of 6px. – DWK Nov 18 '21 at 17:19