The simplest way to handle Astro fonts is the built-in Fonts API: list each family in the fonts array of astro.config.mjs with a provider (Google, Fontsource, Bunny, Fontshare, Adobe, npm or your own local files) and a cssVariable, then render <Font cssVariable="..." /> from astro:assets in your head. Astro downloads the files at build time, serves them from your own domain, writes the @font-face rules, and generates metric-matched fallback fonts to reduce layout shift.
This guide covers the config, local files, preloading, Tailwind wiring, the manual @font-face approach, and the performance trade-offs behind each option.
Key Takeaways
- The Fonts API has been stable since Astro 6; it lives under the top-level
fontsconfig key.- Remote providers are downloaded at build time and self-hosted, so visitors never request Google’s servers.
- Only weight
400is included by default. List every weight you use inweights.- Preload one or two files at most, usually the body font’s regular weight.
- Optimized fallbacks are on by default when the last fallback is a generic family like
sans-serif.
Why self-host fonts in Astro
Loading fonts from a third-party CDN costs an extra DNS lookup and connection before the first font byte arrives, and browsers no longer share cached font files between sites, so the old “everyone already has it cached” argument is gone. Self-hosting serves fonts from the same origin as your HTML, over a connection the browser already opened.
There is also a privacy angle. A request to a font CDN sends the visitor’s IP address to a third party, and a German court ruled in 2022 that embedding Google Fonts this way breached the GDPR. Serving files yourself avoids the question entirely.
Fonts are one of the few things that can hurt an otherwise fast Astro site. A late-loading web font causes text to swap and reflow, which shows up as Cumulative Layout Shift. The Astro performance guide covers the other common causes.
Set up the Astro Fonts API
Register each family in the config. This example uses Inter from Google Fonts and a serif display face from Fontsource:
// astro.config.mjs
import { defineConfig, fontProviders } from 'astro/config';
export default defineConfig({
fonts: [
{
provider: fontProviders.google(),
name: 'Inter',
cssVariable: '--font-inter',
weights: [400, 600, 700],
styles: ['normal'],
subsets: ['latin'],
fallbacks: ['system-ui', 'sans-serif'],
},
{
provider: fontProviders.fontsource(),
name: 'Fraunces',
cssVariable: '--font-fraunces',
weights: ['400 700'],
fallbacks: ['Georgia', 'serif'],
},
],
});
Then render the <Font /> component once per family in your base layout’s head:
---
// src/layouts/BaseLayout.astro
import { Font } from 'astro:assets';
import '../styles/global.css';
---
<html lang="en">
<head>
<meta charset="utf-8" />
<Font cssVariable="--font-inter" preload />
<Font cssVariable="--font-fraunces" />
</head>
<body><slot /></body>
</html>
And use the variables in CSS:
/* src/styles/global.css */
body { font-family: var(--font-inter); }
h1, h2, h3 { font-family: var(--font-fraunces); }
The cssVariable value is how the component finds the family, so it must match the config exactly. A typo produces the FontFamilyNotFound error; the font family not found fix shows it in full.
Options that matter
Each family accepts the same set of properties, whatever the provider:
| Option | Default | What it does |
|---|---|---|
weights |
[400] |
Weights to download; use a range like '100 900' for variable fonts |
styles |
['normal', 'italic'] |
Font styles to include |
subsets |
['latin'] |
Character subsets to download |
formats |
['woff2'] |
File formats; WOFF2 is supported by every current browser |
fallbacks |
['sans-serif'] |
Fonts used while loading or if the font fails |
optimizedFallbacks |
true |
Generate a metric-adjusted fallback face |
display |
provider default | The font-display value |
unicodeRange |
none | Limit a face to specific characters |
The weights default catches people out. If your design uses 400 and 700 but the config only lists the default, every bold heading renders in synthetic bold, which looks smeared. List every weight you actually use, and no more, because each one is another file. Leaving styles at its default also downloads italics; drop 'italic' if you never use it.
Self-hosting local font files
For a commercial font, or any file you already have, use the local provider. Keep the files in src/ rather than public/, so Astro can process and fingerprint them without copying a duplicate into the build:
// astro.config.mjs
import { defineConfig, fontProviders } from 'astro/config';
export default defineConfig({
fonts: [
{
provider: fontProviders.local(),
name: 'Brand Sans',
cssVariable: '--font-brand',
fallbacks: ['system-ui', 'sans-serif'],
options: {
variants: [
{ src: ['./src/assets/fonts/BrandSans-Variable.woff2'], weight: '100 900', style: 'normal' },
{ src: ['./src/assets/fonts/BrandSans-Italic-Variable.woff2'], weight: '100 900', style: 'italic' },
],
},
},
],
});
Each entry in variants becomes one @font-face rule. If you leave out weight or style, Astro tries to infer them from the file. Variable fonts are the best fit here: one file covers every weight, so you get the whole range for the cost of a single download.
Preloading the right fonts
preload adds <link rel="preload" as="font"> tags so the browser fetches the font early, before it discovers the @font-face rule in CSS. Used well, it removes the swap entirely for above-the-fold text. Used badly, it competes with your hero image and CSS for bandwidth.
You can preload every file for a family with preload, or filter by weight, style and subset:
---
// src/layouts/BaseLayout.astro (head)
import { Font } from 'astro:assets';
---
<Font
cssVariable="--font-inter"
preload={[{ weight: 400, style: 'normal', subset: 'latin' }]}
/>
A good rule is to preload the body font’s regular weight and nothing else. Headings in a second family can load normally; the fallback face covers them for the few milliseconds they need.
Fallbacks and layout shift
When a web font replaces a fallback, text often changes width, and lines rewrap. Astro reduces this by generating an adjusted fallback @font-face that uses size-adjust, ascent-override and similar descriptors to match the web font’s metrics on a local font. It happens automatically when the last item in fallbacks is a generic family such as sans-serif or serif.
That is why the examples above end their fallback lists with a generic name. Put any specific system fonts you prefer before it, and set optimizedFallbacks: false only if you want full control over the fallback CSS yourself. The Core Web Vitals guide explains how font swaps show up in CLS measurements.
Using Astro fonts with Tailwind CSS
With Tailwind CSS 4, point Tailwind’s font tokens at the variables the Fonts API creates:
/* src/styles/global.css */
@import "tailwindcss";
@theme inline {
--font-sans: var(--font-inter);
--font-display: var(--font-fraunces);
}
font-sans and font-display now use the self-hosted fonts and their fallbacks. The inline keyword makes Tailwind write the variable reference directly into the utilities, which matters because the font variables are defined by the <Font /> output rather than in your stylesheet. See Tailwind CSS in Astro for the full setup, and design tokens with CSS variables if you manage tokens without Tailwind.
The manual approach with @font-face
You can still self-host fonts the traditional way. Place the files in public/fonts/ and write the rules yourself:
/* src/styles/global.css */
@font-face {
font-family: 'Brand Sans';
src: url('/fonts/BrandSans-Variable.woff2') format('woff2');
font-weight: 100 900;
font-style: normal;
font-display: swap;
}
body { font-family: 'Brand Sans', system-ui, sans-serif; }
This works and has no moving parts, but you lose the generated fallbacks, the preload helper and the hashed file names that let you cache fonts forever. Fontsource packages (npm install @fontsource-variable/inter, then import '@fontsource-variable/inter'; in your layout) are a middle ground: they ship ready-made @font-face CSS from npm and are still self-hosted. For a new Astro 7 project, the Fonts API is the better default.
Which approach to use
- Fonts API with a remote provider: most sites. You pick a font by name and get self-hosting, fallbacks and preloads for free.
- Fonts API with the local provider: licensed brand fonts, or files a designer handed you.
- Manual
@font-face: very small sites or legacy projects where you want zero configuration magic. - System font stack only: documentation and tools where speed matters more than brand typography. No downloads at all.
The official Astro fonts guide and the font provider reference list every provider and its options. For where fonts and global styles live in a project, see the styling docs. Typography is also one of the quickest ways to tell Astro themes apart, since each pairs its own fonts with its layout.
FAQ
Does the Astro Fonts API self-host Google Fonts?
Yes. With fontProviders.google(), Astro downloads the font files during the build and serves them from your site, so visitors never make a request to Google’s servers.
Is the Astro Fonts API still experimental?
No. It became stable in Astro 6 and is configured with the top-level fonts key. Older guides that use experimental.fonts refer to the earlier experimental release.
Why is my bold text blurry after adding a font in Astro?
The Fonts API only includes weight 400 by default, so the browser fakes bold. Add the weights you use to the weights array, or a range like '100 900' for a variable font.
Should I preload fonts in Astro?
Preload only the font used for above-the-fold body text, usually its regular weight. Preloading many files competes with CSS and images and can slow the first render.
Where should I put local font files in an Astro project?
Put them in src/assets/fonts/ when using the Fonts API’s local provider, so Astro processes them without duplicating files. Use public/fonts/ only if you write @font-face rules by hand.