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

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

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

相关:HTML set image on browser tab .

最佳答案

实际上有两种方法可以将网站图标添加到网站。

<link rel="icon">

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

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

PNG 网站图标 are supported by most browsers, except IE <= 10 .为了向后兼容,您可以使用 ICO favicons。

请注意,您不必在 icon 之前在 rel shortcut 的属性 了。来自 MDN Link types :

The shortcut link type is often seen before icon, but this link type is non-conforming, ignored and web authors must not use it anymore.

favicon.ico在根目录中

来自 another SO answer (作者 @mercator ):

All modern browsers (tested with Chrome 4, Firefox 3.5, IE8, Opera 10 and Safari 4) will always request a favicon.ico unless you've specified a shortcut icon via <link>.

所以你所要做的就是制作 /favicon.ico请求您的网站返回您的网站图标。遗憾的是,此选项不允许您使用 PNG 图标。

另见 favicon.png vs favicon.ico - why should I use PNG instead of ICO?

https://stackoverflow.com/questions/4888377/

相关文章:

html - 将 div 放入 anchor 内是否正确?

javascript - HTML5 本地存储与 session 存储

javascript - 单击时选择 HTML 文本输入中的所有文本

javascript - jQuery 数据与属性?

html - 展开一个 div 以填充剩余的宽度

HTML 输入 - 名称与 id

html - HTML div 和 span 元素有什么区别?

html - 目标 ="_blank"与目标 ="_new"

html - 使用 CSS 使 div 可垂直滚动

html - 在页面上垂直和水平居中

的最佳方法?