网站图标 - 最佳实践

117

更新的问题:

在2022年创建网站图标(favicon)时,什么被认为是最佳实践?


这是2013年提出的原始问题:

我正在努力了解到底需要哪些不同尺寸和格式的网站图标、触控图标以及现在的磁贴图标。

我阅读了这篇文章: http://www.jonathantneal.com/blog/understand-the-favicon 但我还不太清楚应该使用什么才能在所有设备和浏览器上看起来相当好,包括IE8及以上的浏览器。

我想我应该创建以下内容:

ICO
favicon.ico (32x32)

PNG
favicon.png (96x96)

磁贴图标
tileicon.png (144x144)

苹果触控图标
apple-touch-icon-precomposed.png (152x152)
根据此https://github.com/h5bp/html5-boilerplate/issues/1367

...然后使用此代码来提供它们?

<link rel="apple-touch-icon" href="path/to/touchicon.png">
<link rel="icon" href="path/to/favicon.png">
<!--[if IE]><link rel="shortcut icon" href="path/to/favicon.ico"><![endif]-->
<!-- or, set /favicon.ico for IE10 win -->
<meta name="msapplication-TileColor" content="#D83434">
<meta name="msapplication-TileImage" content="path/to/tileicon.png">

我有什么遗漏吗?

我不确定这是否适用于IE 10?


为了获得最佳答案,请查看苹果网站: Apple Developer - Ruub
1
编写时,苹果网站关于启动画面图片的信息不完整且有遗漏,可能还有其他问题。 - Muhammad Rehan Saeed
哇 - 这里有很多细节。如果你想要一个简洁、快速、易懂的答案,请查看 https://stackoverflow.com/a/45301651/661584,可能会有所帮助。谢谢。 - MemeDeveloper
5个回答

125

Favicon比它听起来的要复杂得多。10年前,只需要一个favicon.ico文件。之后,出现了触摸图标,由于不同iOS设备屏幕分辨率的原因,又有了多个触摸图标,然后是Windows的平铺图标 ...

这里的一些答案非常全面 - 令人难以承受(所有这些都只是为了一个Favicon吗?)。然而,他们没有指出在Windows中建议使用310x310平铺图标应为558x558。并且由于它们是几个月前编写的,所以它们没有提到最近的Android Chrome M39清单对OS X El Capitan上Safari的固定选项卡SVG图标

每个平台的设计是另一个棘手但被忽略的话题。例如,Favicon通常是透明的。但是iOS不支持透明度(对于此的示例,请比较SO触摸图标和将SO添加到您的iPhone主屏幕时所获得的内容)。

基于这些原因,我认为Favicon的最佳实践是 不要手动创建它。相反,使用工具自动化整个过程并强制执行平台指南。

我建议您使用RealFaviconGenerator。它生成所有您需要完成工作的图片和HTML代码:

  • favicon.ico和桌面浏览器的PNG图标
  • iOS和Android设备的Apple触摸图标
  • Windows 8平铺图标
  • 在OS X El Capitan上的Safari的固定选项卡图标
例如,它不仅生成msapplication-TileImage图片和标记,还生成IE11支持的更近期的browserconfig.xml文件。几个月前,它还更新了以支持Android Chrome清单和Safari OS X El Capitan。
完整披露:我是这个网站的作者。

1
我刚刚使用了你的网站,非常好。当它不在根目录中时,你应该包含msapplication-config元标记:<meta name="msapplication-config" content="../path/to/browserconfig.xml"> - Rick Davies
1
谢谢!关于 browserconfig.xml 的路径:它已经包含在生成的代码中了,不是吗? - philippe_b
1
@user664833 感谢您的反馈 :) 您是指声明 browserconfig.xml 的那一行吗? - philippe_b
1
@philippe_b - 是的,<meta name="msapplication-config" content="/browserconfig.xml"> 这行代码在你们提供的其他 meta 代码中确实丢失了,而 browserconfig.xml 文件本身包含在 favicons.zip 中提供。 - user664833
3
这是有意为之的。只要browserconfig.xml位于站点根目录下,它就不需要被声明。IE会自动查找它。在其他情况下(当您在选项中输入特定路径时),将生成声明。 - philippe_b
显示剩余21条评论

57

这里是我所知道的移动设备和平板电脑用网站图标的完整示例:

<!-- non-retina iPhone pre iOS 7 -->
<link rel="apple-touch-icon" href="icon57.png" sizes="57x57">
<!-- non-retina iPad pre iOS 7 -->
<link rel="apple-touch-icon" href="icon72.png" sizes="72x72">
<!-- non-retina iPad iOS 7 -->
<link rel="apple-touch-icon" href="icon76.png" sizes="76x76">
<!-- retina iPhone pre iOS 7 -->
<link rel="apple-touch-icon" href="icon114.png" sizes="114x114">
<!-- retina iPhone iOS 7 -->
<link rel="apple-touch-icon" href="icon120.png" sizes="120x120">
<!-- retina iPad pre iOS 7 -->
<link rel="apple-touch-icon" href="icon144.png" sizes="144x144">
<!-- retina iPad iOS 7 -->
<link rel="apple-touch-icon" href="icon152.png" sizes="152x152">
<!-- Win8 tile -->
<meta name="msapplication-TileImage" content="favicon-144.png">
<meta name="msapplication-TileColor" content="#B20099"/>
<meta name="application-name" content="name" />

<!-- IE11 tiles -->
<meta name="msapplication-square70x70logo" content="tile-tiny.png"/>
<meta name="msapplication-square150x150logo" content="tile-square.png"/>
<meta name="msapplication-wide310x150logo" content="tile-wide.png"/>
<meta name="msapplication-square310x310logo" content="tile-large.png"/>

IE11的常见问题解答在这里


2
太好了 - IE11也有新的更新!感谢您发布这些信息。 - Leon
4
但这不包括“apple-touch-icon-precomposed.png”和“apple-touch-icon.png”。 - Peter
2
+1 但我不确定这是否是最佳实践,虽然它很全面。对于最佳实践(在我看来)和更实用的方法,请参阅Neil Robertson下面建议的优秀备忘单。 - Boaz
你能否更新答案,使其也适用于6和6+?或者上面的代码已经可以使用了吗? - Rvervuurt
看看我的答案 https://stackoverflow.com/a/45301651/661584 更容易理解。可能会有所帮助。谢谢。 - MemeDeveloper

27
有许多不同的图标和闪屏可以设置在各种设备上。本答案将介绍如何支持它们所有。
以下是一些我用过的代码片段,包括相关信息链接。请参阅我的博客获取更多信息以及关于ASP.NET MVC Boilerplate项目模板的更多信息,该模板已经默认内置了所有这些内容(包括示例图像文件)。
在你的html头部添加以下标记。注释部分完全可选。虽然建议取消注释的部分以覆盖所有图标用法。不要害怕,大部分都是注释,以帮助你。
<!-- Icons & Platform Specific Settings - Favicon generator used to generate the icons below http://realfavicongenerator.net/ -->
<!-- shortcut icon - It is best to add this icon to the root of your site and only use this link element if you move it somewhere else. This file contains the following sizes 16x16, 32x32 and 48x48. -->
<!--<link rel="shortcut icon" href="favicon.ico">-->
<!-- favicon-96x96.png - For Google TV. -->
<link rel="icon" type="image/png" href="/content/images/favicon-96x96.png" sizes="96x96">
<!-- favicon-16x16.png - The classic favicon, displayed in the tabs. -->
<link rel="icon" type="image/png" href="/content/images/favicon-16x16.png" sizes="16x16">
<!-- favicon-32x32.png - For Safari on Mac OS. -->
<link rel="icon" type="image/png" href="/content/images/favicon-32x32.png" sizes="32x32">

<!-- Android/Chrome -->
<!-- manifest-json - The location of the browser configuration file. It contains locations of icon files, name of the application and default device screen orientation. Note that the name field is mandatory.
    https://developer.chrome.com/multidevice/android/installtohomescreen. -->
<link rel="manifest" href="/content/icons/manifest.json">
<!-- theme-color - The colour of the toolbar in Chrome M39+
    http://updates.html5rocks.com/2014/11/Support-for-theme-color-in-Chrome-39-for-Android -->
<meta name="theme-color" content="#1E1E1E">
<!-- favicon-192x192.png - For Android Chrome M36 to M38 this HTML is used. M39+ uses the manifest.json file. -->
<link rel="icon" type="image/png" href="/content/icons/favicon-192x192.png" sizes="192x192">
<!-- mobile-web-app-capable - Run Android/Chrome version M31 to M38 in standalone mode, hiding the browser chrome. -->
<!-- <meta name="mobile-web-app-capable" content="yes"> -->

<!-- Apple Icons - You can move all these icons to the root of the site and remove these link elements, if you don't mind the clutter.
    https://developer.apple.com/library/safari/documentation/AppleApplications/Reference/SafariHTMLRef/Introduction.html#//apple_ref/doc/uid/30001261-SW1 -->
<!-- apple-mobile-web-app-title - The name of the application if pinned to the IOS start screen. -->
<!--<meta name="apple-mobile-web-app-title" content="">-->
<!-- apple-mobile-web-app-capable - Hide the browsers user interface on IOS, when the app is run in 'standalone' mode. Any links to other pages that are clicked whilst your app is in standalone mode will launch the full Safari browser. -->
<!--<meta name="apple-mobile-web-app-capable" content="yes">-->
<!-- apple-mobile-web-app-status-bar-style - default/black/black-translucent Styles the IOS status bar. Using black-translucent makes it transparent and overlays it on top of your site, so make sure you have enough margin. -->
<!--<meta name="apple-mobile-web-app-status-bar-style" content="black">-->
<!-- apple-touch-icon-57x57.png - Android Stock Browser and non-Retina iPhone and iPod Touch -->
<link rel="apple-touch-icon" sizes="57x57" href="/content/images/apple-touch-icon-57x57.png">
<!-- apple-touch-icon-114x114.png - iPhone (with 2× display) iOS = 6 -->
<link rel="apple-touch-icon" sizes="114x114" href="/content/images/apple-touch-icon-114x114.png">
<!-- apple-touch-icon-72x72.png - iPad mini and the first- and second-generation iPad (1× display) on iOS = 6 -->
<link rel="apple-touch-icon" sizes="72x72" href="/content/images/apple-touch-icon-72x72.png">
<!-- apple-touch-icon-144x144.png - iPad (with 2× display) iOS = 6 -->
<link rel="apple-touch-icon" sizes="144x144" href="/content/images/apple-touch-icon-144x144.png">
<!-- apple-touch-icon-60x60.png - Same as apple-touch-icon-57x57.png, for non-retina iPhone with iOS7. -->
<link rel="apple-touch-icon" sizes="60x60" href="/content/images/apple-touch-icon-60x60.png">
<!-- apple-touch-icon-120x120.png - iPhone (with 2× and 3 display) iOS = 7 -->
<link rel="apple-touch-icon" sizes="120x120" href="/content/images/apple-touch-icon-120x120.png">
<!-- apple-touch-icon-76x76.png - iPad mini and the first- and second-generation iPad (1× display) on iOS = 7 -->
<link rel="apple-touch-icon" sizes="76x76" href="/content/images/apple-touch-icon-76x76.png">
<!-- apple-touch-icon-152x152.png - iPad 3+ (with 2× display) iOS = 7 -->
<link rel="apple-touch-icon" sizes="152x152" href="/content/images/apple-touch-icon-152x152.png">
<!-- apple-touch-icon-180x180.png - iPad and iPad mini (with 2× display) iOS = 8 -->
<link rel="apple-touch-icon" sizes="180x180" href="/content/images/apple-touch-icon-180x180.png">

<!-- Apple Startup Images - These are shown when the page is loading if the site is pinned https://gist.github.com/tfausak/2222823 -->
<!-- apple-touch-startup-image-1536x2008.png - iOS 6 & 7 iPad (retina, portrait) -->
<link rel="apple-touch-startup-image"
      href="/content/images/apple-touch-startup-image-1536x2008.png"
      media="(device-width: 768px) and (device-height: 1024px) and (orientation: portrait) and (-webkit-device-pixel-ratio: 2)">
<!-- apple-touch-startup-image-1496x2048.png - iOS 6 & 7 iPad (retina, landscape) -->
<link rel="apple-touch-startup-image"
      href="/content/images/apple-touch-startup-image-1496x2048.png"
      media="(device-width: 768px) and (device-height: 1024px) and (orientation: landscape) and (-webkit-device-pixel-ratio: 2)">
<!-- apple-touch-startup-image-768x1004.png - iOS 6 iPad (portrait) -->
<link rel="apple-touch-startup-image"
      href="/content/images/apple-touch-startup-image-768x1004.png"
      media="(device-width: 768px) and (device-height: 1024px) and (orientation: portrait) and (-webkit-device-pixel-ratio: 1)">
<!-- apple-touch-startup-image-748x1024.png - iOS 6 iPad (landscape) -->
<link rel="apple-touch-startup-image"
      href="/content/images/apple-touch-startup-image-748x1024.png"
      media="(device-width: 768px) and (device-height: 1024px) and (orientation: landscape) and (-webkit-device-pixel-ratio: 1)">
<!-- apple-touch-startup-image-640x1096.png - iOS 6 & 7 iPhone 5 -->
<link rel="apple-touch-startup-image"
      href="/content/images/apple-touch-startup-image-640x1096.png"
      media="(device-width: 320px) and (device-height: 568px) and (-webkit-device-pixel-ratio: 2)">
<!-- apple-touch-startup-image-640x920.png - iOS 6 & 7 iPhone (retina) -->
<link rel="apple-touch-startup-image"
      href="/content/images/apple-touch-startup-image-640x920.png"
      media="(device-width: 320px) and (device-height: 480px) and (-webkit-device-pixel-ratio: 2)">
<!-- apple-touch-startup-image-320x460.png - iOS 6 iPhone -->
<link rel="apple-touch-startup-image"
      href="/content/images/apple-touch-startup-image-320x460.png"
      media="(device-width: 320px) and (device-height: 480px) and (-webkit-device-pixel-ratio: 1)">

<!-- Windows 8 Icons - If you add an RSS feed, revisit this page and regenerate the browserconfig.xml file. You will then have a cool live tile!
     browserconfig.xml - Windows 8.1 - Has been added to the root of the site. This points to the tile images and tile background colour. It contains the following images:
     mstile-70x70.png - For Windows 8.1 / IE11.
     mstile-144x144.png - For Windows 8 / IE10.
     mstile-150x150.png - For Windows 8.1 / IE11.
     mstile-310x310.png - For Windows 8.1 / IE11.
     mstile-310x150.png - For Windows 8.1 / IE11.
     See http://www.buildmypinnedsite.com/en and http://msdn.microsoft.com/en-gb/library/ie/dn255024%28v=vs.85%29.aspx. -->
<!-- application-name - Windows 8+ - The name of the application if pinned to the start screen. -->
<!--<meta name="application-name" content="">-->
<!-- msapplication-TileColor - Windows 8 - The tile colour which shows around your tile image (msapplication-TileImage). -->
<meta name="msapplication-TileColor" content="#5cb95c">
<!-- msapplication-TileImage - Windows 8 - The tile image. -->
<meta name="msapplication-TileImage" content="/content/images/mstile-144x144.png">

我的browserconfig.xml文件。完整的解释在上面。
<?xml version="1.0" encoding="utf-8"?>
<browserconfig>
  <msapplication>
    <tile>
      <square70x70logo src="/Content/Images/mstile-70x70.png"/>
      <square150x150logo src="/Content/Images/mstile-150x150.png"/>
      <square310x310logo src="/Content/Images/mstile-310x310.png"/>
      <wide310x150logo src="/Content/Images/mstile-310x150.png"/>
      <TileColor>#5cb95c</TileColor>
    </tile>
  </msapplication>
</browserconfig>

我的 manifest.json 文件。详细解释请参见上文。

{
    "name": "ASP.NET MVC Boilerplate (Required! Update This)",
    "icons": [
        {
            "src": "\/Content\/icons\/android-chrome-36x36.png",
            "sizes": "36x36",
            "type": "image\/png",
            "density": "0.75"
        },
        {
            "src": "\/Content\/icons\/android-chrome-48x48.png",
            "sizes": "48x48",
            "type": "image\/png",
            "density": "1.0"
        },
        {
            "src": "\/Content\/icons\/android-chrome-72x72.png",
            "sizes": "72x72",
            "type": "image\/png",
            "density": "1.5"
        },
        {
            "src": "\/Content\/icons\/android-chrome-96x96.png",
            "sizes": "96x96",
            "type": "image\/png",
            "density": "2.0"
        },
        {
            "src": "\/Content\/icons\/android-chrome-144x144.png",
            "sizes": "144x144",
            "type": "image\/png",
            "density": "3.0"
        },
        {
            "src": "\/Content\/icons\/android-chrome-192x192.png",
            "sizes": "192x192",
            "type": "image\/png",
            "density": "4.0"
        }
    ]
}

项目中文件列表(请注意,如果您决定将其中一些文件放在项目的根目录下以避免使用上述元标记,则这些文件的名称很重要):
favicon.ico
browserconfig.xml
Content/Images/
    android-chrome-144x144.png
    android-chrome-192x192.png
    android-chrome-36x36.png
    android-chrome-48x48.png
    android-chrome-72x72.png
    android-chrome-96x96.png
    apple-touch-icon.png
    apple-touch-icon-57x57.png
    apple-touch-icon-60x60.png
    apple-touch-icon-72x72.png
    apple-touch-icon-76x76.png
    apple-touch-icon-114x114.png
    apple-touch-icon-120x120.png
    apple-touch-icon-144x144.png
    apple-touch-icon-152x152.png
    apple-touch-icon-180x180.png
    apple-touch-icon-precomposed.png (180x180)
    favicon-16x16.png
    favicon-32x32.png
    favicon-96x96.png
    favicon-192x192.png
    manifest.json
    mstile-70x70.png
    mstile-144x144.png
    mstile-150x150.png
    mstile-310x150.png
    mstile-310x310.png
    apple-touch-startup-image-1536x2008.png
    apple-touch-startup-image-1496x2048.png
    apple-touch-startup-image-768x1004.png
    apple-touch-startup-image-748x1024.png
    apple-touch-startup-image-640x1096.png
    apple-touch-startup-image-640x920.png
    apple-touch-startup-image-320x460.png

总开销

如果您去掉注释,那么额外的HTML代码就是3KB,如果您不支持闪屏,那么就是1.5KB。如果您在HTML内容上使用GZIP压缩(现在每个人都应该这样做),那么每个请求需要634字节的开销来支持所有平台,或者没有闪屏的话,只需要446字节。我个人认为值得支持IOS、Android和Windows设备,但这是您的选择,我只是提供选项!

关于当前Web图标/闪屏/设置情况的附注

目前,特定于供应商的图标、闪屏和特殊标签来控制Web浏览器或固定的图标的情况是荒谬的。在完美的世界里,我们都会使用一个favicon.svg文件,它可以在任何大小下看起来很好,并且可以放在页面的根目录下。目前只有FireFox支持这一点(请参见CanIUse.com)。

然而,图标并不是唯一的设置,现在还有几个特定于供应商的设置(如上所示),但favicon.svg文件可以涵盖大多数用例。

更新

更新以包括新的Android/Chrome版本M39+的favicon/theming选项。有趣的是,他们采用了类似于Microsoft的方法,但使用的是JSON文件而不是XML。


4
这太过分了。(不是你的回答,而是浏览器厂商有太多的要求 :)) - jor
1
@jor 完全同意,这是一个愚蠢的情况。SVG网站图标可以解决90%的问题。 - Muhammad Rehan Saeed
看看我的答案 https://stackoverflow.com/a/45301651/661584 更容易理解。可能会有所帮助。谢谢。 - MemeDeveloper
你提到文件名很重要,但我不太明白你指的是什么。我的问题是,如果我在清单中和HTML文档头部的<link>标签中更新名称,那么我是否可以将所有文件重命名为以“favicon-”为前缀? - SherylHohman

10

最简单的解决方案是在2020年只使用一个PNG图像。

只需将此代码添加到您文档的头部:

<link rel="shortcut icon" type="image/png" href="/img/icon-192x192.png">
<link rel="shortcut icon" sizes="192x192" href="/img/icon-192x192.png">
<link rel="apple-touch-icon" href="/img/icon-192x192.png">

最后一个链接是苹果的(主屏幕),第二个链接是安卓的(主屏幕),第一个链接是其他平台的。
请注意,此解决方案不支持 Windows 8/10 中的“磁贴”功能。它支持快捷方式、书签和浏览器选项卡中的图像。
大小与安卓主屏幕使用的大小完全相同。苹果主屏幕图标大小为 60px(3x),因此为 180px,并将缩小。其他平台使用默认的快捷方式图标,也将被缩小。

1
只是想知道196像素尺寸是从哪里来的。这是特定设备的规格还是标准? - bluesixty
这是Android主屏幕规范:https://www.emergeinteractive.com/insights/detail/The-Essentials-of-FavIcons/。一开始它是一个196×196的图标。我重新阅读了规范,这是我有一段时间没有做过的事情。我意识到有一个变化:196×196变成了192×192。https://realfavicongenerator.net/blog/android-chrome-and-its-favicon/ - Mr. Hugo

1

我其实也在思考同样的问题,试图寻找任何可以集成到构建步骤中或简化所需资源和标记创建的简单项目。

我没有找到符合我的要求的东西,所以我创建了faviconbuild并在MIT许可证下发布。

该项目的目的是创建一个集中、可维护且本地可运行的跨平台工具来构建网站图标和支持标记。它利用了Imagemagick的强大功能,因此您需要为您的平台下载它,或者使用我在releases中提供的版本。请随意在商业或个人项目中使用此工具,贡献、提交特性请求,或仅将其用作自己工具的灵感来源。

该项目由Windows批处理文件和Unix / Mac的bash脚本(或通过Cygwin在Windows上)组成。您可以从内部帮助选项-h或--help获取完整的支持选项列表。

ex:

./faviconbuild.sh -h

两个脚本都解析一个简单的文本文件,你也可以使用 -p 或 --parsed 选项覆盖它。该文件基本上只是一个命令模板,以便在需要时更轻松地自定义输出。
我还发布了一篇博客文章,介绍了开发过程以及有关 bash/batch 脚本的迷你教程。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接