浏览器有时会使用错误的字体来呈现图标,从而导致文本混乱

时间:2019-06-28 01:53:13

标签: 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";
}

html看起来像这样:

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

结果看起来像这样:

enter image description here

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

此错误很少发生,并且很难重现。

1 个答案:

答案 0 :(得分:1)

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

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

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