🔥 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)
Astro Themes••By Komal Nakrani

Change Astro Theme Fonts With the Fonts API

Change Astro theme fonts cleanly: find how the theme loads fonts, swap in a new family with the Fonts API, wire it into Tailwind and remove the old files.

#Astro#Astro Theme Fonts#Fonts#Astro Themes

To change Astro theme fonts, find how the theme loads its current font (a Google Fonts <link>, a Fontsource import, local files, or Astro’s Fonts API), remove that, register the new family in the fonts array of astro.config.mjs, and point the theme’s font token at the new CSS variable. Components that use font-sans or var(--font-sans) switch over without further edits.

Key Takeaways

  • Find every place the old font is loaded before adding a new one, or you’ll ship both.
  • The Fonts API self-hosts the files at build time and generates metric-matched fallbacks.
  • Reuse the theme’s existing font token so you change one value, not every component.
  • List each weight you use; only 400 is included by default.

Find how the theme loads fonts

# Terminal: run from the theme root
grep -rn "fonts.googleapis\|@fontsource\|@font-face\|fontFamily\|cssVariable" src astro.config.* tailwind.config.* 2>/dev/null

You’ll get one of four answers: a <link> to Google Fonts in the base layout, an @fontsource/... import, an @font-face rule with files in public/fonts/, or a fonts entry in astro.config.mjs. Note the font token the theme uses too, usually fontFamily.sans in the Tailwind config or a --font-sans variable. The styling docs list where Komal themes define it.

Register the new font

Add the family to the config. This swaps the body font to Inter and the heading font to Fraunces, keeping the variable names the theme already uses:

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

export default defineConfig({
  site: 'https://www.example.com',
  fonts: [
    {
      provider: fontProviders.google(),
      name: 'Inter',
      cssVariable: '--font-sans',
      weights: [400, 600, 700],
      styles: ['normal'],
      fallbacks: ['system-ui', 'sans-serif'],
    },
    {
      provider: fontProviders.fontsource(),
      name: 'Fraunces',
      cssVariable: '--font-display',
      weights: ['400 700'],
      fallbacks: ['Georgia', 'serif'],
    },
  ],
});

Then render one <Font /> per family in the base layout’s head, and delete the old <link> or import:

---
// src/layouts/BaseLayout.astro
import { Font } from 'astro:assets';
---
<head>
  <meta charset="utf-8" />
  <Font cssVariable="--font-sans" preload />
  <Font cssVariable="--font-display" />
</head>

If the build fails with “Font family not found”, the cssVariable passed to <Font /> doesn’t match the config. The font family not found fix walks through it.

Point the theme’s token at the new font

In a Tailwind CSS 3 theme, make the font utilities read the variable:

// tailwind.config.mjs
import defaultTheme from 'tailwindcss/defaultTheme';

/** @type {import('tailwindcss').Config} */
export default {
  content: ['./src/**/*.{astro,html,js,ts,md,mdx}'],
  theme: {
    extend: {
      fontFamily: {
        sans: ['var(--font-sans)', ...defaultTheme.fontFamily.sans],
        display: ['var(--font-display)', 'serif'],
      },
    },
  },
};

Without Tailwind, set it in global CSS:

/* src/styles/global.css */
body { font-family: var(--font-sans); }
h1, h2, h3 { font-family: var(--font-display); }

Clean up and check

Remove the old font package (npm uninstall @fontsource/old-font) and any files in public/fonts/ you no longer reference. Run npm run build, open the built site, and check in the DevTools Network panel that only the new font files load. Headings with heavier weights need those weights listed in weights, or the browser will fake bold.

For preloading, subsets and local files, the Astro fonts self-hosting guide goes deeper, and the official Fonts API reference lists every option. Each Komal Astro theme keeps its font tokens in one config file, so this swap stays a small diff.

FAQ

How do I use Google Fonts in an Astro theme?

Use fontProviders.google() in the fonts array of astro.config.mjs and render <Font cssVariable="..." /> in your layout. Astro downloads the files at build time and serves them from your own domain.

Can I use a local font file in an Astro theme?

Yes. Put the files in your project and register them with the local provider in the fonts array, or write a plain @font-face rule pointing at files in public/fonts/.

Why does my new font look bold or blurry?

The weight you’re using isn’t in the weights list, so the browser synthesizes it. Add the exact weights your headings and body text use and rebuild.