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

ana*_*rep 7 html css font-face

我试图使用本地字体在html中应用样式,下面是代码。字体未应用于harlow类的使用元素

<!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>
Run Code Online (Sandbox Code Playgroud)

wah*_*wan 14

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

只需在 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 */
}
Run Code Online (Sandbox Code Playgroud)


小智 9

使用正确的文件路径。您的路径在主机上不起作用。因为您的主机没有驱动器 '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>
Run Code Online (Sandbox Code Playgroud)


ana*_*rep 8

我进行了以下更改,并得到了结果

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

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

<!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>
Run Code Online (Sandbox Code Playgroud)

  • 您应该将 `src:url("C:/path/to/ttf");` 更改为 `src:url("file:///path/to/file")`。 (2认同)