liveBook · Manning

Variable font

As explained in CSS in Depth, Second Edition

A variable font is a font file containing one or more adjustable design axes, such as weight or width, within a single font resource.

A variable font puts a range of designs in one font file instead of requiring a separate file for each weight. When a page needs light, regular, and bold text from the same typeface, this can reduce the amount the browser downloads. section 12.5.2 section 12.3

The file defines variation axes: allowable changes to an aspect of the font. A weight axis runs from lighter to bolder designs; an italic axis can instead offer a normal-or-italic choice. In CSS, the useful weight range must also be declared in the font’s @font-face rule. section 12.5.2

Listing 12.11 @font-face rule for a variable font
@font-face {
  font-family: "Open Sans";
  font-style: normal;
  font-weight: 300 800;       #1
  font-display: swap;
  src: url(./open-sans.woff2) format("woff2");
}

Inspect the Open Sans @font-face rule: font-weight: 300 800 declares the range available from one WOFF2 file. section 12.5.2

Choose a weight within the range

After declaring Open Sans with a 300–800 range, set font-family: "Open Sans", sans-serif and font-weight: 467 on an element. The weight need not be a multiple of 100, but it must fall within both the file’s supported range and the @font-face declaration. For ordinary weight selection, font-weight is simpler than addressing the lower-level 'wght' variation axis directly. section 12.5.2

How CSS in Depth, Second Edition covers it

How deep it goes
Defines variation axes and works through an @font-face weight range, precise weight values, and the lower-level 'wght' axis.
Examples
Shows an Open Sans WOFF2 declaration spanning weights 300–800 and an intermediate weight of 467.
What it assumes
Assumes basic CSS declarations while explaining numeric font weights and defining variation axes.
What you can do after
Operate/configure it with @font-face and font-weight to select supported intermediate weights from one file.
How it connects
Connects web fonts, @font-face, font-weight, variation axes, and page-loading performance.

Also explained in

Compare how 2 books explain variable font →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of CSS in Depth, Second Edition. The section links point to the parts of the book it draws on.