如何将 Google 字体导入 HTML/CSS?

问题描述

我的 HTML 文件中无法使用 Roboto 字体。如果我理解正确,Google 的说明是:

“将这些行复制到您的 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=Roboto:wght@100&display=swap" rel="stylesheet">

这是我的 HTML:

<!DOCTYPE html>
<html>
<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=Roboto:wght@100&display=swap" rel="stylesheet">
</head>
<body>
    <h3>This is a heading</h3>
    <p>This is a string.</p>
</body>
</html>

网站显示标准字体,而不是 Roboto 字体:

enter image description here

解决方法

</head>之前添加

<style>
body {
  font-family: "Roboto",sans-serif;
}
</style>
,

你没有在css中使用字体系列,像这样使用

{{1}}