How to Use Google Fonts in HTML and CSS
To use Google Fonts, load the font stylesheet in the HTML <head> and set the same family in CSS with font-family. The example requests weights 400 and 700 of Noto Sans and displays the same text in both weights.
Choose a font and the weights you need
Pick a font from Google Fonts and preview the characters your page actually uses, including accents and digits. Open the web embed code for the selected font and copy its <link> and CSS family name. Choose the weights you expect to use.
Apply weights 400 and 700 in HTML and CSS
Save the following code as google-font.html and open it in an internet-connected browser. The stylesheet <link> loads the font definitions. The font-family declaration applies the font to the text. Change the weight in .bold to 400 to make both lines use the same weight, then restore 700 to compare.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Google Fonts exercise</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="stylesheet"
href="https://fonts.googleapis.com/css2?family=Noto+Sans:wght@400;700&display=swap">
<style>
body {
margin: 0;
color: #243449;
background: #eef2f6;
font-family: "Noto Sans", sans-serif;
line-height: 1.8;
}
main { max-width: 840px; margin: 32px auto; padding: 0 20px; }
h1 { font-size: 28px; font-weight: 700; }
.sample {
padding: 20px;
border: 1px solid #b8c6d4;
background: white;
font-size: 26px;
overflow-wrap: anywhere;
}
.regular { font-weight: 400; }
.bold { font-weight: 700; }
.note { font-size: 16px; }
</style>
</head>
<body>
<main>
<h1>Compare Google Fonts weights</h1>
<p class="sample regular">Hello, café 123 — regular weight 400</p>
<p class="sample bold">Hello, café 123 — bold weight 700</p>
<p class="note">The sans-serif fallback is used before the font loads or if it fails.</p>
</main>
</body>
</html>
Understand the request and font fallback
family identifies the font, and wght@400;700 requests two weights. The CSS font-weight selects a face for the element. If a weight is unavailable, the browser can choose a nearby weight or synthesize it. Google Fonts · CSS API
display=swap lets the browser show fallback text while the web font loads, then swap it in. Line breaks can move if the fonts have different metrics. sans-serif provides a fallback when the font fails to load or a character is missing. preconnect prepares connections as a hint. CSS Fonts · font-display
If you need to load the family from a separate CSS file, put the following @import before its ordinary style rules. Remove the equivalent Google Fonts stylesheet link from the HTML and keep the font-family declaration.
@import url("https://fonts.googleapis.com/css2?family=Noto+Sans:wght@400;700&display=swap");
Check why a font has not changed
In developer tools, check the fonts.googleapis.com stylesheet request, then the font file requests to fonts.gstatic.com. Inspect the selected element’s weight and the font actually used to render it. If only some characters look different, check glyph coverage. If only headings fail to change, look for an overriding theme rule.
Request only what the page uses
When you use only 400 and 700, request those weights. For a variable font, you can request a range such as wght@100..900 after checking which weights it supports. Use text= for short, fixed strings. It can omit characters needed by translations or user input. Google Fonts · CSS API
If the site cannot connect to a remote font service, check the font license and distribution files before self-hosting. For a site-wide change, load the stylesheet once through the shared resource pipeline and set the family in the theme’s common CSS.




