Load fonts with font-display swap
Show text in a fallback font while a web font loads by setting font-display swap, or load the font with the FontFace API and add it when it is ready.
By default a browser may hide text for a few seconds while a custom font downloads. font-display: swap shows a fallback font first and switches when the font arrives.
@font-face {
font-family: 'Inter';
src: url('/fonts/inter.woff2') format('woff2');
font-display: swap;
}The same can be done from JavaScript with the FontFace API, which lets you control when loading starts:
const inter = new FontFace('Inter', 'url(/fonts/inter.woff2)', { display: 'swap' });
await inter.load();
document.fonts.add(inter);
console.log(inter.status, document.fonts.check('16px Inter'));loaded trueswap gives the fastest first paint but can cause a visible shift when the real font replaces the fallback and the text reflows. Reduce it by choosing a fallback with similar metrics, using size-adjust and the other descriptors in @font-face, and by preloading the font file with <link rel="preload" as="font" type="font/woff2" crossorigin>.
optional is another value: the font is used only if it arrives very quickly, which avoids the shift entirely at the price of showing the fallback on a slow first visit. Use WOFF2, which is the smallest format. See font-display.