浏览器有时会使用错误的字体来渲染图标,导致出现乱码

Bob*_*ano 8 css icons font-face

我遇到了 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";
}
Run Code Online (Sandbox Code Playgroud)

html 看起来像这样:

<span class="eIcon-support" title="Support">
    ::before
</span>
Run Code Online (Sandbox Code Playgroud)

结果看起来像这样:

在此输入图像描述

查看 Chrome 开发工具,我可以看到字体文件本身从我们的 CDN 加载得很好,但我注意到在元素 > 计算 > 渲染字体下它显示Times New Roman,而值通常为eIconFontV3。奇怪的是,当正文上设置的字体系列为 时,浏览器尝试使用 Times New Roman 'Gotham Light', Arial, sans-serif

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

小智 3

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

\n

我使用sass将SCSS文件编译为CSS文件,我发现sass将转义字符串(例如\\E91E,hex 5C45393145 )转换为真正的utf-8值(\xee\xa4\x9e,hex EEA49E),但node-sass不这样做。并且转换后有时会导致使用Chrome时文本出现乱码。这里有关于这个问题的更多细节。

\n

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

\n