Sub topic 6
Adding Google Fonts to a Page
This chapter assumes you know basic HTML structure, the link element, CSS selectors, and the font-family property. You should also be able to open your browser's developer tools. By the end, you will be able to embed a Google Fonts family, pick weights, and check that the font loaded.
How the embed works
Google Fonts uses two hosts. The first, fonts.googleapis.com, serves a small CSS file that contains the @font-face rules for the font you requested. The second, fonts.gstatic.com, serves the actual font files that those rules point to.
The browser therefore makes two kinds of requests: one for the CSS, then one or more for font files. The CSS is render-blocking, so it should go in the head of your page.
The link tag method
Google Fonts generates this snippet for you. Paste it into the head.
<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 line has a job:
- The first
preconnectopens the connection to the CSS host early. - The second
preconnectdoes the same for the font file host. It needscrossoriginbecause font requests are made in anonymous CORS mode. Without the attribute, the browser opens a connection that it cannot reuse for the font files. - The
linkwithrel="stylesheet"requests the CSS. In the URL,family=Inter:wght@400;700asks for Inter at weights 400 and 700.display=swapmakes the generated rules usefont-display: swap.
Using the font in CSS
Loading a font does nothing until your CSS refers to it. Use the family name in a stack with fallbacks.
body {
font-family: "Inter", system-ui, -apple-system, "Segoe UI", Arial, sans-serif;
}If the web font fails to load, or is still loading, the browser uses the next family in the list.
The CSS @import alternative
You can also load the same stylesheet from inside your own CSS.
@import url("https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap");This works, but it is slower. The browser must download your CSS file before it discovers the @import, and only then starts the request for the Google CSS. A link in the HTML lets the browser start that request sooner. Prefer link unless you cannot edit the HTML.
Choosing weights and styles
Each weight is a separate font file, so request only what the page uses. Adding weights you never use costs bandwidth for no benefit.
The URL syntax follows a pattern:
| Request | URL fragment |
|---|---|
| One weight | `family=Inter:wght@400` |
| Two weights | `family=Inter:wght@400;700` |
| Weight range (variable) | `family=Inter:[email protected]` |
| Regular and italic | `family=Roboto:ital,wght@0,400;1,400` |
| Two families | `family=Inter:wght@400;700&family=Lora:wght@400` |
Each family gets its own family= parameter. Weights in the list must be in ascending order, or the API returns an error.
A weight range requests the variable version of the font, which covers every weight from 100 to 900 in one file.
<link href="https://fonts.googleapis.com/css2?family=Inter:[email protected]&display=swap" rel="stylesheet">With this link you can write font-weight: 550 in CSS and get that exact weight. A variable file is larger than a single static weight, but it can be smaller than three or four static files combined.
Checking that it worked
Open developer tools and follow these steps.
- In the Network panel, filter by
Font. You should see WOFF2 files fromfonts.gstatic.com. - In the Elements panel, select an element and open the Computed tab, then scroll to the rendered fonts section (in Firefox, the Fonts tab). It shows which font was actually used.
- If the rendered font is a system font, check for a misspelled family name or a failed request.
Common errors
- Family name mismatch. CSS must use the name that appears in the Google CSS. Multi-word names need quotes, such as
"Open Sans". - Missing weight. If you use
font-weight: 600but loaded only 400 and 700, the browser picks the nearest loaded weight, or synthesizes bold. The result may not look as intended. - Missing
crossorigin. The secondpreconnectsilently loses its benefit. - Unsorted weights.
wght@700;400fails. Writewght@400;700.
Exercise
You load Inter with family=Inter:wght@400;700, and a heading in your CSS uses font-weight: 600. What does the browser show, and how would you fix it so the heading renders at exactly 600?
Show answer
The browser does not have a 600 file, so it falls back to a nearby loaded weight (typically 700 for a heading weight above 500), not true 600. To get exactly 600, either add 600 to the list (wght@400;600;700) or request the variable range ([email protected]).
Exercise
Why does the second preconnect link include the crossorigin attribute?
Show answer
Font files are fetched in anonymous CORS mode. A connection opened without crossorigin cannot be reused for those requests, so the preconnect would have no effect.