简体   繁体   English

更正图标字体的字体显示值

[英]Correct font-display value for icon fonts

font-display is a new CSS property that allows developers to control how fonts are rendered depending on if they load quickly enough. font-display是一个新的 CSS 属性,它允许开发人员根据字体加载速度是否足够快来控制字体的呈现方式。 There's been a few articles on it:已经有几篇文章介绍了:

None of them mention icon fonts.他们都没有提到图标字体。 The specification does have an example that mentions icon fonts for the block value, but to me it doesn't make sense to use that:规范确实有一个例子,提到了block值的图标字体,但对我来说使用它没有意义:

'block' '阻止'

Gives the font face a short block period (3s is recommended in most cases) and an infinite swap period .为字体提供一个较短的阻塞周期(在大多数情况下推荐 3 秒)和一个无限的交换周期

If I understand the specification correctly, this means if the icons haven't loaded after the "short block period", the fallback font will be used, resulting in random letters appearing in their place.如果我正确理解规范,这意味着如果图标在“短块期”之后没有加载,将使用后备字体,导致随机字母出现在它们的位置。

If I use the optional value, the random letters will never appear but neither will the icons if they haven't loaded in the "extremely small block period".如果我使用optional值,随机字母将永远不会出现,但如果图标没有在“极小块周期”中加载,它们也不会出现。

There doesn't appear to be a value for giving an infinite block period without swap (so it would show invisible text until and unless the font loads).在没有交换的情况下提供无限块周期似乎没有值(因此它会显示不可见的文本,直到并且除非字体加载)。 Is there a reason behind this and is there a workaround?这背后是否有原因,是否有解决方法?

font-display: block;

As you commented, block still has a swap period;正如你所评论的, block仍然有一个swap期; so it's still rendered until it's loaded.所以它仍然被渲染,直到它被加载。

Quoting Chris Coyier :引用 Chris Coyier 的话

Wanna hear something quite unfortunate?想听到一些非常不幸的事情吗? We already mentioned font-display: block;我们已经提到了font-display: block; . . Wouldn't you think it, uh, well, blocked the rendering of text until the custom font loads?你不会认为,呃,好吧,在自定义字体加载之前阻止文本的呈现吗? It doesn't.它没有。 It's still got a swap period.它仍然有一个交换期。 It would be the perfect thing for something like icon fonts where the icon (probably) has no meaning unless the custom font loads.对于图标字体之类的东西来说,这将是完美的事情,除非加载自定义字体,否则图标(可能)没有意义。 Alas, there is no font-display solution for that .唉,没有字体显示解决方案


Considering alternatives:考虑替代方案:

I've also been there.我也去过那里。 If you have the chance, you might need to switch to SVG for icons.如果有机会,您可能需要将图标切换到SVG It has a few advantages over font-icons: SVG is better at pretty much everything than icon fonts .它比 font-icons 有一些优势: SVG 在几乎所有方面都比 icon fonts 更好

Even Zach Leatherman, web fonts expert, has opinions about it on his very famous Comprehensive Guide to Font Loading Strategies .甚至网络字体专家 Zach Leatherman 在他非常著名的字体加载策略综合指南中也对此发表了看法。

This guide is not intended for use with font icons, which have different loading priorities and use cases.本指南不适用于具有不同加载优先级和用例的字体图标。 Also, SVG is probably a better long term choice.此外,SVG 可能是更好的长期选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM