Faster web fonts
iainbean.com
iainbean.com
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Fonts/V...
I don’t know why more websites don’t do this. Even having one large CSS file with everything compressed is much much faster then the waterfall of more files/connections.
I recommend postcss-import that takes your file paths and embeds the files during build. Use responsibly.
I could make a similar argument to say not to use CSS files, just embed everything into the page.
Unless you have a ton of files, listing them all in the html will make them load just about as fast as a single file listed in the html.
Then the poor user has to download heavier html on every page as the browse the site.
- base64 encoded strings are larger than the real file size.
- It blocks browser rendering. Browsers can still render a page while it's downloading the fonts. Not while it is downloading CSS.
- Makes cache busting hard. You will update CSS files more often than fonts. Fonts, served with right headers, can stay longer in browser cache.
- Base64 encoded strings don't compress well. I go as far as sorting the attributes/combined classes to increase the compressibility of the CSS, but base64 strings hardly compress.
You won't see FOIT/FOUT because the browser is outright blocking the rendering until the CSS is completely downloaded. That is arguably a worse outcome.
Simply because it's not very performant. base64 will make the file about 30% larger, and web fonts are already too big. It'll also block the rest of the CSS file which can delay layout calculations, and the font-display property won't work either.
1: LCP – Largest Contentful Paint – a core web vital metric representing time for the visible viewport to be stable. This is better than the old "document ready" because document ready is technical, hundred other things like reflows can happen after document ready. Whereas LCP represents from a user perspective when the viewport is ready. When fonts create reflows, the viewport becomes unstable and LCP will be higher. Lower LCP = Better user-perceived loading time. https://web.dev/lcp/
It's extra resources, time, and complexity spent on forcing something on the visitor that in most situations they don't want or care about.
(The "font-display" setting is also a setting that the user might want to change even if font downloading is enabled. My idea of "meta-CSS" would give you this and many other things for free. Meta-CSS would not inherently allow the timing to be adjusted, but it is a feature that can be added too if it is wanted.)
Another thing that I might want is to enable the document to specify its own fonts if it is SVG or PDF, but to disallow HTML documents from specifying their own fonts.
I very much doubt that's true for the average user, who will simply be using the browser's built-in default – which to an not inconsiderable extent is rather constrained by backwards compatibility concerns rather than being truly optimised for usage as "the only stylesheet you'll ever need".
Besides, even with a boring plain old homepage-and-definitively-not-a-web-app it's not too difficult to run into the limitations of what you can achieve without some custom styling even purely in terms of basic layouting and typography.
It's unfortunate Courier New is both so badly different from the regular fonts (and so bad at all, by the way) and also the default on Windows.
Using Courier New for code samples is not a great idea. I think it comes from the times when good monospace fonts did not yet exist. I always change this default, andvi wish browsers somehow stopped clinging to it.
Why not just let system designers choose the fonts their users are subjected to by default.
At least they seem to care more about accessibility than XYZ.coms marketing department.
* I don’t know there’s a config at all
* I know there’s a config, but I don’t know enough about fonts to make a good choice
* Most sites override my config anyways (unless I chose an even more aggressive config that breaks websites), so why would I care?
* The idea of some site specifying a font does not bother me, even if it’s a web font that will cause an extra 25kb of download.
For what it’s worth, I really like fonts and know my way around the Firefox config, but I honestly do not care enough to change the default font because of the last two items on that list.
* I know there's a config, but this is my work machine and corporate IT has blocked the browser prefs dialog via group policy
In that StackExchange post you linked to, they mentioned make their own branded web font. That's definitely what I would be advocating for if I were there. The case for Github making their own font is even stronger. Microsoft has a very good font team. Designing a family of brand fonts for GitHub would be a very cool project (How many brands care about their fixed width font as much as github?)
You, personally, should be glad web fonts are a thing because it makes it easy for you to restyle them on your computer. If a world without web fonts that wouldn't be possible.
> This is a good base because it lets website visitors start reading your content right away
They're already reading the content. The one job your website had has already been accomplished without the custom font.
As this guy[1] said, you first serve what's essential for your website's functionality, and then you stop.
I’ve been using it for a few months on my personal statically generated site and it works surprisingly well.
You can also limit how many variants you use. On my website, I dont have bold + italic text, to avoid loading a font. My secondary font is only loaded in one variant.
I also realised small performance gains by inlining my CSS (although HTTP2 Push negates the benefits). This only makes sense for smaller stylesheets though.
Even then, fonts are one of the last performance bottlenecks on my tiny and fast website. That's only visible on absurdly slow connections.
Worth noting that Push has been removed from Chrome, which likely means it’ll eventually go away everywhere else it’s implemented.
> You can also limit how many variants you use. On my website, I dont have bold + italic text, to avoid loading a font. My secondary font is only loaded in one variant.
This is definitely good advice. On my site, I only load one font, used only for headings. Body text certainly can benefit from custom fonts too. But IMO if your headings stand out and the rest of your design is strong with good line spacing, it doesn’t hurt to try out common web fonts or a system font stack for the body text.
> I also realised small performance gains by inlining my CSS (although HTTP2 Push negates the benefits). This only makes sense for smaller stylesheets though.
This seems to generally be the trend, but when I built my site I decided to go the opposite direction and extract all of my styles into a single external CSS file for caching benefits. This turned out to have a negative impact on perceived load time—particularly blocking fonts, but also just some TTFP generally—so I manually inlined what felt like the most critical styles, which helped a lot. First load feels pretty much the same as inline on my connection, which isn’t especially fast, and subsequent loads get it cached.
I could probably improve it further with some page-specific splitting/inlining, but haven’t felt the need to prioritize that yet.
> I could probably improve it further with some page-specific splitting/inlining
My website(s) are very simple, so the CSS files are small. Even the larger pages are around 50 kilobytes plus images. That's why inlining is a no-brainer in my case.
[1] https://groups.google.com/a/chromium.org/g/blink-dev/c/K3rYL...
And I’m probably going to port it to another SSG framework[1] when I get the time and energy, and I’ll probably change a fair bit of tooling in the process.
there is no step two
I wish there was some font cache that could be shared between all sites, so once you downloaded a font it would be available for all. But I guess due to copyright it will never happen.
I'd rather just view the page with the default fonts rather than wait 5-6 secondsfor the minimally different sans serif font that you've chosen for one paragraph that blocks the entire page render.
Hypotheticals aside, the fonts load sufficiently fast in a single round trip (two if HTTP2 push isn't supported), and do not block rendering.
Second, I use Debian and I have a fonts.conf that's hundreds of lines long, including dozens of lines of hinting settings because I prefer that some fonts use hintfull and others use hintslight. I don't have hinting settings for every font that exists, only fonts I installed, so when a site uses a web font, there is no dedicated hinting setting for it in my fonts.conf, and so it almost always looks worse than my default font.
And frankly, it does make a difference.
Too bad those very expensive fonts never come with a decent web license and you end up going for a half-arsed substitute.
The problem aren’t necessarily fonts, but print-first design systems that don’t translate well to screens and interactivity.
The font of a website does not rate highly on my interest rating.