{"id":305626,"date":"2020-03-25T06:33:56","date_gmt":"2020-03-25T13:33:56","guid":{"rendered":"https:\/\/css-tricks.com\/?post_type=chapters&p=305626"},"modified":"2021-10-06T07:39:09","modified_gmt":"2021-10-06T14:39:09","slug":"perfect-font-fallbacks","status":"publish","type":"chapters","link":"https:\/\/css-tricks.com\/books\/greatest-css-tricks\/perfect-font-fallbacks\/","title":{"rendered":"Perfect Font Fallbacks"},"content":{"rendered":"\n
When you load custom fonts on the web, a responsible way to do that is to make sure the With that in place, there will be no delay<\/em> for a user loading your page before they see text. That’s great for performance. But it comes with a design tradeoff, the user will see FOUT or “Flash of Unstyled Text”. Meaning they’ll see the page load with one font, the font will load, then the page will flip out that font for the new one, causing a bit of visual disruption and likely a bit of reflow.<\/p>\n\n\n\n This trick is about minimizing<\/em> that disruption and reflow!<\/p>\n\n\n\n This trick comes by way of Glen Maddern<\/a> who published a screencast about this at Front End Center<\/a> who uses Monica Dinculescu’s Font style matcher<\/a> combined with Bram Stein’s Font Face Observer<\/a> library. <\/p>\n\n\n\n Let’s say you load up a font from Google Fonts. Here I’ll use Rubik<\/a> in two weights:<\/p>\n\n\n\n At the end of that URL, by default, you’ll see On a slow connection, this is how a simple page with text will load:<\/p>\n\n\n\n@font-face<\/a><\/code> declaration uses the property
font-display<\/a><\/code> set to a value like
swap<\/code> or
optional<\/code>. <\/p>\n\n\n\n
@font-face {\n font-family: 'MyWebFont'; \/* Define the custom font name *\/\n src: url('myfont.woff2') format('woff2'); \/* Define where the font can be downloaded *\/\n font-display: swap; \/* Define how the browser behaves during download *\/\n}<\/code><\/pre>\n\n\n\n
@import url(\"https:\/\/fonts.googleapis.com\/css2?family=Rubik:wght@400;900&display=swap\");<\/code><\/pre>\n\n\n\n
&display=swap<\/code> which is how they make sure
font-display: swap;<\/code> is in the
@font-face<\/code> declaration.<\/p>\n\n\n\n