The fallback size problem

Swapping a heading font for a new variable file starts with a familiar step: registering it through @font-face.

@font-face {
  font-family: "NewFont";
  src: url("./fonts/NewFont-Variable.woff2") format("woff2");
  font-display: swap;
  font-weight: 200 700;
}

The family is then applied to headings with a fallback built into the stack:

/* FUTURE: :heading */
:where(h1, h2, h3, h4, h5, h6) {
  font-family: "NewFont", system-ui;
}

The heading also needs a font-size larger than the UA default, which is too small for the design:

h1 {
  font-size: clamp(3.13rem, 2.74vw + 2.75rem, 5.38rem);
}

/* etc. */

If the new file fails to load, the heading renders in system-ui — and at that size it looks enormous, which throws off the UI.

CSS offers no way to declare a different font-size for the fallback case. The experimental if() function looks like a candidate, but its style(), media() and supports() conditions can only test what the browser already knows, so there is no way to ask whether a font failed to load.

Registering the system font under a custom name

The workaround is to register the fallback itself with @font-face. Any font can be registered this way, including local ones, and given a custom name — here, a default system font:

@font-face {
  font-family: "NewFont Fallback";
  src: local("system-ui");
}

Once the fallback has a name, the @font-face descriptors that affect sizing become available. size-adjust is the relevant one, tuned by eye:

@font-face {
  font-family: "NewFont Fallback";
  src: local("system-ui");
  size-adjust: 55%;
}

The adjusted face then joins the font stack:

:where(h1, h2, h3, h4, h5, h6) {
  font-family: "NewFont", "NewFont Fallback", sans-serif;
}

A bare sans-serif is kept at the end in case a machine lacks a system-ui font. Dropping "NewFont" from the stack shows the effect directly.

Adjusting in either direction

The technique works in reverse as well: size-adjust can increase the default font rather than shrink it. The usual guidance is to pick a web font that best matches the custom one, but other factors can matter too.

@font-face exposes many more descriptors for anyone who wants a closer match than eyeball testing provides.