Variable Fonts and Font Loading

Intermediate · 22 min

How one variable font replaces a family, how axes are controlled from CSS, and how font-display and fallback metrics stop fonts delaying or shifting a page.

Before thisFont Families and Web Fonts

Kevin Powell · 22 min

Variable Fonts and Font Loading

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Kevin PowellReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A variable font stores a continuous range along one or more axes, such as weight (`wght`), width (`wdth`), slant (`slnt`), italic (`ital`) and optical size (`opsz`), in a single file.
  • Declare the range in `@font-face` (`font-weight: 100 900`), then use any number in it: `font-weight: 450` is valid and picks that exact instance.
  • Registered axes map to normal properties (`font-weight`, `font-stretch`, `font-style`, `font-optical-sizing`). Custom axes have uppercase tags and are set only through `font-variation-settings`.
  • `font-display` chooses what happens while a font loads. `swap` shows fallback text at once and swaps later; `optional` swaps only if the font arrives almost immediately, avoiding layout shift.
  • Reduce shift from swapping by matching the fallback to the web font with `size-adjust` and the metric overrides, and by preloading the one or two files that are used above the fold.

Many files, or one

A family with five weights and matching italics is ten font files. Each is a request and a few dozen kilobytes. Add a condensed width, and the number doubles. A variable font puts the whole design space in one file. The designer draws the extremes (the lightest and the heaviest, say) and the font stores how each outline moves between them. The browser then computes any weight in between on demand.

The inputs are called axes. A font declares which it has, and a range for each.

The registered axes

Five axes have standard four-letter tags and matching CSS properties:

Axis Tag CSS property
Weight wght font-weight
Width wdth font-stretch
Italic ital font-style: italic
Slant slnt font-style: oblique <angle>
Optical size opsz font-optical-sizing (auto by default)

Optical size is the one that rewards a closer look: a font can adjust its letterforms for the size at which they are drawn, giving small text more open spacing and large headlines tighter, finer shapes. With font-optical-sizing: auto, the default, the browser sets that axis from the font-size for you.

Fonts can also define custom axes, named with uppercase tags such as GRAD for grade or CASL for casualness. These have no dedicated property and are set through font-variation-settings.

Using one

You declare the range once, as a pair of numbers:

@font-face {
  font-family: "Inter VF";
  src: url("/fonts/inter-var.woff2") format("woff2");
  font-weight: 100 900;      /* the file covers 100 to 900 */
  font-stretch: 75% 125%;    /* only if the font has a wdth axis */
  font-display: swap;
}

body   { font-family: "Inter VF", system-ui, sans-serif; font-weight: 400; }
strong { font-weight: 650; }  /* any value in the range */

Now font-weight: 650 or font-weight: 450 are real, not rounded to the nearest 100. Transitions and animations between weights work smoothly, because the value is a number, not a name. The same one file serves normal text, bold and everything between, and a variable file is often only a little larger than a single static weight, and much smaller than the full set.

For custom axes, use font-variation-settings:

.display { font-variation-settings: "GRAD" 50, "wght" 800; }

It takes a whole list and replaces any previous list on that element, so a "GRAD" set in one rule disappears if another rule on the same element sets "wght" this way. Custom properties help here: store each axis in a variable, and assemble the property from them, a pattern the video’s last chapter shows.

Finding axes

Browsers’ dev tools have a font panel that lists a variable font’s axes and lets you drag sliders to try them. Sites such as Axis Praxis, Wakamai Fondue and v-fonts (all named in the video’s description) show the axes a font file has. Convert a TTF variable font to WOFF2 before shipping it, like any other.

Loading without pain

Fonts are a downloaded resource, so there is a moment before they arrive. What the page shows in that moment is controlled by font-display in @font-face:

  • block: a short invisible period, then fallback text. The text can be unreadable for a while.
  • swap: almost no invisible period; fallback text appears right away and is swapped when the web font loads. This is the usual choice for body text.
  • fallback: a tiny blocking period and a short swap window; if the font arrives late, it is not used for that view.
  • optional: a tiny blocking period and no swap. The font is used only if it is ready almost at once, which means no reflow, ever, at the cost of sometimes using the fallback for a first visit.
  • auto: browser’s choice.

The swap itself can move text around, because the fallback and web font differ in width and height. This causes layout shift. Three things reduce it:

  1. Preload the one or two files used at the top of the page: <link rel="preload" href="/fonts/inter-var.woff2" as="font" type="font/woff2" crossorigin>. crossorigin is required even on your own domain, since fonts are fetched in CORS mode and a mismatch means a second download.
  2. Match the fallback. @font-face can define a local fallback adjusted to the web font’s proportions with size-adjust, ascent-override, descent-override and line-gap-override. size-adjust is supported in Chrome 92, Firefox 92 and Safari 17; per MDN’s compatibility data the three override descriptors are not yet shipped in Safari.
  3. Subset. unicode-range plus separate files per script keeps the downloaded bytes to what the page uses.

A built-in font-size-adjust property also keeps the apparent size consistent between fallback and web font by aspect value; it is available in current Chrome, Firefox and Safari, though check from-font support before relying on that value.

What the video covers

Kevin Powell’s “Getting started with Variable fonts on the web” is about 22 minutes and is from March 2023. Its chapters: what variable fonts are (0:45), where to find them (4:36), font weights static versus variable (7:00), converting a variable TTF to WOFF2 (8:35), setting up the @font-face declaration for a variable font (10:31), finding the axes a font has (11:35), working with a custom axis (15:00), and using custom properties to make axes easier to use (17:30). It does not cover font loading, so font-display, preload and the metric overrides in this lesson come from the prose and MDN, not the video, and no second video is offered for that half.

Where this fits

Variable fonts finish the “what font, from where” half of typography. The rest is about how text is set: text spacing and readability, then fluid typography, where a smooth weight axis pairs well with smooth sizes. Loading strategy belongs to CSS performance as well, and the frontend roadmap covers Core Web Vitals, the metrics that layout shift from fonts affects.

Check yourself

  1. 01You have `Inter-VF.woff2` with weight range 100 to 900. What `@font-face` descriptor makes `font-weight: 550` work in your CSS?

    `font-weight: 100 900;` inside the `@font-face` rule, which tells the browser the face covers that whole range. Without the range, the declared single weight is the only one the browser treats as matching.

  2. 02What is the difference between setting `font-weight: 700` and `font-variation-settings: "wght" 700`?

    They reach the same axis for registered axes, but `font-weight` takes part in the cascade and font matching like any property, and `font-variation-settings` overwrites all axis values at once. If you set it for one axis, the other custom axes you set elsewhere are lost. Use the high-level properties where they exist.

  3. 03A page uses `font-display: block` and visitors on a slow connection see blank headings. What is the trade-off in switching to `swap`?

    `swap` shows text immediately in a fallback font, which avoids invisible text, but when the web font arrives the text is re-rendered in a different font and may reflow. Matching the fallback's size and metrics reduces that jump, and `optional` removes it by dropping the late font for the first view.

  4. 04Why does `<link rel="preload" as="font" ...>` need the `crossorigin` attribute even for a font on the same domain?

    Fonts are fetched in CORS mode, so a preload without `crossorigin` is made in a different mode and the browser downloads the font twice instead of reusing the preloaded copy.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark