0

这里的过程有点困惑,但我尝试将新字体嵌入到 html 网站的 CSS 文件中,但无论我尝试什么,该字体都不会出现在 html 网站上。我已经通过转换器将字体放入我的 CSS 文件夹中,该文件夹与我的 index.html 位于同一文件中,但无济于事。这是我放入 CSS 的代码:

    font-family: 'Montserrat';
    src: url('Montserrat-SemiBold.woff2') format('woff2'),
        url('Montserrat-SemiBold.woff') format('woff'),
        url('Montserrat-SemiBold.ttf') format('truetype');
    font-weight: 600;
    font-style: normal;
    font-display: swap;

有什么我可以尝试让代码正确显示的吗?

4

2 回答 2

0

您应该添加 @font-face 关键字来导入字体 请参阅此示例

于 2021-11-26T22:25:37.613 回答
0

从这里开始: https ://www.w3schools.com/cssref/css3_pr_font-face_rule.asp

你好世界示例

在此处输入图像描述

您应该添加一个CSS 选择器(选择元素)body,例如:

<!DOCTYPE HTML>

<html>

<head>
    <style>
        @font-face {
            font-family: 'hello-font';
            font-weight: 600;
            /* Use chrome inspect and check if the path is correct */
            src: url(hello-font.woff2) format('woff2');
        }
        /* missing in your styles */
        body {
            font-family: 'hello-font', sans-serif;
        }
    </style>
    <title>Your Website</title>
</head>

<body>
    <h1>hello world</h1>
</body>

</html>

“文件未找到”

无论如何,如果您使用本地文件 -没有实时 URL就无法知道路径/文件名是否正确(Chrome 检查red错误) - 错误示例:

在此处输入图像描述

选项 2(更好) - 通过 CDN 加载谷歌字体

** 更好的速度性能/更易于维护。

<html>
  <head>
    <link rel="stylesheet"
          href="https://fonts.googleapis.com/css?family=Tangerine">
    <style>
      body {
        font-family: 'Tangerine', serif;
        font-size: 48px;
      }
    </style>
  </head>
  <body>
    <div>Making the Web Beautiful!</div>
  </body>
</html>

于 2021-11-27T01:15:53.053 回答