Free Astro tutorials for complete beginners.

Beginner's Guide to Astro

How to Add Custom Fonts to Astro

Use Astro's built-in Fonts API to add a Google or Fontsource font in three steps, with the font files served from your own site.

Tested on Astro 7.4

Cover image for How to Add Custom Fonts to Astro
In this guide
  1. Step 1: Register the font
  2. Step 2: Add the Font tag to your layout
  3. Step 3: Use the font in your CSS
  4. Checking it worked
  5. Frequently asked questions

Astro has a built-in way to add fonts. You name the font once in your config, add one tag to your layout, and use it in your CSS. Astro downloads the files and serves them from your site.

Step 1: Register the font

Open astro.config.mjs and add a fonts list. This example uses Inter from Fontsource, a free font library:

import { defineConfig, fontProviders } from 'astro/config';

export default defineConfig({
  fonts: [
    {
      provider: fontProviders.fontsource(),
      name: 'Inter',
      cssVariable: '--font-inter',
    },
  ],
});

To use Google Fonts instead, swap in fontProviders.google(). The rest stays the same.

Step 2: Add the Font tag to your layout

Open src/layouts/Layout.astro, import the component, and add it inside <head>:

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

The preload option tells the browser to fetch the font early. Use it for your main text font, and leave it off for fonts that only appear further down the page.

Step 3: Use the font in your CSS

Anywhere your layout’s styles apply, use the variable:

body {
  font-family: var(--font-inter), sans-serif;
}

The sans-serif at the end is a fallback, shown for a moment while the real font loads.

Restart the dev server after changing astro.config.mjs, then look at your site. The text should change to Inter.

Checking it worked

Run npm run build. Your dist/_astro folder will contain a fonts folder with the downloaded files, and your pages will include the @font-face rules that point at them.

Frequently asked questions

Does this load fonts from Google's servers?

No. Astro downloads the font files when your site builds and serves them from your own site. Visitors never connect to Google.

Can I use a font file I already have?

Yes. Use the local provider and point it at your font files. The Astro docs show the exact settings.

Stuck on a step?

Check the common errors guide, look up a word in the glossary, or ask in the official Astro Discord, where the support channels are friendly to beginners.

Keep reading

  1. How to Deploy an Astro Site to Cloudflare Workers

    Put your Astro site online with Cloudflare Workers in four steps. This is how AstroBeginner itself is hosted, so every step has been run for real.

    Tutorials, 7 minute read, November 6, 2026

  2. How to Add Tailwind CSS to Astro

    Add Tailwind CSS to an Astro site with one command, then style your first element. Tested on Astro 7.4 and Tailwind 4.

    Tutorials, 5 minute read, November 6, 2026

  3. How to Add Images to an Astro Site

    Add an image to your Astro site, and let Astro shrink and convert it automatically. One test image went from 62 kB to 5 kB.

    Tutorials, 6 minute read, November 6, 2026

Try "blog", "deploy" or "EmDash".