Wait for fonts to load before rendering web page
This is down to how the browser behaves.
First off where is your @font declared? Is it inline to your HTML, declared in a CSS sheet on the page, or (hopefully) declared in an external CSS sheet?
If it is not in an external sheet, try moving it to one (this is better practice anyway usually).
If this doesn't help, you need to ask yourself is the fraction of a second difference really significantly detrimental to the user experience? If it is, then consider JavaScript, there are a few things you might be able to do, redirects, pauses etc, but these might actually be worse than the original problem. Worse for users, and worse to maintain.
This link might help:
http://paulirish.com/2009/fighting-the-font-face-fout/
Preloading @font-face fonts?
This answer is no longer up to date
Please refer to this updated answer: https://stackoverflow.com/a/46830425/4031815
Deprecated answer
I'm not aware of any current technique to avoid the flicker as the font loads, however you can minimize it by sending proper cache headers for your font and making sure that that request goes through as quickly as possible.
How to wait for webfonts to load?
If you embed the fonts through the "WebFont Loader" (used by Google and Typekit), you can subscribe to the active
event. It will be fired once all fonts have been loaded.
Have a look at the docs: https://developers.google.com/webfonts/docs/webfont_loader#Events
Is a js event emitted when a css font is swapped?
Yes you can do this with Font Face Observer which is a small @font-face to monitor the load of the font. This does not restrict you using any type of font loading.
for example
var font = new FontFaceObserver('My Family', {
weight: 400
});
font.load().then(function () {
console.log('Font is available');
}, function () {
console.log('Font is not available');
});
If you want more information check out https://portalzine.de/dev/options-to-detect-when-a-font-face-has-been-loaded/
Hope that answered your question.
Load font from googleapis after page loading in react.js
Maybe you could append it to the head in a useEffect?
Example:
useEffect(() => {
const link = document.createElement("link");
link.rel = "stylesheet";
link.href =
"https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css";
link.integrity =
"sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z";
link.crossOrigin = "anonymous";
document.head.appendChild(link);
}, []);
google fonts - font family is loaded after page, how to fix it?
Remove display=swap
from the href attribute and replace it with dispay=block
. That should force the browser to wait until the font is loaded before displaying text.
Related Topics
How to Automatically Crop and Center an Image
How to Include View/Partial Specific Styling in Angularjs
Why Does .Foo A:Link, .Foo A:Visited {} Selector Override A:Hover, A:Active {} Selector in Css
How to Set Up Fixed Width For ≪Td≫
Meaning of Numbers in "Col-Md-4"," Col-Xs-1", "Col-Lg-2" in Bootstrap
Ordering of Vendor-Specific CSS Declarations
@Font-Face Works in Ie8 But Not Ie9
How to Vertically Middle-Align Floating Elements of Unknown Heights
Image Greyscale with CSS & Re-Color on Mouse-Over
How to Change the Height of an Image in CSS :Before/:After Pseudo-Elements
How to Increase the Gap Between Text and Underlining in Css
How to Target a :Before or :After Pseudo-Element With a Sibling Combinator
Using Rails 3.1 Assets Pipeline to Conditionally Use Certain Css
Css Selector to Select First Element of a Given Class