font-display

Term side of the Card.
Topic: Text Handling
Unit title: font-display
Categories: Property
Explanation side of the Card

Explanation: The `@font-face` descriptor that defines how a web font is shown while it loads or if it fails. Values 'auto', 'block', 'swap', 'fallback' and 'optional' trade between invisible text, a fallback font flash and layout stability.

Browser compatibility:
  • The chrome browser supports this feature starting from version 60.
  • The edge browser supports this feature starting from version 79.
  • The firefox browser supports this feature starting from version 58.
  • The safari browser supports this feature starting from version 11.1.
Availability: 

Example

Wrongcss
body {
  font-family: "Brand", sans-serif;
  font-display: swap;
}
Rightcss
@font-face {
  font-family: "Brand";
  src: url("brand.woff2") format("woff2");
  font-display: swap;
}

font-display is a descriptor of @font-face; as a regular property on body it is ignored.

Common mistakes

  • Text is invisible for a few seconds on slow networks because the default is auto.
  • The layout jumps when the web font swaps in, because the fallback font has different metrics.

Key points

  • font-display is set inside each @font-face block, not on regular selectors.
  • Web fonts use swap or optional so text is visible while the font loads.
  • Critical fonts are preloaded with crossorigin, so the swap happens early.

Interview questions

font-display swap vs optional?

swap shows fallback text and always switches when the font loads. optional uses the font only if it arrives almost immediately, avoiding a late layout shift.

Does font-display: swap cause layout shift?

It can, when the fallback and web font differ in size. Tune the fallback with size-adjust in @font-face to reduce it.

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy