【问题标题】:Browser sometimes uses the wrong font to render icons, resulting in garbled text浏览器有时会使用错误的字体来渲染图标,导致文字乱码
【发布时间】:2019-11-10 00:13:51
【问题描述】:

Chrome 出现间歇性问题,无法呈现正确的字体来显示图标。

我正在使用 icomoon.io 创建一组自定义图标作为字体,并使用以下 css(由 icomoon 自动生成)来显示这些自定义图标。

@font-face {
    font-family: 'eIconFont';
    src: url('fonts/eIconFontV3.eot?kmqo7q');
    src: url('fonts/eIconFontV3.eot?kmqo7q#iefix') format('embedded-opentype'), url('fonts/eIconFontV3.woff2?kmqo7q') format('woff2'), url('fonts/eIconFontV3.ttf?kmqo7q') format('truetype'), url('fonts/eIconFontV3.woff?kmqo7q') format('woff'), url('fonts/eIconFontV3.svg?kmqo7q#eIconFontV3') format('svg');
    font-weight: normal;
    font-style: normal;
}
[class^="eIcon-"], [class*=" eIcon-"] {
    /* use !important to prevent issues with browser extensions that change fonts */
    font-family: 'eIconFont' !important;
    speak: none;
    font-style: normal;
    font-weight: normal;
    font-variant: normal;
    text-transform: none;
    line-height: 1;
    /* Enable Ligatures ================ */
    letter-spacing: 0;
    -webkit-font-feature-settings: "liga";
    -moz-font-feature-settings: "liga=1";
    -moz-font-feature-settings: "liga";
    -ms-font-feature-settings: "liga" 1;
    font-feature-settings: "liga";
    -webkit-font-variant-ligatures: discretionary-ligatures;
    font-variant-ligatures: discretionary-ligatures;
    /* Better Font Rendering =========== */
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}
.eIcon-support:before {
    content: "\e93b";
}
.eIcon-collapse:before {
    content: "\e935";
}
.eIcon-expand:before {
    content: "\e936";
}

html 看起来像这样:

<span class="eIcon-support" title="Support">
    ::before
</span>

结果如下所示:

查看 Chrome 开发工具,我可以看到字体文件本身可以从我们的 CDN 正常加载,但我注意到在 Elements > Computed > Rendered Fonts 下显示 Times New Roman,当该值通常为eIconFontV3。奇怪的是,浏览器尝试使用 Times New Roman,而 body 上设置的 font-family 是'Gotham Light', Arial, sans-serif

这个错误很少发生,而且很难重现。

【问题讨论】:

  • @font-face 是否应该有两个 src 值?似乎一个会覆盖另一个。
  • @showdev 我不确定为什么会有两个src,这正是 icomoon.io 生成的。可能会更加谨慎的后备?
  • 可能是这样。我找到了this,但我不确定它是否相关。你有没有看到类似的错误?
  • @showdev 不,很遗憾控制台中没有显示错误。

标签: css icons font-face


【解决方案1】:

我也遇到了这个问题。如果您使用sass 并导致问题,也许下面的详细信息会对您有所帮助。

我使用 sass 将 SCSS 文件编译为 CSS 文件,我发现 sass 将转义字符串(例如 \E91E,十六进制 5C45393145 )转换为真正的 utf-8 值(,十六进制 EEA49E) ,但node-sass 不这样做。在使用 Chrome 时,转换有时会导致文本出现乱码。 Here 有更多关于该问题的详细信息。

我的临时解决方案是使用 node-sass 代替 sass 来编译 SCSS 文件。

【讨论】:

  • 虽然我理解使用 sass 和 node-sass 生成的 CSS 的区别,但我不明白为什么 Chrome(或任何 webkit 浏览器)有时使用 Times New Roman 而不是指定的图标字体。这让我很头疼。
猜你喜欢
  • 2019-05-07
  • 2021-10-18
  • 2021-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多