Dev.to WebDev 🛠 Dev 👁 0 📖 3 min read

How to test your fluid font sizes for jumps at breakpoints

Resize your browser slowly. If the text suddenly gets smaller as the window gets wider, your fluid type has a jump in it. We spent about four hours on this in a project whose responsive CSS came from a visual generator.

Resize your browser slowly. If the text suddenly gets smaller as the window gets wider, your fluid type has a jump in it.

We spent about four hours on this in a project whose responsive CSS came from a visual generator. The page felt wrong at certain widths, and nothing looked wrong in the code.

What the problem looks like

The generated CSS was a stack of media queries, each with its own calc():

html { font-size: calc(0.625rem + 0.4167vw); }
@media (max-width: 1440px) { html { font-size: calc(0.8127rem + 0.2081vw); } }
@media (max-width: 479px)  { html { font-size: calc(0.7495rem + 0.8368vw); } }

Each rule is a straight line, and each one is fine alone. The trouble is that nothing makes the lines meet at the breakpoints.

Test it

You don't have to find this by eye. Copy your rules into a script and walk through every width:

// fluid-check.mjs
// Each rule: applies up to maxWidth, font-size = rem * 16 + vw% of the width.
const rules = [
  { maxWidth: 479, rem: 0.7495, vw: 0.8368 },
  { maxWidth: 1440, rem: 0.8127, vw: 0.2081 },
  { maxWidth: Infinity, rem: 0.625, vw: 0.4167 },
];

const fontSize = (width) => {
  const { rem, vw } = rules.find((rule) => width <= rule.maxWidth);
  return rem * 16 + (vw * width) / 100;
};

let previous = fontSize(320);
for (let width = 321; width <= 2560; width++) {
  const size = fontSize(width);
  if (size < previous - 0.01) {
    console.log(`${width}px: ${previous.toFixed(2)}px -> ${size.toFixed(2)}px`);
  }
  previous = size;
}

Run it with node fluid-check.mjs. For the CSS above it prints:

480px: 16.00px -> 14.00px

One pixel wider, and the root font size drops by two pixels. Everything sized in rem shrinks with it, which is about 12% across the whole page.

If the script prints nothing, your sizes never go down as the window grows. Check the rule order against your own CSS, because find here takes the first rule whose maxWidth fits, smallest first.

The fix: one clamp()

The jump exists because there are several separate formulas. With a single formula there's nothing to line up, so replace the stack with one clamp() per size, calculated from two points: the size you want on your smallest design and on your largest.

function fluid(minPx, maxPx, minWidth = 390, maxWidth = 1620) {
  const slope = (maxPx - minPx) / (maxWidth - minWidth);
  const intercept = minPx - slope * minWidth;
  const rem = (px) => `${(px / 16).toFixed(4)}rem`;
  return `clamp(${rem(minPx)}, ${rem(intercept)} + ${(slope * 100).toFixed(4)}vw, ${rem(maxPx)})`;
}

console.log(fluid(32, 48));

That prints:

clamp(2.0000rem, 1.6829rem + 1.3008vw, 3.0000rem)

The result is 32px at a 390px window, 40px at 1005px and 48px at 1620px. Below and above those widths it stays at 32px and 48px.

Use it directly:

h1 { font-size: clamp(2rem, 1.6829rem + 1.3008vw, 3rem); }

Two things to watch

Leave the root alone. Set fluid sizes on the elements that need them and not on html. Scaling the root changes every rem on the page at once, including any third-party UI that shares it. We had an admin panel shrinking along with the site before we fixed the root at a constant value.

Check zoom. A size built mostly from vw barely reacts to browser zoom, which is a problem for people who rely on it (WCAG 1.4.4). Keep the largest size within about 2.5 times the smallest, and test at 200% zoom.

Keep it from coming back

If your sizes come from a config or a plugin, move the width loop into a script that runs before each build, and make it exit with an error when a size goes down. Then feed it a known-bad formula once, to prove it can fail.

Full disclosure: we make Zero Drift, a paid Next.js and Sanity starter, and a version of this check ships with it. Everything above works without it.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.