Sub topic 6
Adding Google Fonts to a Page
This chapter shows how to load a Google Fonts family into a page. It covers the link tag method, the CSS @import method, the URL format, and the mistakes that most often cause fonts to fail. It assumes you can write basic HTML, including the link element, and that you know the CSS font-family property. It also assumes you can open your browser's developer tools and read the Network tab.
How the embed works
Google Fonts serves fonts in two steps. Your page asks for a CSS file from fonts.googleapis.com. That CSS file contains @font-face rules. Each rule points to a font file hosted on fonts.gstatic.com. The browser downloads a font file only when text on the page uses it.
This two-host design explains why the embed code includes two preconnect hints. Each host needs its own connection, and the hints let the browser open both early.
The link tag method
The link tag method is the one Google Fonts shows by default. Put these three tags inside the head element of your page:
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap" rel="stylesheet">Each tag has a job:
- The first
preconnectopens a connection to the host that serves the CSS. - The second
preconnectopens a connection to the host that serves the font files. It needs thecrossoriginattribute because browsers fetch fonts in anonymous CORS mode. Without the attribute, the early connection is not reused for the font request, and the hint is wasted. - The
stylesheetlink requests the CSS. This request blocks rendering, so the browser waits for it before painting the page.
Load this link before your own stylesheet if your CSS refers to the font. Either order works, because the font family name is resolved when text is rendered. Putting the font link first keeps the request early.
Reading the css2 URL
The css2 URL packs your choices into its query string. Here is the one from the example above, split into parts:
| Part | Meaning |
|---|---|
| `family=Inter` | The family to load. |
| `:wght@400;700` | An axis list. `wght` is weight. The values 400 and 700 are the weights requested. |
| `&display=swap` | The `font-display` behavior that Google writes into every `@font-face` rule. |
Three rules for this format cause most of the errors people see:
- Spaces in family names become
+. For example,family=Open+Sans. - To load several families, repeat the
familyparameter:family=Inter:wght@400;700&family=Lora:ital,wght@0,400;1,400. - Axis values must be sorted in ascending order. If you list
wght@700;400, the API returns an error instead of CSS.
If the request fails, open the URL directly in a browser tab. The API returns a plain text error message that names the problem.
Choosing weights and styles
Every weight and style you request adds a font file, and each file adds download time. Request only what the design uses. A page that uses regular body text and bold headings needs 400 and 700. If you request 100 through 900 as separate static weights, you download many files that no rule uses.
For italics, add the ital axis. When ital is present, each value is a pair of ital,wght, where 0 means upright and 1 means italic. The pairs must also be sorted:
<link href="https://fonts.googleapis.com/css2?family=Lora:ital,wght@0,400;0,700;1,400&display=swap" rel="stylesheet">This requests upright 400 and 700 plus italic 400. If your CSS asks for bold italic, the browser creates a synthetic bold from the italic 400 file. Synthetic styles are usually uneven, so request the real file when the design needs it.
Variable fonts
A variable font is one file that holds a continuous range of a property, such as weight. Instead of listing each weight, you give a range with two dots:
<link href="https://fonts.googleapis.com/css2?family=Inter:[email protected]&display=swap" rel="stylesheet">With this link, CSS can use any weight from 100 to 900, including values such as 450, from one file. Use a range when the design uses three or more weights, or when you want fine control. Use a short list of static weights when you need only two. A narrower range such as [email protected] also works and gives the browser less data to download.
Not every family is variable. If you request a range for a static-only family, the API returns an error. Check the family's page on Google Fonts, which lists the available axes.
The CSS @import method
You can also load fonts from inside a stylesheet:
@import url("https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap");
body {
font-family: "Inter", system-ui, sans-serif;
}The @import rule must come first in the file, before any other rules. This method is simple, but it is slower. The browser must download and parse your stylesheet before it discovers the font stylesheet, so the two requests happen one after the other instead of in parallel. You also cannot add preconnect hints through CSS. Prefer the link tag. Use @import only when you cannot edit the HTML, for example in a theme system that lets you change CSS only.
Using the font in CSS
Loading a font makes it available. It does not apply it. You must name the family in a font-family rule, and the name must match what the service calls it. Always add fallbacks:
body {
font-family: "Inter", system-ui, -apple-system, "Segoe UI", Arial, sans-serif;
}
h1, h2 {
font-weight: 700;
}The stack lists the web font first. If the file is slow or blocked, the browser draws the text with the next available font. The last item, sans-serif, is a generic family that always resolves to something.
With a variable font loaded, you can use any weight in the range:
.caption {
font-weight: 450;
}Checking that it works
Do not trust the way the page looks. A system font can look similar to the web font. Check in developer tools:
- Open the Network tab, filter by Font, and reload. You should see one or more files from
fonts.gstatic.com. - Open the Elements tab, select a text element, and open the Computed panel. At the bottom, the Rendered Fonts section lists the font actually used and says whether it came from the network or is a local font.
- Confirm that the CSS request returned status 200. A 400 status means the URL is malformed.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Text shows a system font | The `font-family` name has a typo, or the family is not requested in the URL | Match the name exactly and check the URL |
| The CSS request returns 400 | Axis values are unsorted, a space was left in the family name, or a range was used on a static font | Sort values, use `+`, check the family's axes |
| Bold text looks smeared | Weight 700 was not requested, so the browser synthesized it | Add 700 to the `wght` list |
| Font works on your computer but not for users | The font name matches a font installed locally on your machine | Test with the cache cleared and the Network tab open |
| Console warns about a preload that was not used | The `crossorigin` attribute is missing on the `gstatic` preconnect | Add `crossorigin` |
Summary of the method
- Add the two
preconnectlinks and the stylesheet link to thehead. - Request only the weights and styles you use, or one variable range.
- Keep axis values sorted and spaces encoded as
+. - Name the family in
font-familywith fallbacks. - Verify the result in the Network tab and the Rendered Fonts panel.
Exercise
You want to load Lora in upright 400, upright 700, and italic 400, and also Inter as a variable font from weight 300 to 800. Write the single stylesheet link URL, then name one thing that would make the API return an error.
Show answer
https://fonts.googleapis.com/css2?family=Lora:ital,wght@0,400;0,700;1,400&family=Inter:[email protected]&display=swap. The family parameter is repeated for each family, and the Lora pairs are sorted with the upright (0) values before the italic (1) value. The request would fail if the pairs were unsorted (for example 1,400 before 0,700), if wght values were listed in descending order, or if a space in a family name such as Open Sans were not written as +.
Exercise
A page loads Inter through a link tag, but the Network tab filtered by Font shows no requests and the text looks like Arial. Name two checks you would make.
Show answer
First, check the CSS request in the Network tab. A 400 status means the URL is malformed, for example with unsorted axis values. Second, check the font-family rule in the Computed panel and the CSS source. The family name must match exactly, and the rule must apply to the element. Also confirm that the link tag is inside the head and that no content blocker is stopping the request.