如何为网站添加浏览器标签图标(图标)?


577

我一直在一个网站上工作,我想在浏览器标签中添加一个小图标。

如何在HTML中执行此操作以及需要在代码中放置它的位置(例如标头)?我有一个.png徽标文件,希望将其转换为图标。

相关:浏览器标签上的HTML设置图像


1
检查链接以获取更多信息。您走在正确的轨道上。
reggie 2011年

使用FTP将图像(favicon.ico)上传到网站的根目录(请阅读我们的FTP教程)。根部分是主文件区域,您将在其中存储主页的index.html(index.asp,index.php等)文件。 davesite.com将是根davesite.com/webstation不会是根davesite.com/webstation/html不是root或者这个文件,摆放不当,将加载作为默认为整个域。
knookie

在此过早按下Enter键-无论如何,谢谢您的回复。只是要说您提供的链接中的第一部分(我在上面粘贴了)似乎无效?但是第二部分工作得很好-谢谢
knookie 2011年

Answers:


415

实际上,有两种向网站添加图标的方式。

<link rel="icon">

只需将以下代码添加到<head>元素中:

<link rel="icon" href="http://example.com/favicon.png">

大多数浏览器均支持 PNG图标,但IE <= 10除外。为了向后兼容,您可以使用ICO图标。

请注意,您不必再iconrel属性之前添加属性shortcut。从MDN链接类型

shortcut链接类型通常见过icon,但这种链接类型是不符合要求的,忽略网页制作者不能再使用它

favicon.ico 在根目录中

另一个SO答案(通过@mercator):

favicon.ico除非您通过指定了快捷方式图标,否则所有现代浏览器(经过Chrome 4,Firefox 3.5,IE8,Opera 10和Safari 4的测试)都将始终请求<link>

因此,您要做的就是/favicon.ico向您的网站发出请求,以返回您的网站图标。不幸的是,此选项不允许您使用PNG图标。

另请参见favicon.png和favicon.ico-为什么我应该使用PNG而不是ICO?


1
如果您要自动使用所有图标(包括移动图标,“添加到主屏幕”图标,移动书签,iOS / Droid,桌面标签等),ICO文件生成等,以及上述元文件的可复制粘贴的HTML代码,一键操作,您就可以使用这个方便的工具(我不隶属于) realfavicongenerator.net
Albert Renshaw

2
要添加:您的图标应为正方形,表示宽度等于高度。
Nguyen Tan Dat '18

3
测试时不要忘记按Ctrl + F5。有时会被缓存。
Hairi

我并没有发现任何导致Firefox或Chrome加载它在我的网站的根目录,仅仅将命名为favicon.ico ICO文件。所以我走了<link rel路线。但是,我的网站地址未在DNS中可靠地列出,因此我无法指望href="http://example.com"它。因此,我在index.htm文件中找到了以下解决方案:<link rel="icon" href="favicon.ico">
CODE-REaD

426
  1. 使用工具将png转换为ico文件。您可以搜索“网站图标生成器”,并且可以找到许多在线工具。
  2. head使用- link标签将ico地址放入中:

    <link rel="shortcut icon" href="http://sstatic.net/stackoverflow/img/favicon.ico">

3
谢谢!我已正确执行了这些步骤,它在IE和Firefox上确实运行良好,但是由于某种原因,它在Google Chrome上无法正常工作,我是否必须删除缓存?或从那里我该怎么办?
PatrickGamboa


2
“快捷方式”部分可以在许多情况下将其删除:stackoverflow.com/questions/13211206/...mathiasbynens.be/notes/rel-shortcut-icon
英国广播公司

72

我发现最好的是http://www.favicomatic.com/, 我说的最好,因为它给了我最清晰的图标,并且在转换后不需要编辑。它将生成16x16和32x32的收藏夹图标,并引用它们“每个该死的大小,先生!” 此外,他们的网站看起来很酷并且易于使用。

它们还会生成您需要用于它们生成的文件的html。

<link rel="apple-touch-icon-precomposed" sizes="57x57" href="apple-touch-icon-57x57.png" />
<link rel="apple-touch-icon-precomposed" sizes="114x114" href="apple-touch-icon-114x114.png" />
<link rel="apple-touch-icon-precomposed" sizes="72x72" href="apple-touch-icon-72x72.png" />
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="apple-touch-icon-144x144.png" />
<link rel="apple-touch-icon-precomposed" sizes="60x60" href="apple-touch-icon-60x60.png" />
<link rel="apple-touch-icon-precomposed" sizes="120x120" href="apple-touch-icon-120x120.png" />
<link rel="apple-touch-icon-precomposed" sizes="76x76" href="apple-touch-icon-76x76.png" />
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="apple-touch-icon-152x152.png" />
<link rel="icon" type="image/png" href="favicon-196x196.png" sizes="196x196" />
<link rel="icon" type="image/png" href="favicon-96x96.png" sizes="96x96" />
<link rel="icon" type="image/png" href="favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="favicon-16x16.png" sizes="16x16" />
<link rel="icon" type="image/png" href="favicon-128.png" sizes="128x128" />
<meta name="application-name" content="&nbsp;"/>
<meta name="msapplication-TileColor" content="#FFFFFF" />
<meta name="msapplication-TileImage" content="mstile-144x144.png" />
<meta name="msapplication-square70x70logo" content="mstile-70x70.png" />
<meta name="msapplication-square150x150logo" content="mstile-150x150.png" />
<meta name="msapplication-wide310x150logo" content="mstile-310x150.png" />
<meta name="msapplication-square310x310logo" content="mstile-310x310.png" />

我查看了Google的前20个结果,这是迄今为止最好的结果。


4
我应该将所有这些都放在每页的head标签中吗?
Noumenon

1
我不会将所有内容都放在您的head标签中。我只会放入支持您需要支持的设备所需的标签。老实说,您可以使用一个<link rel =“ icon” type =“ image / ico” href =“ / images / favicon.ico”>
Jared Menard

5
这个git页面解释了这些图标大多数用于什么的:github.com/audreyr/favicon-cheat-sheet
Jared Menard,

14

您可以为各种设备设置许多不同的图标,甚至是启动屏幕。这个答案涉及如何全力支持他们。

以下是我使用的一些摘录,以及指向我收集信息的地方的相关链接。请参阅我的博客,以获取有关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

总开销

如果您删除了3KB额外HTML的注释,则不支持1.5KB的启动屏幕。如果您正在对HTML内容使用GZIP压缩(如今每个人都应该这样做),那么每个请求支持所有平台的开销约为634字节,而没有启动屏幕的则为446字节。我个人认为支持IOS,Android和Windows设备是值得的,但是您可以选择,我只是提供选择!

关于当前Web图标/初始屏幕/设置情况的旁注

这种带有特定于供应商的图标,初始屏幕和用于控制Web浏览器或固定图标的特殊标签的情况是荒谬的。在理想的情况下,我们都将使用favicon.svg文件,该文件无论大小如何都可以看起来很好,并且可以放在页面的根目录。在撰写本文时,只有FireFox支持此功能(请参见CanIUse.com)。

但是,图标并不是当今唯一的设置,还有其他几个特定于供应商的设置(如上所示),但favicon.svg文件将涵盖大多数用例。

更新资料

已更新,以包括新的Android / Chrome版本M39 +网站图标/主题选项。有趣的是,它们采用了与Microsoft类似的方法,但是使用的是JSON文件而不是XML。



10

我已经为我的网站成功完成了此操作。

唯一的例外是,SeaMonkey浏览器需要在您的<head>;中插入HTML代码。而其他浏览器仍会显示favicon.ico而不插入任何HTML。此外,除IE以外的任何浏览器都可以使用其他类型的图像,而不仅仅是.ico格式。我希望这有帮助。


5

上面有很多复杂的解决方案。为了我?在将图像尺寸更改为32 x 32像素之后,我使用GIMP保存了原始PNG文件的副本。

只要确保将其另存为* .ico文件并使用

<link rel="shortcut icon" href="http://sstatic.net/stackoverflow/img/favicon.ico">

以上所列


4

我建议您尝试http://faviconer.com将.PNG或.GIF转换为.ICO文件。

您可以在一个.ICO文件中同时创建16x1632x32(用于新的视网膜显示)。

IE和Firefox没有问题



2
<link rel="shortcut icon" 
href="http://someWebsiteLocation/images/imageName.ico">

如果我可以为仍然困惑的你们增加更多的清晰度。.ico文件往往比.png文件提供更高的透明度,这就是为什么我建议如上所述在此处转换图像的原因:http : //www.favicomatic.com/done 也是如此,href内只是图像的位置,它可以是任何服务器位置,请记住在前面添加http://,否则它将无法工作。


1

我没有使用过其他任何东西,但是https://realfavicongenerator.net/似乎是一个最佳选择,并且这里还没有提到。

它支持SVG作为生成图标的源图像,并提供有用的选项来覆盖不同平台的图像。此外,默认情况下,它不会生成大量与每个过时的平台向后兼容的图像。相反,它为您提供了检查是否需要它们的选项。

从开发人员发送给我的电子邮件中,他们还计划增加对生成SVG图标的支持,以及SVG主题敏感度(我认为这是一个非常棒的功能)。


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.