首页 > 解决方案 > 浏览器有时会使用错误的字体来渲染图标,导致文字乱码

问题描述

我遇到了一个间歇性问题,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 上的字体系列设置为'Gotham Light', Arial, sans-serif.

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

标签: cssiconsfont-face

解决方案



推荐阅读