web font loading recipes

字体下载 2025-07-23

web-font-loading-recipes

A bunch of demos for different web font loading strategies. Some of these are included on A Comprehensive Guide to Font Loading Strategies, some of them are more experimental.

Demos are hosted at https://www.zachl*e**at.com/web-fonts/demos/

Table of Contents

  • Recommended Methods
  • Further Enhancements
  • Experiments in Progress
  • Not Recommended but included for Posterity
  • Anti-patterns and Deprecated Methods
  • Failed Experiments

Recommended Methods

As web fonts are a progressive enhancement and with increasing support for the CSS Font Loading API, we can look forward to a time in which we won’t need to inline a polyfill into the header (for even faster font loading). The simplified CSS Font Loading API recipes are the defaults, but polyfilled versions are included for broader browser support—notably only the polyfilled versions will show web fonts in Internet Explorer and Edge web browsers (which do not have support for the CSS Font Loading API).

font-display: swap

  • CSS: @font-face descriptor
  • Code
  • Read more
  • Demo** (4 web fonts)

preload

  • HTML: tag
  • Code
  • Read more
  • Demo** (4 web fonts—1 preloaded)

font-display: swap with preload

  • HTML: tag and CSS: @font-face descriptor
  • Code
  • Read more about font-display and preload
  • Demo** (4 web fonts—1 preloaded)

FOUT with a Class

  • JavaScript: CSS Font Loading API
  • Code
  • Read more
  • Demo (4 web fonts)
  • or using a polyfill—Demo (4 web fonts)

FOUT

Similar to the above, but without using a class—using only the CSS Font Loading API. This doesn’t require any modification of the CSS, injects the web fonts using JS programmatically. I first saw this method in the Webfont Handbook from @bramstein.

  • JavaScript: CSS Font Loading API
  • Code
  • Demo (4 web fonts)
  • Related: .style.fontFamily method (only works well with one family per page), first saw this in a tweet from @simevidas

FOFT

Two stage load, using one Roman font file in the first stage (with font-synthesis).

  • JavaScript: CSS Font Loading API
  • Code
  • Read more
  • Demo (5 web fonts, two are the same—but only loaded once)
  • or using a polyfill—Demo (4 web fonts)

Critical FOFT

  • JavaScript: CSS Font Loading API
  • Code
  • Read more
  • Demo (5 web fonts—1 subset)
  • or using a polyfill—Demo (5 web fonts—1 subset)

Critical FOFT with Data URI

  • JavaScript: CSS Font Loading API
  • Code
  • Read more
  • Demo (5 web fonts—1 subset inline Data URI)
  • or using a polyfill—Demo (5 web fonts—1 subset inline Data URI)

Critical FOFT with preload

  • HTML: tag and JavaScript: CSS Font Loading API
  • Code
  • Read more
  • Demo (5 web fonts—1 subset)
  • or using a polyfill—Demo (5 web fonts—1 subset)

The eBay Method

  • JavaScript: CSS Font Loading API and FontFaceObserver polyfill
  • Code: HTML and Lazy-loaded JavaScript
  • Emulate font-display: optional with JavaScript.
    • Notable that it lazy loads the font loading polyfill only if CSS Font Loading API is not supported
  • Read more at eBay’s Font Loading Strategy.
  • Demo (4 web fonts) (polyfill is lazy loaded when CSS Font Loading API is not supported)

“The Compromise”: Critical FOFT with preload, with a polyfill fallback emulating font-display: optional

  • HTML: tag and JavaScript: CSS Font Loading API and FontFaceObserver polyfill
  • Code: HTML and Lazy-loaded JavaScript
  • Read more
    • Inspired by the eBay method above.
  • Demo (5 web fonts—1 subset) (polyfill is lazy loaded when CSS Font Loading API is not supported)
  • Currently in use on zachleat.com and smashingmagazine.com

Further Enhancements

These aren’t necessarily font loading strategies on their own but they are extra enhancements you can layer on top of and pair with existing strategies.

Network Information API

Opt out of web fonts on slow connection speeds.

  • Code (shown with FOUT approach)
  • Demo
  • Related blog post: Should I Use JavaScript to Load My Web Fonts?

prefers-reduced-motion User Preference

Opt out of web fonts when user has enabled Reduce Motion accessibility preference.

  • Code (shown with FOUT approach)
  • Demo
  • Related blog post: Should I Use JavaScript to Load My Web Fonts?

save-data User Preference

Opt out of web fonts when user has enabled Data Saver mode.

  • Code (shown with FOUT approach)
  • Demo
  • Related blog post: Should I Use JavaScript to Load My Web Fonts?

Experiments in Progress

You’ll probably see blog posts on these at some point.

  • Metric compatible web fonts
    • Show how fonts can look without FOUT reflow if they are metric compatible.
  • FOUT metric matching with a Variable Font
    • Reduction in FOUT reflow (reduce text movement on web font render)
    • Related: Font style matcher from @notwaldorf

Not Recommended but included for Posterity

font-display: optional

  • A little harsh to put this in the Not Recommended section but I like my web fonts on an empty-cache visit ?
  • Code
  • Demo** (4 web fonts)

System fonts

C’mon. ?

  • Code
  • Documentation
  • Demo (0 web fonts)

Unceremonious Web Fonts

  • Code
  • Documentation
  • Demo** (4 web fonts)

Unceremonious Web Fonts, WOFF2 Only (Cutting the Mustard)

Old browsers used to render FOIT without a timeout, which in practice made web fonts a single point of failure. Using WOFF2 only cuts the mustard to modern browsers that have a three second FOIT timeout for web fonts. We’re anti-invisible text here, but this approach is worth mentioning.

  • Code
  • Demo** (4 web fonts)

Unceremonious Faux Web Fonts

font-synthesis is not a good end-product.

  • Code
  • Demo** (1 web font): Bold and italic variants are rendered using font-synthesis

Anti-patterns and Deprecated Methods

Inline Data URI

  • Code
  • Read more
  • Demo (4 web fonts)

Asynchronous Data URI

  • Code
  • Read more
  • Demo (4 web fonts)