DEV Community

I doubled the root font size on all 76 of our pages and 60 per cent of the text did not move

Browser zoom is not the only way readers make text bigger. Chrome, Firefox and Safari all have a font size setting, and it works differently from zoom: instead of scaling the whole layout, it changes the default font size that rem and em resolve against.

Text declared in those units grows. Text declared in pixels does not. So "we pass zoom" and "we respect the reader's font size" are two different claims, and only the first one is easy.

I measured the second on all 76 public pages of Nakodo by doing to each page what the setting does to rem:

const style = document.createElement("style");
style.textContent = "html{font-size:32px !important}"; // double the default 16px
document.head.append(style);

Then I compared the computed font-size of every element that directly contains text, before and after.

Elements holding text, across 76 pages 14,067
Whose computed size changed 5,610
Share 39.9%

Three fifths of the text on the site ignores the reader entirely.

The histogram says exactly who

Rendered at Elements After doubling
14px 5,594 28px for 5,588 of them
15px 5,506 15px
12px 820 24px for 16 of them
11px 699 11px
26px 547 26px
13px 433 13px
22px 245 22px
17px 82 17px
46px 71 46px

The 14px row is text-sm, which Tailwind writes as 0.875rem, and it doubles as it should.

The 15px row is this, in our own stylesheet:

body {
  @apply bg-background text-foreground;
  font-size: 15px;
  line-height: 24px;
}

That single declaration is the largest group of text on the site and the largest group that cannot be resized. Every paragraph that does not carry an explicit class inherits it.

The 12px row is the interesting one, because it splits. 16 of those elements are text-xs (0.75rem) and they double to 24px. The other 804 are written as text-[12px], which Tailwind faithfully emits as pixels. Two elements can render at exactly the same size and respond differently, and nothing in the rendered page tells you which is which.

A grep over the source explains the shape of the whole table:

$ grep -rho "text-\[[0-9]*px\]" src | sort | uniq -c | sort -rn
135 text-[13px]
19 text-[28px]
18 text-[22px]
15 text-[32px]
14 text-[26px]
9 text-[17px]
9 text-[15px]
...
245 total

245 arbitrary pixel values against 428 uses of the rem-based scale (text-sm 309, text-xs 66, text-xl 19, text-base 17, text-lg 16, text-2xl 1).

The pixel values are not sloppiness, exactly: they came from a typographic spec with a 13px caption size and a 26px heading size, and Tailwind's arbitrary-value syntax makes it a two-second job to write the spec literally. That is the trap. The convenient way to be faithful to a design is also the way to opt out of a reader's preference.

The pages that care least about you are the wordiest ones

Share of text elements that responded, by page:

Page Responded
/pricing 73.7%
/alternatives 61.3%
/tiktokers 60.5%
/guides/how-to-find-a-tiktokers-email 29.4%
/terms 27.3%
/privacy 27.3%

That ordering is the opposite of useful. The pages with the least prose and the most interface, like the pricing table, are the most responsive, because interface is where the utility classes live.

The pages that are nothing but long reading, the legal pages and the long guides, are the least responsive, because long reading is exactly what inherits the body rule. If you set a larger default font size because you read long documents more comfortably that way, our long documents are the pages that will ignore you.

It does not break anything, and one page nearly does

The good news is that the layout survives being pushed around. At double the root size:

Before After
Total height of 76 pages 423,679px 732,819px
Median page growth 1.73x
Smallest growth (/privacy) 1.30x
Largest growth (/blog) 2.18x
Elements clipped by overflow: hidden 105 28
Pages with a horizontal scrollbar 0 1

The clipping count falling is not a mistake. Most of those 105 are decorative drawings in a footer with overflow-hidden on it, and when the page above them gets taller the footer has room to show them in full. Growth hides that class of defect rather than causing it, which is worth remembering the next time a resize test comes back cleaner than the baseline.

The one page that gains a horizontal scrollbar is the home page, by 107 pixels:

document.documentElement.clientWidth; // 1512
document.documentElement.scrollWidth; // 1619

The overflowing element is an SVG 240 pixels wide. It is a drawing positioned beside a block of text, and when the text grows the drawing is pushed past the edge. Everything else on 76 pages reflows.

What Chrome actually does, in case you test this

Two separate settings, and they behave differently:

  • Font size (the dropdown with Medium, Large, Very large) changes the default font size. rem and em and anything left at medium scale. Pixel values do not. That is the measurement above.
  • Minimum font size (further down, under Customise fonts) is a floor. It raises text that is declared below the floor whatever unit was used, pixels included. So our 11px, 12px and 13px text is not beyond rescue: a reader who sets a 16px minimum gets it raised, and gets it raised without the surrounding rem text growing in proportion, which is its own kind of layout surprise.

Neither of these is the same as zoom, which multiplies the CSS pixel and therefore moves everything. We pass zoom. I checked that separately by rendering the whole site at 320 CSS pixels wide, which is what 400% zoom on a 1280 screen amounts to, and nothing scrolls sideways.

What came out of it

  • Move the body rule off pixels. font-size: 15px; line-height: 24px on body is 5,506 elements, every one of them prose. Writing it as 0.9375rem with a unitless line height costs nothing and converts the largest immovable block of text on the site in one line. The same rule is already on the list for a different reason: at 15px our measure runs to about 78 characters a line, and 16px brings it to 73.
  • Ban arbitrary pixel font sizes in review. 245 of them, 135 being one value. A lint rule that rejects text-[Npx] and a scale entry called something like text-caption would make the next 135 of them rem by default.
  • Fix the drawing on the home page. One SVG, 107 pixels of overflow, and the only reflow failure in 76 pages.
  • Test the thing the reader controls, not the thing you control. Zoom is the test everyone runs because it is in the browser menu. The font size setting is the one that distinguishes a site built in rem from a site built in pixels, and it took three lines of JavaScript to find out which one we are.

Try it on the live site: open the pricing page and the privacy notice, paste the three-line style injection from the top of this post into the console on each, and watch one page grow and the other mostly sit there.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.