Typography

Variable fonts in CSS: weight, width and custom axes

One variable font file can replace a whole family. Here is how the weight, width, slant and optical size axes map to CSS, how to declare them in @font-face, and how to set custom axes without the inheritance trap.

Variable fonts in CSS let one font file cover a whole range of styles. You control them with ordinary properties: font-weight for the weight axis, font-stretch for width, font-style for slant and italic, and font-optical-sizing for optical size. Only custom axes, such as grade or roundness, need the lower-level font-variation-settings.

This guide covers the five standard axes, the @font-face setup, custom axes and the inheritance trap that catches most people, plus when a variable font actually saves bytes.

Key takeaways

  • A variable font stores every style along one or more axes in a single file; any value in the range works, such as a weight of 450.
  • Declare ranges in @font-face, for example font-weight: 100 900.
  • Use font-weight, font-stretch and font-style for registered axes; save font-variation-settings for custom ones.
  • Setting font-variation-settings replaces every axis it doesn’t mention, so use custom properties to change one at a time.
  • All current browsers support variable fonts.

What is a variable font?

A traditional family ships a separate file for each style: Light, Regular, Medium, Bold and so on. A variable font stores the outlines once, with rules for how they change along each axis. You can pick any point on that axis, so 450 or 650 is as easy as 400 or 700, and one file often replaces six or more.

How to tell if a font is variable

  • Check the file name. Variable versions are often labelled “Variable”, “VF” or with the axes in brackets, such as Inter[opsz,wght].ttf.
  • Check the download. Many families ship a folder of static files and a separate variable file. Use the variable one for the web.
  • Drop it into a tester. If it shows sliders, it’s variable; if it shows a single fixed style, it’s static.

Variable fonts also include named instances, such as “SemiBold” at weight 600. These are shortcuts to points on the axes and appear in design apps, but on the web you can use any value, not just the named ones.

The five registered axes

AxisTagCSS propertyExample
Weightwghtfont-weightfont-weight: 650;
Widthwdthfont-stretchfont-stretch: 87.5%;
Slantslntfont-style: obliquefont-style: oblique 10deg;
Italicitalfont-style: italicfont-style: italic;
Optical sizeopszfont-optical-sizingfont-optical-sizing: auto;

Registered axes use lower-case tags. Optical size is usually left on auto, which lets the browser match it to the font size; setting a specific number needs font-variation-settings.

Setting up @font-face for a variable font

@font-face {
  font-family: "Geist";
  src: url("/fonts/geist-variable.woff2") format("woff2");
  font-weight: 100 900;       /* the font's weight range */
  font-style: normal;
  font-display: swap;
}

h1     { font-weight: 750; }
p      { font-weight: 420; }
strong { font-weight: 650; }

The two-value font-weight tells the browser this one file covers every weight from 100 to 900. Without the range, it treats the font as a single weight and may fake bold by thickening the outlines. If the font has a width axis, add a range for font-stretch too, such as 75% 125%.

Custom axes and font-variation-settings

Designers can add their own axes, written with upper-case tags: GRAD (grade) changes thickness without changing letter widths, and ROND might soften corners. These have no ordinary CSS property, so you set them with font-variation-settings:

.button-label {
  font-variation-settings: "GRAD" 150;
}

Grade is handy for hover states and dark mode: text can look slightly bolder without shifting the layout, because the letters stay the same width.

The inheritance trap

font-variation-settings sets all of its axes at once. If a parent sets "GRAD" 150, "ROND" 50 and a child sets only "ROND" 100, the child loses the grade entirely. MDN’s recommended fix is to put each axis in a custom property and set the property once:

:root {
  --grad: 0;
  --rond: 0;
}
body {
  font-variation-settings: "GRAD" var(--grad), "ROND" var(--rond);
}
.soft { --rond: 100; }   /* only roundness changes */
.dark { --grad: -25; }   /* only grade changes */

The same reasoning is why registered axes should use the normal properties: font-weight inherits on its own, works with <strong> and headings, and doesn’t wipe out other settings.

Tip: Weight and width can be animated with an ordinary CSS transition, which makes subtle hover effects easy. Keep them short, and turn them off under prefers-reduced-motion.

Where to find good variable fonts

Google Fonts lists hundreds of variable families and lets you filter by axis, and most open source families on GitHub now ship a variable file alongside the static ones. Inter, Roboto Flex, Recursive and Fraunces are popular starting points because they offer more than just weight. Check the licence before self-hosting: open fonts under the SIL Open Font Licence can be served from your own domain, while commercial fonts often charge separately for web use.

Do variable fonts make pages faster?

Usually, if you use more than two or three styles. One variable file is often larger than any single static weight, but smaller than four or five static files together, and it saves the extra requests. If your site only uses Regular and Bold, two subset static files can still be lighter. Either way, subset and compress the font: the guide to WOFF2 and subsetting covers that, and axes survive the process.

Explore a font’s axes

Drop any font into the variable font playground to see every axis it has, drag the sliders, step through its named styles and type your own text. It writes the @font-face rule with the right ranges and a class that uses font-weight, font-stretch and font-style where it can, falling back to font-variation-settings only for custom axes. Nothing is uploaded. Then shrink the file with the font converter and size it responsively with the fluid typography calculator.

Sources: MDN: Variable fonts guide.