Using custom fonts
Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.
This guide will show you how to add web fonts to your project and use them in your components.
Astro provides a way to use fonts from your filesystem and various font providers (e.g. Fontsource, Google) through a unified, fully customizable, and type-safe API.
Web fonts can impact page performance at both load time and rendering time. This API helps you keep your site performant with automatic web font optimizations including preload links, optimized fallbacks, and opinionated defaults. See common usage examples.
The Fonts API focuses on performance and privacy by downloading and caching fonts so they’re served from your site. This can avoid sending user data to third-party sites, and also ensures that a consistent set of fonts is available to all your visitors.
Configuring custom fonts
Section titled “Configuring custom fonts”Registering custom fonts for your Astro project is done through the fonts option in your Astro config.
For each font you want to use, you must specify its name, a CSS variable, and an Astro font provider.
Astro provides built-in support for the most popular font providers: Adobe, Bunny, Fontshare, Fontsource, Google, Google Icons and NPM, as well as for using your own local font files. Additionally, you can further customize your font configuration to optimize performance and visitor experience.
Using a local font file
Section titled “Using a local font file”This example will demonstrate adding a custom font using the font file DistantGalaxy.woff2.
-
Add your font file inside the
src/directory, for examplesrc/assets/fonts/. -
Create a new font family in your Astro config file using the local font provider and specify the variants to be included:
astro.config.mjs import { defineConfig, fontProviders } from "astro/config";export default defineConfig({fonts: [{provider: fontProviders.local(),name: "DistantGalaxy",cssVariable: "--font-distant-galaxy",options: {variants: [{src: ['./src/assets/fonts/DistantGalaxy.woff2'],weight: 'normal',style: 'normal'}]}}]}); -
Your font is now configured and ready to be added to your page head so that it can be used in your project.
Using Fontsource
Section titled “Using Fontsource”Astro supports several font providers out of the box, including support for Fontsource that simplifies using Google Fonts and other open-source fonts.
The following example will use Fontsource to add custom font support, but the process is similar for any of Astro’s built-in font providers (e.g. Adobe, Bunny).
-
Find the font you want to use in Fontsource’s catalog. This example will use Roboto.
-
Create a new font family in your Astro config file using the Fontsource provider:
astro.config.mjs import { defineConfig, fontProviders } from "astro/config";export default defineConfig({fonts: [{provider: fontProviders.fontsource(),name: "Roboto",cssVariable: "--font-roboto",}]}); -
Your font is now configured and ready to be added to your page head so that it can be used in your project.
Applying custom fonts
Section titled “Applying custom fonts”After a font is configured, it must be added to your page head with an identifying CSS variable. Then, you can use this variable when defining your page styles.
-
Import and include the
<Font />component with the requiredcssVariableproperty in the head of your page, usually in a dedicatedHead.astrocomponent or in a layout component directly:src/layouts/Layout.astro ---import { Font } from "astro:assets";---<html><head><Font cssVariable="--font-distant-galaxy" /></head><body><slot /></body></html>