Web-Design
Tuesday May 25, 2021 By David Quintanilla
CSS Font Descriptors — Smashing Magazine


Internet fonts are sometimes horrible for net efficiency and not one of the font loading methods are notably efficient to handle that. Upcoming font choices could lastly ship on the promise of creating it simpler to align fallback fonts to the ultimate fonts.

Font loading has lengthy been a bugbear of net efficiency, and there really are no good choices here. If you wish to use net fonts your selections are principally Flash of Invisible Textual content (aka FOIT) the place the textual content is hidden till the font downloads or Flash of Unstyled Textual content (FOUT) the place you utilize the fallback system font initially after which improve it to the online font when it downloads. Neither choice has actually “received out” as a result of neither is basically passable, to be sincere.

Wasn’t font-display Supposed To Resolve This?

The font-display property for @font-face gave that option to the online developer whereas beforehand the browser determined that (IE and Edge favored FOUT up to now, whereas the opposite browsers favored FOIT). Nevertheless, past that it didn’t actually clear up the issue.

Quite a few websites moved to font-display: swap when this primary got here out, and Google Fonts even made it the default in 2019. The pondering right here was that it was higher for efficiency to show the textual content as rapidly as you may, even when it’s within the fallback font, after which to swap the font in when it lastly downloads.

I used to be supportive of this back then too, however am more and more discovering myself annoyed by the “hydration impact” when the online font downloads and characters broaden (or contract) to fill within the subsequent house. Smashing Journal, like most publishers, makes use of net fonts and the beneath screenshot reveals the distinction between the preliminary render (with the fallback fonts), and the ultimate render (with the online fonts):

Two screenshots of a Smashing Magazine article with different fonts. The text is noticeably different sized and an extra sentence can fit in when the web fonts are used.
Smashing Journal article with fallback font and with full net fonts. (Large preview)

Now, when put aspect by aspect, the online fonts are significantly nicer and do match with the Smashing Journal model. However we additionally see there’s fairly some distinction within the textual content format with the 2 fonts. The fonts are very totally different sizes and, due to this, the display screen content material shifts round. On this age of Core Web Vitals and Cumulative Structure Shifts being (fairly rightly!) acknowledged as detrimental to customers, font-display: swap is a poor selection due to that.

I’ve reverted again to font-display: block on websites I take care of as I actually do discover the textual content shift fairly jarring and annoying. Whereas it’s true that block received’t cease shifts (the font continues to be rendered in invisible textual content), it a minimum of makes them much less noticeable to the person. I’ve additionally optimized by font-loading by preloading fonts that I’ve made as small as doable by self-hosting subsetted fonts — so guests usually noticed the fallback fonts for under a small time period. To me, the “block interval” of swap was too brief and I’d truthfully want to attend a tiny bit longer to get the preliminary render right.

Utilizing font-display: non-obligatory Can Resolve FOIT And FOUT — At A Value

The opposite choice is to make use of font-display: non-obligatory. This selection principally makes net fonts non-obligatory, or to place in another way, if the font isn’t there by the point the web page wants it, then it’s as much as the brwoser to by no means swap it. With this feature, we keep away from each FOIT and FOUT by principally solely utilizing fonts which have already been downloaded.

If the online font isn’t out there then, we fall again to the fallback font, however the subsequent web page navigation (or a reload of this web page) will use the font — because it ought to have completed downloading now. Nevertheless, if the online font is that unimportant to the positioning, then it is likely to be a good suggestion to simply take away them fully — which is even higher for net efficiency!

Nevertheless, first impressions depend and to have that preliminary load with out net fonts altogether appears slightly bit an excessive amount of. I additionally assume — with completely no proof to again this up by the way in which! — that it’s going to give folks the impression, maybe subconsciously, that one thing is “off” concerning the web site and will affect how folks use the web site.

So, all font choices have their drawbacks, amongst with the choice to not use net fonts in any respect, or utilizing system fonts (which is limiting — however perhaps not as limiting as many think!).

Making Your Fallback Font Extra Carefully Match Your Font

The holy grail of net font loading has been to make the fallback font nearer to the precise net font to cut back the noticeable shift as a lot as doable, in order that utilizing swap is much less impactful. Whereas we by no means will have the ability to keep away from the shifts altogether, we cab do higher than within the screenshot above. The Font Style Matcher app by Monica Dinculescu is usually cited in font loading articles and provides a unbelievable glimpse of what must be doable right here. It helpfully permits you to overlay the identical textual content in two totally different fonts to see who totally different they’re:

Font Style Matcher screenshots showing two sets over text overlaid on each other with the top having big differences and the bottom having the text very similar.
Font Style Matcher screenshots with the default, similar settings for 2 fonts (prime) and adjusted settings to offer a greater match (backside). (Large preview)

Sadly, the problem with the font fashion matching is that we are able to’t have these CSS kinds apply solely to the fallback fonts, so we have to use JavaScript and the FontFace.load API to use (or revert) these fashion variations when the online font hundreds.

The quantity of code isn’t enormous, however it nonetheless looks like slightly bit extra effort than it must be. Although there are different benefits and prospects to utilizing the JavaScript API for this as defined by Zach Leatherman in this fantastic talk from way back in 2019 — you may scale back reflows and deal with data-server mode and prefers-reduced-motion although that (notice nonetheless that each have since been uncovered to CSS since that speak).

It’s additionally trickier to deal with cached fonts we have already got, to not point out variations in numerous fallback kinds. Right here on Smashing Journal, we strive a variety of fallbacks to make the perfect use of the system fonts totally different customers and working programs have put in:

font-family: Mija,-apple-system,Arial,BlinkMacSystemFont,roboto slab,droid serif,segoe ui,Ubuntu,Cantarell,Georgia,serif;

Realizing which font is used, or having separate changes for every and making certain they’re utilized accurately can rapidly develop into fairly complicated.

A Higher Resolution Is Coming

So, that’s a quick catch-up on the place issues stand as of in the present day. Nevertheless, there’s some smoke beginning to appear on the horizon.

As I discussed earlier, the primary problem with making use of the fallback styling variations was in including, after which eradicating them. What if we may inform the browser that these variations are just for the fallback fonts?

That’s precisely what a brand new set of font descriptors being proposed as a part of the CSS Fonts Module Level 5 do. These are utilized to the @font-face declarations the place the person font is outlined.

Simon Hearne has written a couple of proposed update to the font-face descriptors specification which incorporates 4 new descriptors: ascent-override, descent-override, line-gap-override and advance-override. You possibly can play with the F-mods playground that Simon has created to load your customized and fallback fonts, then play with the overrides to get an ideal match.

As Simon writes, the mix of those 4 descriptors allowed us to override the format of the fallback font to match the online font, however they solely actually modify vertical spacing and positioning. So for character and letter-spacing, we’ll want to supply extra CSS. Nevertheless, it now adjustments with the upcoming size-adjust descriptor.

How does it work? Let’s say you might have the next CSS:

@font-face {
  font-family: 'Lato';
  src: url('/static/fonts/Lato.woff2') format('woff2');
  font-weight: 400;
}

h1 {
    font-family: Lato, Lato-fallback, Arial;
}

Then what you’d do is to create a @font-face for the Arial fallback font and apply adjustor descriptors to it. You’ll get the next CSS snippet then:

@font-face {
  font-family: 'Lato';
  src: url('/static/fonts/Lato.woff2') format('woff2');
  font-weight: 400;
}

@font-face {
    font-family: "Lato-fallback";
    size-adjust: -9900.00%;
    ascent-override: 96%;
    src: native("Arial");
}

h1 {
    font-family: Lato, Lato-fallback, sans-serif;
}

This implies thata when the Lato-fallback is used initially (as Arial is a native font and can be utilized right away with none extra obtain) then the size-adjust and ascent-override settings can help you get this nearer to the Lato font. It’s an additional @font-face declaration to jot down, however actually so much simpler than the hoops we needed to soar by means of earlier than!

Total, there are 4 essential @font-face descriptors included on this spec: size-adjust, ascent-override, descent-override, and line-gap-override with a couple of others nonetheless being thought-about for subscript, superscript, and different use circumstances.

Malte Ubl created a very useful tool to automatically calculate these settings given two fonts and a browser that helps these new settings (extra on this in a second!). As Malte factors out, computer systems are good at that form of factor! Ideally, we may additionally expose these settings for widespread fonts to net builders, e.g. perhaps give these hints in font collections like Google Fonts? That will surely assist improve adoption.

Now totally different working programs could have barely totally different font settings and getting these precisely proper is principally an unimaginable job, however that’s not the goal. The goal is to shut the hole so utilizing font-display: swap is now not such a jarring expertise, however we don’t have to go to the extremes of non-obligatory or no net fonts.

When Can We Begin Utilizing This?

Three of those settings have already been shipped in Chrome since model 87, although the important thing size-adjust descriptor isn’t but out there in any secure browser. Nevertheless, Chrome Canary has it, as does Firefox behind a flag so this isn’t some summary, distant idea, however one thing that might land very quickly.

For the time being, the spec has all types of disclaimers and warnings that it’s not prepared for real-time but, however it actually feels prefer it’s getting there. As at all times, there’s a stability between us, designers and builders, testing it and giving suggestions, and discouraging using it, so the implementation doesn’t get caught as a result of too many individuals find yourself utilizing an earlier draft.

Chrome has acknowledged their intent to make size-adjust available in Chrome 92 due for release on July 20th presumably indicating it’s virtually there.

So, not fairly prepared but, however appears to be like prefer it’s coming within the very close to future. Within the meantime, have a play with the demo in Chrome Canary and see if it could actually go a bit nearer to addressing your font loading woes and the CLS affect they trigger.

Smashing Editorial
(vf, il)





Source link