Skip to content
Tutorials Center
My learning

Topics

Lesson 42 of 139 ยท Text and Fonts

Variable Fonts and OpenType Features

Unlock hidden font features: font-variant-* and font-feature-settings for numerals, ligatures and small caps, and font-variation-settings for variable axes.

A font file is more than a set of letter shapes. Professionally made fonts carry extra designs and rules inside them: numerals that line up in a table, fractions like ยฝ built from tiny parts, small capitals, alternate letterforms, and ligatures that join letter pairs such as "fi". Modern variable fonts go further and let you slide between designs, such as light to heavy or narrow to wide.

All of this is switched on from CSS. The tricky part is knowing which property to use and what to expect when a font simply does not contain the feature. This lesson focuses on exactly that: the OpenType features you can turn on, the high-level font-variant-* properties, the low-level font-feature-settings escape hatch, and the axes of variable fonts. It builds on Fonts and Typefaces and Web Fonts.

Features are in the font, CSS only asks for them

OpenType features are named by four-letter tags, such as liga (standard ligatures), tnum (tabular numbers) or smcp (small capitals). CSS can request a feature, but the font must actually contain it. If it does not, the request is silently ignored. That is why the same page can look different on two devices.

Two practical consequences:

  • Test with the font your visitors will really get. For features that matter, use a web font you ship yourself.
  • To see which features a font offers, drop it into a font inspector tool (several free ones exist online, or use the Fonts panel in browser developer tools, see DevTools).

The font-variant-* family: the friendly way

CSS groups the most common features into readable properties. Prefer these over raw tags, because they also work as fallbacks (a browser can synthesize small caps if the font lacks them).

Property Common values What it controls
font-variant-numeric tabular-nums, proportional-nums, oldstyle-nums, lining-nums, slashed-zero, ordinal, diagonal-fractions How digits and fractions look
font-variant-ligatures common-ligatures, no-common-ligatures, discretionary-ligatures, none Joined letter pairs
font-variant-caps small-caps, all-small-caps, petite-caps, titling-caps, unicase Capital styles
font-variant-position sub, super Subscript and superscript glyphs
font-variant-east-asian jis78, simplified, traditional, ruby... CJK variants
font-kerning auto, normal, none Pair spacing adjustments

Values from one property can be combined: font-variant-numeric: tabular-nums slashed-zero;. The shorthand font-variant sets them all at once, but is rarely worth it.

Numbers that behave

Numerals come in two spacing styles and two height styles:

  • Tabular digits are all the same width, so columns of numbers line up and a ticking timer does not wobble. Proportional digits get natural spacing (a narrow 1 and a wide 0) and look better in running text.
  • Lining digits are all capital height, like 1234567890. Oldstyle digits have varying heights, with 3, 4, 5, 7, 9 dropping below the baseline, and blend into lowercase text.
Numbers in tables and in text

Whether the two tables look different depends on the font. Some fonts use tabular digits by default, and some have no oldstyle set. If you see no change, your system font does not offer that feature, which is exactly the situation a self-hosted font avoids.

Ligatures and kerning

Ligatures replace awkward letter pairs with one glyph, such as "fi", "fl" or "ff". Most body fonts turn common ligatures on by default. Turn them off when they cause trouble: in code, in letter-spaced text (ligatures break when you add letter-spacing), or when a ligature looks wrong in a specific word.

CSS
code, .tracked {
  font-variant-ligatures: none;   /* no joined glyphs */
}
.fancy {
  font-variant-ligatures: common-ligatures discretionary-ligatures;
}

Programming fonts use ligatures for operators (=> or != becoming a single symbol). If you do not want that in code samples, set font-variant-ligatures: none.

Small capitals, subscripts and superscripts

font-variant-caps: small-caps shows lowercase letters as smaller capitals, which is elegant for acronyms inside sentences ("NASA" in a paragraph shouts, "nasa" does not). If the font lacks real small caps, browsers synthesize them by scaling capitals, which looks thinner than true small caps but still works.

Small caps and super/subscripts

For true semantics, such as footnotes or chemical formulas, keep using <sup> and <sub>. font-variant-position is a typographic refinement for fonts that include properly drawn glyphs.

font-feature-settings: the raw tags

When a feature has no friendly property, such as stylistic sets (ss01, ss02...), character variants (cv01...) or contextual alternates in a specific font, use font-feature-settings with the four-letter tag and a value (1 or on, 0 or off; some features accept numbers).

CSS
.heading {
  font-feature-settings: "ss01" 1, "liga" 0, "tnum" 1;
}

Two cautions:

  1. It replaces, it does not merge. Setting font-feature-settings on a child resets everything the parent set with that property. If you set it in several places, repeat all the tags you want, or use custom properties to combine them.
  2. Prefer font-variant-* when one exists. They cascade independently, so font-variant-numeric on a table and font-variant-ligatures on the body do not interfere.

Because stylistic sets differ completely from font to font (in one font ss01 is a single-story "a", in another it is something else), you must look them up for the specific font you ship.

A feature switchboard

Toggle the boxes. Some will visibly change the text with your font and some will do nothing. Both outcomes are correct: the text shows only what the font supports.

Variable fonts and their axes

A variable font stores a design space instead of single designs. Each dimension of that space is an axis. The font declares each axis's minimum, default and maximum, and you can choose any value in between.

Axis Tag CSS property Typical range
Weight wght font-weight 100 to 900
Width wdth font-stretch (as a percentage) 75% to 125%
Slant slnt font-style: oblique <angle> -10 to 0 degrees
Italic ital font-style: italic 0 or 1
Optical size opsz font-optical-sizing: auto 8 to 144
Custom axes any four letters, uppercase font-variation-settings font-specific

Standard (registered) axes have lowercase tags and normal CSS properties, which you should prefer. Custom axes have uppercase tags (such as GRAD for grade or XOPQ) and can only be set with font-variation-settings:

CSS
@font-face {
  font-family: "Harbor Sans";
  src: url("/fonts/harbor-sans-var.woff2") format("woff2");
  font-weight: 100 900;       /* declare the supported ranges */
  font-stretch: 75% 125%;
}

.title   { font-family: "Harbor Sans"; font-weight: 720; font-stretch: 90%; }
.dark-ui { font-variation-settings: "GRAD" -25; }  /* thinner strokes on dark backgrounds */

Declaring ranges in @font-face is essential. Without font-weight: 100 900, the browser assumes the file has one weight and may clamp or fake the rest.

font-variation-settings pitfall: like font-feature-settings, it replaces rather than merges. It also overrides font-weight and font-stretch for the axes it mentions, so a stray "wght" 400 can silently cancel your weight settings. Use custom properties to compose axes:

CSS
.text {
  --grade: 0;
  --opsz: 14;
  font-variation-settings: "GRAD" var(--grade), "opsz" var(--opsz);
}
.dark .text { --grade: -25; }

Why axes are worth using

  • Fewer files. One variable file can replace six static ones.
  • Fine control. Weight 550 for UI labels, a slightly condensed width to fit a narrow column.
  • Optical sizing. With font-optical-sizing: auto, large text uses tighter, more elegant shapes and small text gets sturdier, more open ones. It is on by default in modern browsers.
  • Smooth animation. Axes are animatable, so a heading can get bolder on hover with a transition.
Axis playground with a weight transition

The code uses the standard properties font-weight and font-stretch, not font-variation-settings, because registered axes have first-class CSS properties.

Performance and fallbacks

  • A variable font file is larger than one static file, but usually smaller than the several static files it replaces. Subset it to the scripts you need.
  • Always supply a sensible fallback stack. If the variable font fails to load, font-weight: 650 snaps to the nearest weight of the fallback.
  • Use @supports (font-variation-settings: normal) to gate rules that only make sense for variable fonts.
  • Features cost nothing at runtime: they just choose different glyphs from a file you already downloaded.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What happens if you request an OpenType feature, such as "smcp", that the font does not contain?

    Features live in the font file. CSS can only ask for them. Missing features are ignored, though some font-variant values have synthesized fallbacks.
  2. 2. Which is the better choice when a friendly property exists?

    font-variant-* properties do not reset each other, while font-feature-settings replaces its whole value wherever it is set.
  3. 3. What does font-variant-numeric tabular-nums do?

    Tabular figures have equal widths, which keeps tables, prices and timers aligned.
  4. 4. Which axis tags can be set through normal CSS properties rather than font-variation-settings?

    Registered axes map to font-weight, font-stretch, font-style and font-optical-sizing. Custom axes use uppercase tags and font-variation-settings.
  5. 5. Why must an @font-face rule for a variable font declare a range such as font-weight 100 900?

    The range tells the browser which weights the single file can produce, so it can pick any value in between.

Summary

  • OpenType features are four-letter tags inside the font; CSS only requests them, and unsupported requests are ignored.
  • Prefer font-variant-numeric, -ligatures, -caps and -position over raw font-feature-settings; remember that the latter replaces its whole value.
  • Use tabular numerals for tables and timers, lining or oldstyle numerals for style, and turn ligatures off in code and letter-spaced text.
  • Variable fonts expose axes: registered ones (wght, wdth, slnt, ital, opsz) have normal CSS properties, custom uppercase ones use font-variation-settings.
  • Declare axis ranges in @font-face, compose settings with custom properties and test with the font you actually ship.

Next lesson: Line Height and Vertical Rhythm, where you tune the vertical rhythm of your text.