To use Astro variable fonts, set weights: ['100 900'] (or the font’s real range) on a font in the fonts array of astro.config.mjs. Astro then fetches the single variable file instead of one file per weight, and any font-weight in that range works in your CSS. Local variable files work the same way with fontProviders.local().
Key Takeaways
- A variable font holds a continuous range of weights (and sometimes width or slant) in one file.
- In the Fonts API, a range string such as
'100 900'requests the variable version.- One file is easier to preload than four or five static weights.
- Use
font-variation-settingsonly for custom axes; usefont-weightandfont-stretchfor the standard ones.
From Google Fonts
// astro.config.mjs
import { defineConfig, fontProviders } from 'astro/config';
export default defineConfig({
fonts: [
{
provider: fontProviders.google(),
name: 'Inter',
cssVariable: '--font-inter',
weights: ['100 900'],
styles: ['normal', 'italic'],
},
],
});
Check the font’s page for its actual range; a font that only spans 300 to 700 should be configured as '300 700'. Then add the <Font> component to your head, as in our Google Fonts tip.
From a local file
Keep the file in src/, not public/, so Astro processes it and does not copy it twice:
// astro.config.mjs
import { defineConfig, fontProviders } from 'astro/config';
export default defineConfig({
fonts: [
{
provider: fontProviders.local(),
name: 'Recursive',
cssVariable: '--font-recursive',
options: {
variants: [
{ src: ['./src/assets/fonts/Recursive-Variable.woff2'], weight: '300 1000', style: 'normal' },
],
},
},
],
});
---
// src/layouts/Layout.astro
import { Font } from 'astro:assets';
---
<head>
<Font cssVariable="--font-recursive" preload />
</head>
Use any weight
/* src/styles/global.css */
body { font-family: var(--font-recursive); font-weight: 400; }
h1 { font-weight: 820; }
.lede { font-weight: 350; }
Intermediate values such as 350 and 820 render exactly, which static fonts cannot do. Variable weights also animate smoothly:
.nav a {
transition: font-weight 150ms ease;
}
.nav a:hover {
font-weight: 700;
}
Note that a weight change alters text width, so hover effects can shift neighboring links. Reserve the space or use it on standalone elements.
Custom axes
Fonts with extra axes, such as Recursive’s casual axis, use font-variation-settings with the axis tag:
.code-note {
font-variation-settings: 'CASL' 1, 'MONO' 1;
}
Use font-weight, font-stretch and font-style for the registered axes (wght, wdth, slnt, ital) so the browser can match them properly. MDN’s variable fonts guide lists the axes, and our self-hosted fonts guide covers file handling. Many of our Astro themes use a variable font already; the theme styling docs show how to change it.
FAQ
Are variable fonts better for performance in Astro?
Usually, when you use three or more weights. One variable file is often smaller than several static files and needs only one preload. If you use a single weight, a static file is smaller.
How do I set the weight range for a variable font in Astro?
Pass a range string in weights, such as ['100 900'], for remote providers, or set weight: '100 900' on the variant for local fonts.
Can I use Fontsource variable fonts in Astro?
Yes. Use fontProviders.fontsource() with a weight range in the Fonts API, or install an @fontsource-variable/* package and import its CSS in your layout.