使用字体在 HTML 中使用本地字体

To use local font in HTML using font face

我尝试使用本地字体在 html 中应用样式,下面是 code.Font 没有被应用到 harlow class 使用的元素

<!DOCTYPE html>
<html>
<head>
<style>
@font-face {
    font-family: myFirstFont;
    src:local("C:\Users\Website\fonts\Harlow_Solid_Italic.ttf");
}

.harlow{
    font-family: myFirstFont;
}
</style>
</head>
<body>
<div>With CSS3, websites can finally use fonts other than the pre selected "web-safe" fonts.</div>
<p><b class="harlow">Note:</b> Internet Explorer 8 and earlier, do not support the @font-face rule with the WOFF format (only support for EOT format).</p>
</body>
</html>

我做了以下修改,得到了结果

  • 字体系列的引号
  • 使用 URL 而不是本地
  • 将“\”更改为“/”

注: 使用 local css 函数会在开发人员控制台中引发错误,指出资源未加载。请参阅下面修改后的代码。

<!DOCTYPE html>
<html>
<head>
<style>
@font-face {
    font-family: "myFirstFont";
    src: url("C:/Users/Desktop/Website/fonts/Harlow_Solid_Italic.ttf");
}

.harlow {
    font-family: "myFirstFont";
}
</style>
</head>
<body>
<div>With CSS3, websites can finally use fonts other than the pre selected "web-safe" fonts.</div>
<p><b class="harlow">Note:</b> Internet Explorer 8 and earlier, do not support the @font-face rule with the WOFF format (only support for EOT format).</p>
</body>
</html>

使用正确的文件路径。 您的路径在主机上不起作用。因为您的主机没有驱动器 'c:/...' 或类似的东西。 所以你可以使用

<!DOCTYPE html>
<html>
<head>
<style>
@font-face {
    font-family: myFirstFont;
    src:url("/fonts/Harlow_Solid_Italic.ttf");
}

.harlow{
    font-family: myFirstFont;
}
</style>
</head>
<body>
<div>With CSS3, websites can finally use fonts other than the pre selected "web-safe" fonts.</div>
<p><b class="harlow">Note:</b> Internet Explorer 8 and earlier, do not support the @font-face rule with the WOFF format (only support for EOT format).</p>
</body>
</html>

根据浏览器兼容性在所有格式类型中使用字体

- 只需在 css 文件的所有样式之前添加以下代码,然后 您可以将此字体系列用于 css 中的任何选择器 文件.

@font-face {
         font-family: 'CustomHeading';
         src: url('./fonts/SFAtarianSystem.ttf') format('embedded-opentype'), /* Internet Explorer */
         url('./fonts/SFAtarianSystem.ttf') format('woff2'), /* Super Modern Browsers */
         url('./fonts/SFAtarianSystem.ttf') format('woff'), /* Pretty Modern Browsers */
         url('./fonts/SFAtarianSystem.ttf')  format('truetype'), /* Safari, Android, iOS */
         url('./fonts/SFAtarianSystem.ttf') format('svg'); /* Legacy iOS */
}