我一直在做一个网站,我想在浏览器选项卡上添加一个小图标。

我怎么能在HTML中做到这一点,在代码的哪里,我需要把它(例如头)?我有一个。png徽标文件,我想转换为一个图标。

相关:HTML设置图像浏览器选项卡。


当前回答

HTML标准的链接rel=icon

为了完整起见,这个标准是这样写的:https://html.spec.whatwg.org/multipage/links.html#rel-icon

The icon keyword may be used with link elements. This keyword creates an external resource link. The specified resource is an icon representing the page or site, and should be used by the user agent when representing the page in the user interface. [...] The following snippet shows the top part of an application with several icons. <!DOCTYPE HTML> <html lang="en"> <head> <title>lsForums — Inbox</title> <link rel=icon href=favicon.png sizes="16x16" type="image/png"> <link rel=icon href=windows.ico sizes="32x32 48x48" type="image/vnd.microsoft.icon"> <link rel=icon href=mac.icns sizes="128x128 512x512 8192x8192 32768x32768"> <link rel=icon href=iphone.png sizes="57x57" type="image/png"> <link rel=icon href=gnome.svg sizes="any" type="image/svg+xml"> <link rel=stylesheet href=lsforums.css> <script src=lsforums.js></script> <meta name=application-name content="lsForums"> </head> <body> ... For historical reasons, the icon keyword may be preceded by the keyword "shortcut". If the "shortcut" keyword is present, the rel attribute's entire value must be an ASCII case-insensitive match for the string "shortcut icon" (with a single U+0020 SPACE character between the tokens and no other ASCII whitespace).

其他回答

使用工具将png转换为ico文件。你可以搜索“favicon生成器”,你可以找到许多在线工具。 将ico地址与链接标签放在头部: <link rel="快捷图标" href="http://sstatic.net/stackoverflow/img/favicon.ico">

对于Chrome浏览器显示页面图标(favicon),您需要从托管服务器检查您的网站,或者您可以在PC上开发和测试您的网站时使用本地主机。

我已经成功地为我的网站做到了这一点。

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

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

它们还生成您需要用于它们生成的文件的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" />

我看了前20个左右的谷歌结果,这是迄今为止最好的。

请使用下面的代码在标题部分你的索引文件。

<link rel="icon" href="yourfevicon.ico" />