Options to detect when a @font-face has been loaded

What is a @font-face?

@font-face is a CSS at-rule used to define custom fonts for use on web pages. It allows web designers and developers to specify downloadable fonts that can be used to render text on their websites, regardless of whether the font is installed on the user’s device.

What is the FOUT, FOIT or FOFT?

There are three common issues regarding text rendering on browsers. And the goal always is to reduce or at least minimize the text reflow.

What the FOUT!

FOUT stands for “Flash of Unstyled Text.” It’s a phenomenon that occurs when using web fonts in CSS. When a web page is loading, the browser might initially render text using a fallback font while waiting for the web font to download and apply. This results in a momentary flash where the text appears in the fallback font before switching to the desired web font.

FOUT can be visually disruptive, especially if the fallback font differs significantly from the intended web font in terms of style or size. To mitigate FOUT, web developers often use techniques such as font loading strategies (like using the font-display property in CSS) or server-side rendering to ensure that the desired web font is loaded and applied as quickly as possible to minimize or eliminate the flash of unstyled text.

What the FOIT!

FOIT stands for “Flash of Invisible Text.” It’s another phenomenon related to web fonts in CSS. Unlike FOUT, where fallback text is briefly visible before the web font loads, FOIT occurs when the browser hides text until the web font is fully loaded and ready to be applied.

When FOIT happens, the text remains invisible until the browser has downloaded the web font. This can result in a delay in displaying text content on the webpage, which might lead to a poor user experience.

To address FOIT, web developers sometimes use techniques such as font loading strategies (like preloading fonts or using the font-display property) to ensure that text content remains visible while waiting for web fonts to load. These techniques aim to balance the trade-off between the performance benefits of font loading and the user experience of text visibility.

What the FOFT!

“FOFT” stands for “Flash of Faux Text.” This term describes a phenomenon similar to FOUT (Flash of Unstyled Text) but specifically occurs when using synthetic fonts, such as system fonts or web-safe fonts, instead of custom web fonts.

In FOFT, when a web page is loading, the browser may initially render text using a generic font (like Arial or Times New Roman) before switching to the specified font once it has fully loaded. This switch can result in a brief flash where the text appears in the generic font before being replaced with the intended font, causing a disruption in the visual appearance of the text.

To mitigate FOFT, developers can employ similar techniques used to address FOUT, such as optimizing font loading strategies or utilizing font preloading techniques to ensure a smoother transition from the generic font to the specified font. Additionally, choosing system fonts or web-safe fonts that closely resemble the desired custom font can help reduce the visibility of FOFT.

DETECT WHEN FONTS ARE LOADED!

Option 1

Use the CSS font loading API. Not supported in all browsers yet (MDN, W3C). You can call document.fonts  to get a FontFaceSet object.

  • check(fontSpec) – returns whether all fonts in the given font list have been loaded and are available. The fontSpec uses the CSS shorthand syntax for fonts.
    Example: document.fonts.check('bold 16px Roboto'); // true or false
  • document.fonts.ready – returns a Promise indicating that font loading and layout operations are done.
  1. alert('Roboto loaded? ' + document.fonts.check('1em Roboto'));  // false
  2.  
  3. document.fonts.ready.then(function () {
  4.   alert('All fonts in use by visible text have loaded.');
  5.    alert('Roboto loaded? ' + document.fonts.check('1em Roboto'));  // true
  6. });
  7.  
  8. document.fonts.onloadingdone = function (fontFaceSetEvent) {
  9.    alert('onloadingdone we have ' + fontFaceSetEvent.fontfaces.length + ' font faces loaded');
  10. };

Option 2

Use Font Face Observer – its a small @font-face loader and monitor compatible with any web font service. It will monitor when a web font is applied to the page and notify you. It does not limit you in any way in where, when, or how you load your web fonts. Unlike the Web Font Loader Font Face Observer uses scroll events to detect font loads efficiently and with minimum overhead.

  1. var font = new FontFaceObserver('My Family', {
  2.   weight: 400
  3. });
  4.  
  5. font.load().then(function () {
  6.   console.log('Font is available');
  7. }, function () {
  8.   console.log('Font is not available');
  9. });

GitHub

Option 3

Use jQuery-FontSpy.js, works by checking the change in width of a string.

  1. fontSpy('My Icons', {
  2.   glyphs: '\ue81a\ue82d\ue823',
  3.   success: function() {
  4.     //alert("My Icons loaded successfully");
  5.   },
  6.   failure: function() {
  7.     //alert("My Icons failed to load");
  8.   }
  9. });

GitHub

Option 4

Use Webfont Loader,  if loading fonts from Typekit , Fonts.com, Google, Fontdeck or custom location.

This introduces the following events:

  • loading – This event is triggered when all fonts have been requested.
  • active – This event is triggered when the fonts have rendered.
  • inactive – This event is triggered when the browser does not support linked fonts or if none of the fonts could be loaded.
  • fontloading – This event is triggered once for each font that’s loaded.
  • fontactive – This event is triggered once for each font that renders.
  • fontinactive – This event is triggered if the font can’t be loaded.
  1. <script src="https://ajax.googleapis.com/ajax/libs/webfont/1.6.16/webfont.js"></script>
  2. <script>
  3.   WebFont.load({
  4.     google: {
  5.       families: ['Droid Sans', 'Droid Serif']
  6.     }
  7.   });
  8. </script>

GitHub

Enjoy coding …

Alex

I am a full-stack developer. I love programming,  design and know my way around server architecture as well.  I would never feel complete, with one of these missing. I have a broad range of interests, that’s why I constantly dive into new technologies and expand my knowledge where ever required. Technologies are evolving fast and I enjoy using the latest. Apart from that, I am a peace loving guy who tries to have people around him that think the same.  I truly believe in the principle: “If you help someone, someone will help you, when you need it."

Recent Posts

B&B / Hotel Booking Solutions for WordPress | 2024

BOOKING SOLUTIONS 202x This is my take on a subset of booking, appointment, PMS or… Read More

4 weeks ago

WordPress Cron + WP-CLI + Ntfy

THE GOAL Create a system cron for WordPress, that is accessible and can be easily… Read More

2 months ago

2024 is here and now :)

2024, what's cooking? Slowly getting into the 2024 spirit. 3 projects coming to a close… Read More

4 months ago

2023 ends and whats next !

Short look back at 2023 This has been a busy and interesting year. I am… Read More

4 months ago

cubicFUSION Grid Tweaker – Elementor Grid made easy.

Elementor Pro provides grid containers as an experimental feature. The options provided are limited, when… Read More

5 months ago

Archaeology Travel Booth – Travel Innovation Summit 2023

Archaeology Travel is an online travel guide for people who enjoy exploring the world’s pasts.… Read More

6 months ago