🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Styling••By Komal Nakrani

Astro Google Fonts the Fast Way: The Built-in Fonts API

Load Astro Google Fonts the fast way: declare the font in astro.config.mjs with fontProviders.google(), add the Font component, and Astro self-hosts and preloads it.

#Astro#Google Fonts#Fonts API#Performance

The fast way to use Astro Google Fonts is the built-in Fonts API: list the font in the fonts array of astro.config.mjs with fontProviders.google(), then add <Font cssVariable="--font-inter" preload /> to your layout’s <head>. Astro downloads the files at build time, serves them from your own domain, preloads the ones you choose and generates a metric-matched fallback to reduce layout shift.

Key Takeaways

  • The fonts config option is stable since Astro 6; no integration is needed.
  • Fonts are self-hosted from your site, so there is no request to Google’s servers in production.
  • Only weight 400 is downloaded unless you list more in weights.
  • Use the CSS variable you chose (var(--font-inter)) in your styles or Tailwind theme.

Configure the font

// 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'],
    },
  ],
});

name is the family name as Google lists it. subsets defaults to latin; add others, such as latin-ext, only if your content needs them. styles defaults to normal and italic, so set it to ['normal'] if you never use italics.

Add it to the head

---
// src/layouts/Layout.astro
import { Font } from 'astro:assets';
---
<html lang="en">
  <head>
    <Font cssVariable="--font-inter" preload />
  </head>
  <body>
    <slot />
  </body>
</html>

<style is:global>
  body {
    font-family: var(--font-inter);
  }
</style>

<Font> outputs the @font-face rules and the CSS variable. With preload, it also adds <link rel="preload"> tags. Preloading every weight can slow the first render, so you can pass a filter instead, such as preload={[{ weight: 400 }]}; our preload fonts tip covers that.

Use it with Tailwind CSS 4

Point Tailwind’s font token at the variable:

/* src/styles/global.css */
@import 'tailwindcss';

@theme {
  --font-sans: var(--font-inter);
}

Now font-sans and the default body font use Inter.

The <link href="https://fonts.googleapis.com/css2?..."> snippet works, but it adds a connection to two extra domains before text can render, and it sends visitor requests to a third party, which some privacy policies have to disclose. The Fonts API removes both: the files ship with your site. Our Fonts API deep dive explains the internals and the official fonts guide lists every option.

Common error

If you see “Font family not found.” with “No data was found for the "--font-inter" family passed to the <Font> component.”, the cssVariable on <Font> does not match the config. Our font family not found fix walks through it. To swap the font in one of our Astro themes, see the theme styling docs.

FAQ

How do I add Google Fonts to Astro?

Add the font to the fonts array in astro.config.mjs with fontProviders.google(), then place <Font cssVariable="..." /> from astro:assets in your layout’s head. Use the CSS variable in your styles.

Does the Astro Fonts API self-host Google Fonts?

Yes. Astro downloads the font files during the build and serves them from your site, so visitors never request files from Google.

Yes. Paste it in your layout’s <head>. It works, but it is slower and depends on a third party, so the Fonts API is the better default.