使用字体在 HTML 中使用本地字体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38086083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
To use local font in HTML using font face
提问by anavaras lamurep
I trying to use local font to apply styles in html, below is the code.Font is not getting applied for harlow class used element
我尝试使用本地字体在 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>
回答by anavaras lamurep
I made the following changes and I got the result
我进行了以下更改并得到了结果
- Quotation marks for font-family
- Using of URL instead of local
- Changing of "\" to "/"
- 字体系列的引号
- 使用 URL 而不是本地
- 将“\”更改为“/”
Note:Use of the local
css function throws an error in the developer console saying resource is not loaded. See the modified code below.
注意:使用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>
回答by MN. Vala
Use the correct path for file. your path does not work on the host. because your host has no drive 'c:/...' or anythings like this. so you can use
使用正确的文件路径。您的路径在主机上不起作用。因为您的主机没有驱动器 '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>
回答by wahsandaruwan
Use font face in all the format types according to the browser compatibility
根据浏览器兼容性使用所有格式类型的字体
- Just add bellow code before all the styling of your css file and then you can use this font family for any selector inside within your css file.
- 只需在 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 */
}