Lots from Divya with the setup:
There are 2 kinds of problems that can arise when using webfonts; Flash of invisible text (FOIT) and Flash of Unstyled Text (FOUT) ... If we were to compare them, FOUT is of course the lesser of the two evils
If you wanna fight FOIT, the easiest tool is the font-display
CSS property. I like the optional
value because I generally dislike the look of fonts swapping.
If you want to fight them both, one option is to preload the fonts:
<link rel="preload" href="/fonts/awesome-l.woff2" as="font" />
But...
Preload is your friend, but not like your best friend ... preloading in excess can significantly worsen performance, since preloads block initial render.
Even huge sites aren't doing much about font loading perf. Roel Nieskens:
I expected major news sites to be really conscious about the fonts they use, and making sure everything is heavily optimised. Turns out a simple Sunday afternoon of hacking could save a lot of data: we could easily save roughly 200KB
Fonts are such big part of web perf, so let's get better at it! Here's Zach Leatherman at the Performance.now() conference:
Part of the story is that we might just have lean on JavaScript to do the things we need to do. Divya again:
Web fonts are primarily CSS oriented. It can therefore feel like bad practice to reach for JavaScript to solve a CSS issue, especially since JavaScript is a culprit for an ever increasing page weight.
But sometimes you just have to do it in order to get what you need done. Perhaps you'd like to handle page visibility yourself? Here's Divya's demonstration snippet:
const font = new fontFace("My Awesome Font", "url(/fonts/my-awesome-font.woff2)" format('woff2')")
font.load().then (() => {
document.fonts.add(font);
document.body.style.fontFamily = "My Awesome Font, serif";
// we can also control visibility of the page based on a font loading //
var content = document.getElementById("content");
content.style.visibility = "visible";
})
But there are a bunch of other reasons. Zach has documented them:
- Because, as you load in fonts, each of them can cause repaints and you want to group them.
- Because the user has indicated they want to use less data or reduce motion.
- Because you'd like to be kind to slow networks.
- Because you're using a third-party that can't handle
font-display
.
The post Fighting FOIT and FOUT Together appeared first on CSS-Tricks.
from CSS-Tricks https://ift.tt/2S92D2s
via IFTTT
No comments:
Post a Comment