Sub topic 8
Loading Only the Weights and Styles You Need, and Using Variable Fonts
This chapter assumes you can add a link element to a page, write a font-family rule, and open the Network panel in your browser's developer tools. It builds on the embedding chapters for Google Fonts and Adobe Fonts. Here you will learn to request only the font files a page uses, and to decide when a variable font is a better deal than several static files.
Why every weight has a cost
A font family is a set of separate font files. Regular, bold, italic and bold italic are four files, and each one is a download. A family that offers nine weights in upright and italic styles is eighteen files. If your page only uses regular body text and bold headings, the other sixteen are wasted bytes.
Those bytes matter because text usually can't render in the web font until its file arrives. Each extra file also competes for bandwidth with your CSS, images and scripts. The goal is a short, deliberate list of styles. A common budget for a content site is two to four font files in total.
Static fonts: asking for specific weights
Google Fonts builds its stylesheet URL from the families and styles you request. This is the form from the earlier chapter:
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap" rel="stylesheet">The part after the colon is the axis list. wght@400;700 means "the weight axis, at the values 400 and 700". Google returns CSS with one @font-face rule for each requested value. The browser downloads a file only when text on the page actually uses that rule, but the stylesheet itself still describes everything you asked for. Asking for fewer values keeps both the stylesheet and the downloads smaller.
Adding italics
Italic is a second axis called ital, with values 0 (upright) and 1 (italic). When you use more than one axis, the URL syntax changes in three ways:
- List the axes before the
@, separated by commas. - Write each style as a tuple, with values in the same order as the axes.
- Separate tuples with semicolons, sorted in ascending order.
<link href="https://fonts.googleapis.com/css2?family=Lora:ital,wght@0,400;0,700;1,400&display=swap" rel="stylesheet">This requests Lora regular, Lora bold and Lora regular italic. It does not request bold italic. If your CSS then asks for bold italic text, the browser will fake it, as described below.
To load more than one family, repeat the family parameter:
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&family=Lora:ital,wght@0,400;1,400&display=swap" rel="stylesheet">Common URL errors
| Mistake | Result |
|---|---|
| Axes in the wrong order, such as `wght,ital` | The request fails with a 400 error and no CSS loads. Axis names must be sorted alphabetically, with lowercase before uppercase. |
| Tuples out of ascending order | The request fails with a 400 error. |
| A weight the family doesn't offer | The request fails with a 400 error. Check the family's page for its real weights. |
| Spaces in family names | Use `+`, as in `family=Source+Sans+3`. |
What happens when a weight is missing
Suppose your CSS asks for font-weight: 600 but you loaded only 400 and 700. Browsers do not fail. They follow a matching algorithm from the CSS fonts specification:
- If the requested weight is between 400 and 500, the browser tries the weights in that range first, then lighter weights, then heavier ones.
- If the requested weight is below 400, it tries lighter weights first, then heavier ones.
- If the requested weight is above 500, it tries heavier weights first, then lighter ones.
So 600 with only 400 and 700 available renders as 700. A request for 300 renders as 400. The page works but the typography differs from what you designed, and nothing warns you.
A second fallback is more visible. If you ask for bold and the family has no bold file, or italic and no italic file, the browser may synthesize the style. It smears the regular glyphs to fake bold, or slants them to fake italic. The result is usually uneven and looks worse than a real design. You can turn this off:
body {
font-synthesis: none;
}With none, a missing bold or italic simply renders as the nearest real style. Use this during development to expose gaps, because faux styles are easy to miss.
Variable fonts
A variable font stores a whole range of designs in one file. Instead of separate files for 400, 500, 600 and 700, a single file has a weight axis, and the browser interpolates any value between the minimum and maximum. Axes can be registered or custom:
| Axis | CSS property | Meaning |
|---|---|---|
| `wght` | `font-weight` | Stroke thickness, usually 100 to 900 |
| `wdth` | `font-stretch` | Narrow to wide letterforms |
| `ital` | `font-style: italic` | Switches between upright and italic designs |
| `slnt` | `font-style: oblique <angle>` | Slant angle of an upright design |
| `opsz` | `font-optical-sizing` | Adjusts details for small or large text |
Custom axes use four uppercase letters and are specific to a family. They are set only through font-variation-settings.
Requesting a variable font from Google Fonts
Replace the list of values with a range written with two dots:
<link href="https://fonts.googleapis.com/css2?family=Inter:[email protected]&display=swap" rel="stylesheet">Now any weight from 100 to 900 works in your CSS, including values that no static file ever offered:
body {
font-family: "Inter", system-ui, sans-serif;
font-weight: 400;
}
h1 {
font-weight: 800;
}
.label {
font-weight: 550;
}The range form also accepts a narrower span, such as [email protected]. Use it when you know the page never needs the extremes. A smaller range does not always shrink the file, since the font data may be served in full, but it keeps your design intent clear.
For several axes, follow the same ordering rules as before. This example requests Open Sans with both width and weight ranges:
<link href="https://fonts.googleapis.com/css2?family=Open+Sans:wdth,[email protected],300..800&display=swap" rel="stylesheet">You can then use font-stretch: 85% in CSS. For an italic variable family, write tuples such as ital,wght@0,200..900;1,200..900.
Fine control with font-variation-settings
Standard properties such as font-weight and font-stretch are the preferred way to set registered axes. Use font-variation-settings for custom axes, or when no property exists:
.display {
font-variation-settings: "wght" 720, "wdth" 90;
}This property has one trap. It replaces the whole set of settings rather than merging with other rules. If a parent sets "wght" 720 and a child sets only "wdth" 90, the child loses the weight setting. Prefer the regular properties wherever they exist, so cascading works as you expect.
Static or variable: how to decide
A variable file is larger than any single static file, but it can be smaller than several of them combined. The break-even point depends on the family. As a rule of thumb:
- If you need one or two weights, load static files. Two small files are often lighter than one variable file.
- If you need three or more weights, or you want in-between weights or a width axis, load the variable font.
- If the family has no variable version, you have no choice. Check the family's page on Google Fonts, which lists "Variable" or the axes it supports.
Measure rather than assume. Load each version, then compare the transferred size in the Network tab.
Adobe Fonts: choosing styles in the web project
With Adobe Fonts you don't write the weights into a URL. You choose them in the web project. Each family you add to the project lists its available styles, such as 400 normal and 700 normal, and you can include only the ones you need. The project then produces its stylesheet:
<link rel="stylesheet" href="https://use.typekit.net/your-project-id.css">You use the family name and weight from the project page in your CSS:
h1, h2 {
font-family: "proxima-nova", Helvetica, Arial, sans-serif;
font-weight: 700;
}If you request a style the project doesn't include, the same matching and synthesis rules apply as with any other font. Adding a style later means editing the project in Adobe's interface. The <link> stays the same, but it can take a short time for the change to reach visitors because of caching. Adobe Fonts offers variable versions of some families too. When one is available, the project page shows it, and you reference its weight range in CSS as you would with any variable font.
Self-hosted variable fonts
If you self-host, you declare the range in @font-face yourself. A weight range is two numbers separated by a space:
@font-face {
font-family: "Inter";
src: url("/fonts/inter-variable.woff2") format("woff2");
font-weight: 100 900;
font-style: normal;
font-display: swap;
}Without the range, the browser assumes the file only supports one weight and may synthesize or reject values in between. Compare this with the earlier static rule, which declared font-weight: 400 for a single file. Italic variable files need their own @font-face rule with font-style: italic, unless the font has a slnt or ital axis inside a single file.
A checklist for each project
- List every text style in the design: body, bold, italic, headings, captions.
- Map each style to a weight and style, then remove duplicates.
- Request only those values, or one range if you chose a variable font.
- Set
font-synthesis: nonewhile testing, then check that nothing falls back unexpectedly. - Check the Network tab for the number and size of font files.
Exercises
Exercise
A page uses regular body text, bold headings, and italic quotations in Lora. Write the Google Fonts family parameter that requests exactly those three styles, and explain what the browser does if a quotation also contains bold text.
Show answer
Use family=Lora:ital,wght@0,400;0,700;1,400. Tuples follow the axis order ital,wght and are sorted ascending. Bold italic is not loaded, so the browser either synthesizes a faux bold italic from the italic file or, with font-synthesis: none, shows the regular italic.
Exercise
Your CSS sets a label to font-weight: 600, but you loaded only Inter 400 and 700 as static files. Which file does the browser use, and what is one way to get a true 600?
Show answer
The browser uses the 700 file, because for requested weights above 500 it looks at heavier weights first. To get a real 600, request the variable font with [email protected] (or add 600 to the static list, such as wght@400;600;700).
Exercise
A child element sets font-variation-settings: "wdth" 90; and its parent sets font-variation-settings: "wght" 720;. Why might the child's text lose its heavy weight, and how can you avoid this?
Show answer
font-variation-settings replaces the inherited value instead of merging with it, so the child's rule drops the wght setting. Use font-weight: 720 and font-stretch: 90% for these registered axes, or repeat both settings in the child rule.