I’m changing the heading font in a challenge with a brand new variable file. Straightforward sufficient to register the brand new font with @font-face proper?
@font-face {
font-family: "NewFont";
src: url("./fonts/NewFont-Variable.woff2") format("woff2") tech(variations);
font-display: swap;
font-weight: 200 700;
}
It’s declared on the headings, in fact, together with a fallback insurance coverage:
/* FUTURE: :heading */
:the place(h1, h2, h3, h4, h5, h6) {
font-family: "NewFont", system-ui;
}
Cool, cool. However the brand new font-size wants the next worth than the UA defaults, because the default sizing is just too darn small:
h1 {
font-size: clamp(3.13rem, 2.74vw + 2.75rem, 5.38rem);
}
/* and so on. */
Let’s say that the brand new font file fails to load for, you understand, causes. We get the system-ui font as a fallback:
Oh gosh, that’s large. That doesn’t go well with my UI in any respect.
There’s no solution to set a fallback font-size for when a fallback font is used. I used to be actually tempted to see if the experimental if() operate is likely to be a risk as a future solution to sort out it however, alas, the fashion(), media(), and helps() circumstances it accepts can solely use what the browser already is aware of. In different phrases, there’s no solution to inform the browser if this font fails to load.
I ran my hand by way of my ever-thinning hair and remembered that we are able to use @font-face to register any font we would like, even native ones, and provides them a customized identify. Say a default system font:
@font-face {
font-family: "NewFont Fallback";
src: native("system-ui");
}
Now we’ve obtained one thing to play with! There are different descriptors we are able to fudge with within the at-rule that regulate the font measurement, like size-adjust. With slightly eyeball testing:
@font-face {
font-family: "NewFont Fallback";
src: native("system-ui");
size-adjust: 55%;
}
Now that may be added to the font stack:
:the place(h1, h2, h3, h4, h5, h6) {
font-family: "NewFont", "NewFont Fallback", sans-serif;
}
…including sans-serif in there in any bizarre case the place somebody’s machine doesn’t have a system-ui font. You possibly can take away "NewFont" from the stack to see the distinction:
I suppose it might work the opposite method and easily use size-adjust to enhance the default font. Both method, standard knowledge is often “select the net font that finest matches your customized font.” However there are different components to think about, I assume. Perhaps you already knew all this. I hadn’t considered that earlier than or actually run into that state of affairs. Hopefully it’s helpful for you should you hit an identical hurdle.
There are tons and plenty of different @font-face descriptors you is likely to be eager to play with should you’re extra persnickety about matching than me.









