首页 > 解决方案 > 在 CSS 文件中加载本地 woff2 文件不起作用

问题描述

我正在尝试在 CSS 文件中加载 woff2 字体文件。但是我不断收到错误消息,说找不到该文件。但路径是正确的。
我的 CSS 文件:

@font-face {
  font-family: 'Material Icons';
  font-style: normal;
  font-weight: 400;
  src: url("C:\Users\win10\Desktop\Electron_Projects\EnderFramework\_themes\_default\fonts\material_icons.woff2") format('woff2');
}

icon, icons{
  font-family: 'Material Icons';
  font-weight: normal;
  font-style: normal;
  display: inline-block;
  line-height: 1;
  text-transform: none;
  letter-spacing: normal;
  word-wrap: normal;
  white-space: nowrap;
  direction: ltr;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  -moz-osx-font-smoothing: grayscale;
  font-feature-settings: 'liga';
}

这是我不断收到的错误:
加载资源失败:net::ERR_FILE_NOT_FOUND file:///C:/Userswin10%C3%9Esktop%0Electron_Projects%0EnderFramework_themes_default%0Fontsmaterial_icons.woff2:1

标签: htmlcsswebfonts

解决方案


尝试指定一个不完整的文件路径,相对。

@font-face {
  font-family: 'Material Icons';
  font-style: normal;
  font-weight: 400;
  src: url("..\fonts\material_icons.woff2") format('woff2');
}

此外,由于使用反斜杠,可能会导致错误。

@font-face {
  font-family: 'Material Icons';
  font-style: normal;
  font-weight: 400;
  src: url("../fonts/material_icons.woff2") format('woff2');
}

推荐阅读