Quick‑start guide to Web Font APIs
Quick‑start guide to web font APIs
What is a font API?
| Concept | Description |
|---|---|
| Web‑Font service | Hosted library (e.g., Google Fonts, Adobe Fonts) that provides CSS <link> or JavaScript snippets to fetch font files from a CDN. |
| CSS @font‑face | Low‑level browser API that tells the user agent where to download a font file and what family name to expose to CSS. |
| Variable‑font API | Newer syntax (font-variation-settings, axis ranges) that lets a single file expose many styles (weight, width, slant, …). |
| Font‑display descriptor | Controls how text is rendered while the font is loading (auto, block, swap, fallback, optional). |
| JavaScript loading helpers | Optional helpers (FontFace constructor, document.fonts.load()) for on‑the‑fly loading or preloading fonts. |
All of these belong to the “Font API” family because they expose programmatic ways to request and use fonts.
Core CSS terminology
| Property / keyword | Meaning |
|---|---|
font-family | The name you use in CSS after the font is loaded. |
src | URL(s) of the actual font file(s) (.woff2, .woff, .ttf, …). |
unicode-range | Subset of characters covered by a given file – great for subsetting. |
font-weight / font-style / font-stretch | Classic static descriptors. |
font-variation-settings | Key/value pairs for variable axes ('wght' 400). |
font-display | Rendering fallback strategy while the font loads. |
Using the built‑in CSS @font‑face (self‑hosted)
1<!-- 1️⃣ Put the font files on your server (or CDN) -->
2<link rel="preload" href="/fonts/Inter-Variable.woff2"
3 as="font" type="font/woff2" crossorigin>
4
5<style>
6 /* 2️⃣ Declare the family */
7 @font-face {
8 font-family: 'Inter Variable';
9 src: url('/fonts/Inter-Variable.woff2') format('woff2');
10 /* Optional – load only Latin characters */
11 unicode-range: U+0000-00FF;
12 /* Show fallback text until the font is ready */
13 font-display: swap;
14 }
15
16 /* 3️⃣ Use it */
17 body {
18 font-family: 'Inter Variable', system-ui, sans-serif;
19 }
20</style>Benefits: full control over caching, licensing, and subsetting; works offline if you bundle the files.
Google fonts CSS API (the most common public font API)
1<link href="https://fonts.googleapis.com/css2?
2family=Roboto:wght@400;700&
3family=Open+Sans:ital,wght@0,400;0,600;1,400&
4display=swap"
5rel="stylesheet">Explanation
| Part | What it does |
|---|---|
family=Roboto:wght@400;700 | Request only normal & bold weights of Roboto. |
family=Open+Sans:ital,wght@0,400;0,600;1,400 | Load regular/semibold Roman + italic regular of Open Sans. |
display=swap | Use fallback text until fonts finish loading (recommended for UX). |
Then in CSS:
1h1 { font-family: 'Roboto', sans-serif; }
2p { font-family: 'Open Sans', sans-serif; }Tips
- Combine multiple families in a single request – reduces HTTP round‑trips.
- Use axis ranges for variable fonts:
family=Inter:wght@200..800.
Variable‑font example (Google fonts)
1<link href="https://fonts.googleapis.com/css2?
2family=Inter:wght@200..800&display=swap"
3rel="stylesheet">
4
5<style>
6 .hero {
7 font-family: 'Inter', system-ui;
8 /* Animate weight from thin → bold */
9 font-variation-settings: 'wght' 200;
10 transition: font-variation-settings .6s ease;
11 }
12 .hero:hover { font-variation-settings: 'wght' 800; }
13</style>Why it matters: One file replaces dozens of static .woff2s → dramatically smaller payload.
JavaScript font loading API (FontFace)
When to use – you need to load a font after page load (e.g., in SPA routing) or want to await its readiness before showing UI.
1// Create a FontFace object
2const myFont = new FontFace(
3 'Custom Serif',
4 'url(/fonts/CustomSerif-VF.woff2) format("woff2")',
5 { weight: '400 900', style: 'normal' } // optional descriptors
6);
7
8// Add it to the document’s FontFaceSet
9document.fonts.add(myFont);
10
11// Start loading
12myFont.load().then(() => {
13 // Font ready → apply it
14 document.body.style.fontFamily = '"Custom Serif", serif';
15}).catch(err => console.error('Font failed', err));You can also query what fonts are loaded:
1document.fonts.ready.then(() => console.log('All fonts ready'));Adobe Fonts (Typekit) – another public font API
1<link rel="stylesheet" href="https://use.typekit.net/xyz123.css">Then reference families exactly as listed in the kit:
1h2 { font-family: "adobe-caslon-pro", serif; }Key differences vs. Google fonts
- Requires an Adobe account & embedding kit ID (
xyz123). - Provides sync and async loading options (
<script>… Typekit.load(); </script>).
Performance best practices
Subset only needed glyphs – use
unicode-rangeor Google Fonts’text=parameter.Leverage
preloadfor critical fonts:
1<link rel="preload" href="/fonts/Inter-Variable.woff2"
2 as="font" type="font/woff2" crossorigin>Set
font-displayto at leastswap.
Avoidblockingtext rendering, which hurts CLS (Cumulative Layout Shift).Combine families into one request when using Google Fonts or Adobe Fonts.
Cache forever – most CDNs send far‑future
Cache‑Controlheaders for woff2 files.
Quick reference cheat‑sheet
| API | How to request | Typical use case |
|---|---|---|
| CSS @font-face | <style>@font-face{…}</style> + optional <link rel="preload"> | Self‑hosted fonts, full control |
| Google Fonts CSS API | <link href="https://fonts.googleapis.com/css2?..."> | Free fonts with simple URL parameters |
| Adobe Fonts (Typekit) | <link href="https://use.typekit.net/xxxx.css"> | Paid library with design‑system integration |
| FontFace JS API | new FontFace(name, src[, descriptors]) | Dynamic loading in SPAs / runtime bundles |
| Variable‑font axis range | family=Inter:wght@200..800 or CSS font-variation-settings | Reduce file count & enable smooth weight transitions |
TL;DR code sample – one‑liner for most projects
1<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;700&display=swap"
2 rel="stylesheet">
3<style>
4 body {font-family:'Montserrat',Arial,sans-serif;}
5</style>✅ Loads only regular & bold weights → tiny download.
✅display=swapguarantees fast fallback text → good UX.
✅ Works on all modern browsers without extra JavaScript.
What about Aspose?
If you need to manipulate or convert font files on the server side (e.g., generate subsets, convert between TTF/WOFF2, or extract glyph metrics), the Aspose.Font library provides a programmatic API for .NET, Java, and Python. It can be integrated into build pipelines or back‑end services to prepare font assets before they are served via any of the Font APIs described above.
Ready to embed?
Copy any of the snippets above into your HTML/CSS/JS and you’ll have a production‑ready Font API integration in seconds. Happy styling!