字体内的图标对网站有用吗?

发布于 2024-09-17 11:50:15 字数 766 浏览 4 评论 0原文

我想创建仅包含图标的特殊字体。每个字母都有不同的图标。 我想把这个字体嵌入到css中。 并像这样使用 如果我需要一些图标:

<a class="icon">f</a>

和包含以下内容的CSS

<link href='http://fonts.mysite.com/css?family=MyFontWithIcons' rel='stylesheet' type='text/css'>

@media screen {
@font-face {
  font-family: 'MyFontWithIcons';
  font-style: normal;
  font-weight: normal;
  src: local('MyFontWithIcons'), url('http://fonts.mysite.com/font?MyFontWithIcons') format('truetype');
}
}

并显示带有图标类的图标:

.icon {font-family: 'MyFontWithIcons'; }

并且字母“f”将被替换为位于f字母位置的字体内的图标。 css 与字体一起使用,并将 f 替换为字体内的图标。 我认为这是好主意还是坏主意?字体文件的大小比许多图像图标小。 而且通过这种技术,我可以使用不同的字体颜色=不同的图标颜色?尺寸等。 那么,这是好主意还是坏主意?

I wanna create special font containing only icons. and for each letter will be diffirent icon.
I want to embed this font in css.
and use like this
if i need some icon:

<a class="icon">f</a>

and css

<link href='http://fonts.mysite.com/css?family=MyFontWithIcons' rel='stylesheet' type='text/css'>

which contains:

@media screen {
@font-face {
  font-family: 'MyFontWithIcons';
  font-style: normal;
  font-weight: normal;
  src: local('MyFontWithIcons'), url('http://fonts.mysite.com/font?MyFontWithIcons') format('truetype');
}
}

and to show icons with icon class:

.icon {font-family: 'MyFontWithIcons'; }

And letter "f" will be replaced with icon inside font which located on the place of f letter.
and css works with font and replacing f with icon inside font.
I think it's good idea or not? font file is less in size than lots of image icons.
and also with this technique I can use diffirent font colors = diffirent icon colors? sizes and so on.
So, it's good idea or not?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

场罚期间 2024-09-24 11:50:15

使用图标的自定义字体的问题是,如果出现以下情况,您将没有备份计划:

  • 您的用户的浏览器太旧,无法支持 @font-face
  • 用户代理不是您的传统浏览器(例如盲人屏幕阅读器、搜索引擎、HTML 到文本转换器等)
  • 用户将包含“图标”的文本复制粘贴到丢弃丰富格式信息或不包含相关字体的内容中。
  • 用户代理永远不会支持@font-face(例如,用于 Unix/Linux 的 Lynx、Links2 和 ELinks 等文本 Web 浏览器)
  • 用户位于公司代理后面,该代理会丢弃或破坏浏览器的标头显示自定义字体之前的要求。 (比您想象的更常见)
  • 用户正在运行 NoScript,并启用默认字体阻止行为,以防止字体引擎中的 0day 漏洞。

出于这个原因,图像提供了alt 属性。

但是,如果您要使用图标字体,请确保将字形存储在 Unicode 私人使用区域。这将通过确保可能没有冲突来稍微缓解问题(公司有时可以并且确实使用 PUA 字形来存储自定义数据),并且如果它们编码得巧妙,屏幕阅读器可以知道如何优雅地忽略 PUA 字形。

至于实现后备,我建议使用 Modernizr (具体来说,Modernizr.fontface) 来测试支持。

The problem with using a custom font for icons is that you've got no backup plan if:

  • Your user's browser is too old to support @font-face
  • The user agent isn't your traditional browser (eg. Screen readers for the blind, search engines, HTML-to-text converters, etc.)
  • The user copy-pastes the text containing the "icon" into something that discards rich formatting information or doesn't have the font in question.
  • The user agent will never support @font-face (Eg. Textual web browsers like Lynx, Links2, and ELinks for Unix/Linux)
  • The user is behind a corporate proxy that discards or mangles headers that your browser demands before displaying custom fonts. (more common than you think)
  • The user is running NoScript with the default font-blocking behaviour enabled to protect against 0-day exploits in the font engine.

Images provide the alt attribute for just this reason.

However, if you're going to use a font for icons, make sure you store your glyphs in a Unicode Private Use Area. That'll mitigate the problem a bit by ensuring there's probably no conflict (Companies can and do sometimes use PUA glyphs to store custom data) and, if they're coded smartly, screen readers could know to gracefully ignore PUA glyphs.

As for implementing a fallback, I'd suggest using Modernizr (specifically, Modernizr.fontface) to test for support.

时光倒影 2024-09-24 11:50:15

这是一个好主意,但它不适用于所有浏览器(目前)。此外,它仅适用于 2 种彩色图像(黑色和透明),这使得它相当有限。对于拥有文本浏览器或文本转语音软件的人来说,这没有任何意义。它并不是真正的“语义”,因为 标签是用于图像的。从纯 html 的角度来看,使用字体没有任何意义。它还会让你的 CSS 变得混乱。

因此,仅仅减少一点带宽就会带来很多不好的事情;我不会打扰。

It's a good idea but it doesn't work in all browsers (yet). Also, it only works with 2 color images (black and transparent), which makes it rather limited. For people with text browsers or text to speech software, it won't make any sense. It's not really 'semantic', because the <img>-tag was meant for images. Using fonts for that won't make any sense from a html-only perspective. It also clutters your css.

So that's a lot of bad things just for a little bit less bandwith; I wouldn't bother.

嘴硬脾气大 2024-09-24 11:50:15
  • @font-face 并未在各浏览器中得到统一支持。迎合所有浏览器是一项艰巨的任务,尤其是。如果包括 IE6
  • 辅助功能:屏幕阅读器不会读取图标,他们仍然会看到“f”而不是图标
  • 您需要一些后备才能正常降级
  • @font-face is not supported uniformly across browsers. Its a bit of a task to cater to all browsers esp. if including IE6
  • Accessibility: screen readers won't read icons, they would still see an 'f' and not an icon
  • You would need some fallback to degrade gracefully
疯狂的代价 2024-09-24 11:50:15

所有其他答案都集中在缺点上。
我个人非常喜欢使用图标字体。
以下是使用图标字体的一些充分理由:

  1. 您可以无缝地使用与文本内嵌的图标。如果您在文本块内包含图标,它将继承其父级的字体大小和颜色,并将与随附文本的基线对齐。
  2. 您可以使用 css 动态更改图标的颜色,而无需创建和上传额外的图像文件并使用 js 来操作标记。这意味着a)您将样式确定保留在它们所属的位置,即在您的样式表中,b)您可以简单轻松地使图标颜色上下文(例如标题内的所有图标都是白色的,否则它们为黑色)和主题(例如,如果主体具有dark-theme类,则所有图标均为白色,如果主体具有tropical-theme类,则所有图标均为粉红色code>)
  3. 可以轻松缩放图标,而无需使用额外的带宽(就像增加图像文件的大小一样),而无需创建和上传额外的图像文件。推论:您不必担心不同的屏幕分辨率,也不需要考虑视网膜等。
  4. 您的字体文件的文件大小可能比图像文件更小,因此,再次节省带宽。
  5. 如果您使用 fontello 等字体生成器,您的图标集合将自动编目,以供您和您的设计师在演示文件中仔细检查。此外,fontello 有一个 API,因此您的字体生成可以作为构建过程的一部分自动管理。

All the other answers focus on the downsides.
I am, personally, a big fan of using icon fonts.
Here are some good reasons to use an icon font:

  1. You can seamlessly use icons inline with text. If you include an icon inside of a block of text, it will inherit the font size and color of its parent, and will be aligned with the accompanying text's baseline.
  2. you can change the color of icon dynamically using css, without having to create and upload additional image files and use js to manipulated the markup. this means that a) you are keeping style determinations where they belong, i.e. in your stylesheets and b) you can simply and easily make your icon colors contextual (e.g. all icons inside headers are white, or else they are black) and themable (e.g. all icons are white if body has class dark-theme, and all icons are pink if body has class tropical-theme)
  3. the icon can easily be scaled without using additional bandwidth (as it would if you were increasing the size of the image file), without having to create and upload additional image files. corollary: you don't have to worry about different screen resolutions and don't need to make any allowances for retina, etc.
  4. your font file will likely have a smaller file size than your image files would so, again, save on bandwidth.
  5. if you use a font generator such as fontello, your collection of icons will automatically be catalogued for you and your designers to scrutinize, in a demo file. in addition, fontello has an API so your font generation can be managed automatically as part of your build process.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文