brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: font-display: swap in CSS shows text right away while a web font loads

Css
~/tips/tip-css-font-display-swap

font-display: swap;

Visitors can read the page in a fallback font instead of staring at invisible text.

By default many browsers hide text for a short while when the web font has not arrived yet. On a slow connection that means a page with no words. font-display controls that behavior per font.

@font-face {
  font-family: "Source Serif";
  src: url("/fonts/source-serif.woff2") format("woff2");
  font-weight: 400;
  font-display: swap;
}

swap gives the font an extremely short block period and then shows the fallback font straight away. When the web font arrives, the browser swaps it in. That is what I’d use for body text, because reading early matters more than the exact font.

The other values are worth knowing. optional waits a very short moment and uses the web font only if it is already there, otherwise the page keeps the fallback for that visit. fallback sits between the two. block hides text for a short while longer, which suits icon fonts better, where a fallback character makes no sense.

If you load fonts from Google Fonts, the display=swap parameter in the URL sets this for you.

The cost of swap is a visible jump when the font changes, since the fallback usually has different widths. That shift counts against your layout stability. Matching the fallback’s size with size-adjust makes it much smaller.

Further reading: MDN: font-display

More Css tips CSS Overview in Chrome DevTools audits the colors and fonts of a page .notice:empty { .toast { article { counter-reset: h2; } .toolbar { All Css tips (66) All topics

Link