html - 浏览器不会获取字体文件
问题描述
我有一个简单的 HTML 演示页面来试用我的自定义字体文件(一组 SVG 图标)
HTML 相当简单,我指定字体文件的路径(与 HTML 文件相同的目录)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>nextgenicons</title>
<style>
body {
font-family: sans-serif;
margin: 0;
padding: 10px 20px;
}
.preview {
line-height: 2em;
}
.previewicon {
display: inline-block;
width: 32px;
text-align: center;
}
.icon {
display: inline-block;
font-size: 16px;
}
@font-face {
font-family: "nextgenicons";
src: url("nextgenicons.eot?95d1e9193d97fb68fae3bc44b221fa23?#iefix") format("embedded-opentype"),
url("nextgenicons.woff2?95d1e9193d97fb68fae3bc44b221fa23") format("woff2"),
url("nextgenicons.woff?95d1e9193d97fb68fae3bc44b221fa23") format("woff"),
url("nextgenicons.svg?95d1e9193d97fb68fae3bc44b221fa23#nextgenicons") format("svg");
}
.icon {
line-height: 1;
}
.icon:before {
font-family: nextgenicons !important;
font-style: normal;
font-weight: normal !important;
vertical-align: top;
}
.icon-logo:before {
content: "\f101";
}
.icon-ok:before {
content: "\f102";
}
.icon-settings:before {
content: "\f103";
}
.icon-menu:before {
content: "\f104";
}
</style>
</head>
<body>
<h1>nextgenicons</h1>
<div class="preview">
<span class="previewicon">
<span class=" icon-logo"></span>
</span>
<span>logo</span>
</div>
<div class="preview">
<span class="previewicon">
<span class=" icon-ok"></span>
</span>
<span>ok</span>
</div>
<div class="preview">
<span class="previewicon">
<span class=" icon-settings"></span>
</span>
<span>settings</span>
</div>
<div class="preview">
<span class="previewicon">
<span class=" icon-menu"></span>
</span>
<span>menu</span>
</div>
</body>
</html>
浏览器只显示方块来代替实际的图标:
这是根目录,它包含 HTML 页面和字体文件(请注意 .html 是带有 IE 图标的那个,另一个带有 Chrome 图标的是 .svg 文件):
更有趣的是浏览器根本不获取字体文件。这是页面加载时网络活动的快照:
所以我猜想显示黑色方块是因为浏览器没有获取字体文件,但为什么会这样呢?
解决方案
您正在定义图标类,但是您没有在 dom 中关联它<span class=" icon-logo"></span>
应该是<span class="icon icon-logo"></span>
推荐阅读
- python - asyncio.sleep 如何不阻塞线程?
- flutter - 使用 Flutter 从 Firebase Firestore 查询中获取单个文档
- ssh - stdin:不是 tty 连接关闭 SSH
- php - 将数据重新排列到php中的新数组中
- node.js - NodeJS + MQTT - 以 1 毫秒的频率发布会导致 EventEmitter 内存泄漏
- android - 我无法提供带有匕首的视图模型
- python-3.x - 尝试使用 pytest 和 capsys 访问标准输出时出现 ValueError
- visual-studio-code - Visual Studio Code 中的 window.zoomLevel 自动更改为负值
- selenium - Safari 浏览器与 Selenium 有问题
- java - 使用 google chrom 检查时 Stetho SQL 数据库不起作用