要在 HTML 中嵌入 Google Fonts 或自定义字体,可以使用 CSS 的 @font-face 规则。 首先,在 head 标签中通过 link 元素加载 Google Fonts CDN 或者是自定义字体文件,例如:
<link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
或者
<link href="myfonts/fonts.css" rel="stylesheet">
在 CSS 中,使用 @font-face 规则声明嵌入的字体,
@font-face {
font-family: 'Open Sans';
font-style: normal;
font-weight: 400;
src: url(https://fonts.gstatic.com/s/opensans/v22/mem8YaGs126MiZpBA-U1UpcaXcl0e7fK.ttf);
}
其中, font-family 是该字体的名称, src 是字体的 URL。
然后,在需要使用该字体的元素中指定该字体的名称即可,例如:
body {
font-family: 'Open Sans', sans-serif;
}
这样,页面中的文本就会使用 Google Fonts 中的 Open Sans 字体了。