Home >Web Front-end >CSS Tutorial >How Can We Detect When CSS `@font-face` Fonts Have Loaded to Prevent Broken Icon Display?
Detecting Font Loading Status for CSS @font-face
Issue:
When integrating a font using @font-face, icons may initially appear as unstyled circles with ⓘ symbols instead of their intended design. This issue arises because the font files take time to load.
Query:
How can we determine if the @font-face font files have been loaded to ensure proper icon display?
Answer:
jQuery-FontSpy.js Plugin:
This open-source plugin detects font loading status by comparing the width of a string rendered in Comic Sans MS (a widely available font) with the same string rendered in the custom font you wish to load. When the widths differ, the custom font has been successfully loaded.
Customization:
Usage:
$('.bannerTextChecked').fontSpy({ onLoad: 'hideMe', onFail: 'fontFail anotherClass' });
Example:
.hideMe { visibility: hidden !important; } .fontFail { visibility: visible !important; /* Style to resemble the custom font */ }
Additional Notes:
The above is the detailed content of How Can We Detect When CSS `@font-face` Fonts Have Loaded to Prevent Broken Icon Display?. For more information, please follow other related articles on the PHP Chinese website!