The syntax for loading variable fonts is very similar to any other web font, with a few notable differences, which are provided via upgrades to the traditional @font-face
syntax now available in modern browsers.
The basic syntax is the same, but the font technology can be specified, and allowable ranges for descriptors like font-weight
and font-stretch
can be supplied, rather than named according to the font file being loaded.
Example for a standard upright (Roman) font
@font-face {
font-family: "MyVariableFontName";
src: url("path/to/font/file/my-variable-font.woff2")
format("woff2-variations");
font-weight: 125 950;
font-stretch: 75% 125%;
font-style: normal;
}
In this case, the normal
value indicates that this font file should be used when in a style rule the font-family
property is MyVariableFontName
and the font-style property is normal
. The oblique 0deg
and oblique 0deg 20deg
values, because of the 0deg
, also indicate the font has normal upright glyphs.
Example for a font that contains only italics and no upright characters
@font-face {
font-family: "MyVariableFontName";
src: url("path/to/font/file/my-variable-font.woff2")
format("woff2-variations");
font-weight: 125 950;
font-stretch: 75% 125%;
font-style: italic;
}
In this case, the italic
value indicates that this font file should be used when in a style rule the font-family
property is MyVariableFontName
and the font-style property is italic
. The oblique 14deg
value also indicates the font has italic glyphs.
Example for a font that contains an oblique (slant) axis
@font-face {
font-family: "MyVariableFontName";
src: url("path/to/font/file/my-variable-font.woff2")
format("woff2-variations");
font-weight: 125 950;
font-stretch: 75% 125%;
font-style: oblique 0deg 12deg;
}
In this case, the oblique 0deg 12deg
value indicates that this font file should be used when in a style rule the font-family
property is MyVariableFontName
and the font-style property is oblique with an angle between zero and 12 degrees inclusive.
Note: Not all browsers have implemented the full syntax for font format, so test carefully. All browsers that support variable fonts will still render them if you set the format to just the file format, rather than format-variations (i.e. woff2
instead of woff2-variations
), but it's best to use the proper syntax if possible.
Note: Supplying value ranges for font-weight
, font-stretch
, and font-style
will keep the browser from attempting to render an axis outside that range if you are using the appropriate attribute (i.e. font-weight
or font-stretch
), but will not block you from supplying an invalid value via font-variation-settings
, so use with care.