web font loading recipes

2025-12-11 0 682

webfontloadingrecipes

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: <link> tag
  • Code
  • Read more
  • Demo** (4 web fonts—1 preloaded)

font-display: swap with preload

  • HTML: <link> 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: <link> 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: <link> 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)

<style> Injection

Anything that uses JavaScript to inject a new <style> with @font-face blocks inside. Really bad repaint cost—avoid this. This is used in the Asynchronous Data URI method above but is also common in worse-performing methods too.

font-display: optional and preload

  • Code
  • Read more: preload with font-display: optional is an Anti-pattern
  • Demo** (4 web fonts)

️ FOFT using only font-display (mixing font-display values across a font-family)

This method does not currently have cross-browser support. I’m hoping this will change—learn more.

  • Code
  • Demo** (4 web fonts—1 swap / 3 optional)

Failed Experiments

Asynchronous CSS

This is a common thing people try—they asynchronously load the CSS (and only the CSS). Heck, I used this behavior before I started studying web font loading.

  • Failed: lazy loading the CSS only delays the start of the FOIT. It does not prevent it.
  • Read more at Lazy Loading Web Fonts is Probably Not What You Want
  • Code (learn more about asynchronous CSS)
  • Demo (4 web fonts)

@supports and font-display

  • Reasons for trying:
    • might be nice to only use web fonts if you can FOUT with font-display
    • might be nice to have FOUT with a class if font-display not supported (and work well without JS dependencies)
  • Failed: @supports doesn’t work with font-face descriptors.
  • Code
  • Demo

font-family Stack

  • Put two or more font-family web fonts in a single font-family stack.

  • Failed: The font matching algorithm selects the first web font that matches and attempts to load it (ignoring subsequent web font families). Even if you preload the subset first stage, it’ll swap over due to font-family order priority.

  • Code

  • Demo

  • Update: While you can mitigate the above problem with font-display, perhaps modifying the order of the font-family stack and @font-face block ordering, there are still problems with removing the unnecessary subset web font from the page after the larger version has loaded. Font features that occur with glyphs that cross these font file boundaries will be broken (kerning, ligatures, et cetera). Relatedly, you cannot remove a CSS-paired FontFace object using the CSS Font Loading API (per the specification).


** Take note that these methods will FOUT in Internet Explorer and Edge by taking advantage of their default font loading behavior.

下载源码

通过命令行克隆项目:

git clone https://github.com/zachleat/web-font-loading-recipes.git

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

申明:本文由第三方发布,内容仅代表作者观点,与本网站无关。对本文以及其中全部或者部分内容的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。本网发布或转载文章出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,也不代表本网对其真实性负责。

左子网 建站资源 web font loading recipes https://www.zuozi.net/34724.html

flash usdt erc20
上一篇: flash usdt erc20
rtl9210 firmware
下一篇: rtl9210 firmware
常见问题
  • 1、自动:拍下后,点击(下载)链接即可下载;2、手动:拍下后,联系卖家发放即可或者联系官方找开发者发货。
查看详情
  • 1、源码默认交易周期:手动发货商品为1-3天,并且用户付款金额将会进入平台担保直到交易完成或者3-7天即可发放,如遇纠纷无限期延长收款金额直至纠纷解决或者退款!;
查看详情
  • 1、描述:源码描述(含标题)与实际源码不一致的(例:货不对板); 2、演示:有演示站时,与实际源码小于95%一致的(但描述中有”不保证完全一样、有变化的可能性”类似显著声明的除外); 3、发货:不发货可无理由退款; 4、安装:免费提供安装服务的源码但卖家不履行的; 5、收费:价格虚标,额外收取其他费用的(但描述中有显著声明或双方交易前有商定的除外); 6、其他:如质量方面的硬性常规问题BUG等。 注:经核实符合上述任一,均支持退款,但卖家予以积极解决问题则除外。
查看详情
  • 1、左子会对双方交易的过程及交易商品的快照进行永久存档,以确保交易的真实、有效、安全! 2、左子无法对如“永久包更新”、“永久技术支持”等类似交易之后的商家承诺做担保,请买家自行鉴别; 3、在源码同时有网站演示与图片演示,且站演与图演不一致时,默认按图演作为纠纷评判依据(特别声明或有商定除外); 4、在没有”无任何正当退款依据”的前提下,商品写有”一旦售出,概不支持退款”等类似的声明,视为无效声明; 5、在未拍下前,双方在QQ上所商定的交易内容,亦可成为纠纷评判依据(商定与描述冲突时,商定为准); 6、因聊天记录可作为纠纷评判依据,故双方联系时,只与对方在左子上所留的QQ、手机号沟通,以防对方不承认自我承诺。 7、虽然交易产生纠纷的几率很小,但一定要保留如聊天记录、手机短信等这样的重要信息,以防产生纠纷时便于左子介入快速处理。
查看详情

相关文章

猜你喜欢
发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务