简体   繁体   English

Safari 图标错误地呈现为白色背景

[英]Safari favicon incorrectly rendering with white background

Edit: I have discovered that this is due to dark mode because there not enough contrast between the favicon and the background.编辑:我发现这是由于暗模式造成的,因为图标和背景之间没有足够的对比度。 However, is there still a way to disable this?但是,还有办法禁用它吗? I made a mock image file with the icon and the contrast seems to be enough.我用图标制作了一个模拟图像文件,对比度似乎就足够了。


I am attempting to add a favicon to an HTML website.我正在尝试向HTML网站添加收藏夹图标。 However, in Safari, the favicon is incorrectly rendered with a white background (see image below).但是,在 Safari 中,图标被错误地渲染为白色背景(见下图)。 This is unexpected, as the file provided is a transparent svg .这是出乎意料的,因为提供的文件是透明的svg

To include the favicon into safari, I used the mask-icon link attribute to tell Safari where the favicon is located at.为了将收藏mask-icon包含在 safari 中,我使用了mask-icon链接属性来告诉 Safari 收藏mask-icon所在的位置。 If this is not defined, Safari will use the default favicon in the icon link attribute.如果未定义,Safari 将使用icon链接属性中的默认图标。 However, my icon does not work well in Safari like this, so a separate one is defined for Safari using the code below.但是,我的图标在 Safari 中不能像这样很好地工作,因此使用下面的代码为 Safari 定义了一个单独的图标。

<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#2163d9">

This follows Apple's developer guidelines on creating pinned tab icons .这遵循Apple 关于创建固定选项卡图标的开发人员指南 The guidelines state that the image file should comply with the following:该指南指出,图像文件应符合以下要求:

  • 100% black vectors 100% 黑色矢量
  • One layer一层
  • viewBox attribule of 0 0 16 16 viewBox属性为0 0 16 16

Here is the SVG file.这是SVG文件。

<svg xmlns="http://www.w3.org/2000/svg" 
    xmlns:xlink="http://www.w3.org/1999/xlink" width="16" height="16" viewBox="0 0 16 16">
    <defs>
        <style>.a{clip-path:url(#b);}</style>
        <clipPath id="b">
            <rect width="16" height="16"/>
        </clipPath>
    </defs>
    <g id="a" class="a">
        <g transform="translate(-254 -191.5)">
            <path d="M299.962-203.031l-3.973-2.485L299.962-208Z" transform="translate(-31.961 405.016)"/>
            <path d="M-42.6-75.784l5.539,2.769,6-3.985-4.8-3Z" transform="translate(299.062 280.016)"/>
            <path d="M-82-313l6-3,5.625,4.219L-82-304.515Z" transform="translate(338 508)"/>
        </g>
    </g>
</svg>

However, this still results in the incorrect rendering of the favicon.但是,这仍然会导致网站图标的渲染不正确。 I have cleared the cache of the website and tried on an entirely different host, but the issue persists.我已经清除了网站的缓存并在完全不同的主机上尝试过,但问题仍然存在。

The favicon however is correctly displayed in the MacOS touch bar (see image below).然而,图标在 MacOS 触摸栏中正确显示(见下图)。

Does anyone have any idea why the Safari favicon is being rendered incorrectly?有谁知道为什么 Safari favicon 渲染不正确?

电脑上网浏览器

MacOS 触控栏

The color attribute in link rel="mask-icon" for Safari Pinned Tab Favicon, is not controlling the background, but the colour of the actual icon. Safari Pinned Tab Favicon 的link rel="mask-icon"color属性控制背景,而是控制实际图标的颜色。 This is expected . 这是意料之中的

In the example, the color attribute sets the display color of the image示例中,color属性设置图像的显示颜色

However, as you correctly recognized it depends on Dark or Bright mode.但是,正如您正确识别的那样,它取决于暗模式或亮模式。 The white background is only added to Favicons in Safari when we use Dark Mode.当我们使用深色模式时,白色背景只会添加到 Safari 中的 Favicon。

That would explain it, also other Blogs come to this conclusion, that in Dark mode, Safari automatically adds the White Background.这可以解释它,其他博客也得出这个结论,即在黑暗模式下,Safari 会自动添加白色背景。

And yet that can't be the truth.然而这不可能是事实。 Apple, Google, and many other websites, if visited on Safari in Dark Mode, do not have that white background in the (pinned/tab) Favicon. Apple、Google 和许多其他网站,如果在深色模式下在 Safari 上访问,则(固定/标签)Favicon 中没有白色背景。 Additionally many of them with very low contrast.此外,其中许多具有非常低的对比度。

I noticed reading online, Safari Favicons should be monochrome SVG where we define the (favicon) colour with the color attribute only.我注意到在线阅读,Safari Favicon 应该是单色 SVG,我们仅使用color属性定义(favicon)颜色。 I've created a Favicon following this principle but still, I get the white background in Dark mode.我已经按照这个原则创建了一个 Favicon,但仍然在黑暗模式下获得白色背景。

References: https://developer.apple.com/design/human-interface-guidelines/ios/icons-and-images/app-icon https://makandracards.com/makandra/26757-do-not-use-transparent-pngs-for-ios-favicons参考资料: https : //developer.apple.com/design/human-interface-guidelines/ios/icons-and-images/app-icon https://makandracards.com/makandra/26757-do-not-use-transparent -pngs-for-ios-favicon

A possible Solution一个可能的解决方案

Assume a white Favicon in Dark mode and a Black favicon in Bright mode.假设暗模式下的网站图标为白色,亮模式下的网站图标为黑色。

if (window.matchMedia('(prefers-color-scheme: dark)').matches === true) {
        console.log('dark');
        document.head.insertAdjacentHTML(
            'beforeend',
            '<link rel="mask-icon" href="/safari-pinned-tab.svg?v=your_dark_mode_fav" color="white">'
        );
    }
    else {
        document.head.insertAdjacentHTML(
            'beforeend',
            '<link rel="mask-icon" href="/safari-pinned-tab.svg?v= your_bright_mode_fav" color="black">'
        );
    }

This won't change on the fly and needs cache claering to be pushed to visitor.这不会即时更改,需要将缓存清除推送给访问者。

I'll proceed with researching, this is still not satisfying enough.我会继续研究,这仍然不够令人满意。

I wonder what happens if you put a tiny (one pixel) white border around the favicon image.我想知道如果在网站图标图像周围放置一个很小的(一个像素)白色边框会发生什么。 That should (for the computer program analysing it) be like a very high contrast and hence avoid the white background added, and it'd be invisible to the nude eye.这应该(对于分析它的计算机程序)就像一个非常高的对比度,因此避免添加白色背景,并且它对裸眼是不可见的。

Funny fact: I run in the same issue with almost the same colour as you did #0075be And I also think that's contrasting enough, but it seems not.有趣的事实:我遇到了与您几乎相同颜色的同一问题#0075be而且我也认为这足够对比,但似乎不是。

I ran into the same issue on my own website ( lyramsr.co ) and have fixed it by slightly increasing the brightness of the normal favicon and changing the colour of the mask icon accordingly.我在自己的网站 ( lyramsr.co ) 上遇到了同样的问题,并通过稍微增加普通图标的亮度并相应地更改蒙版图标的颜色来修复它。 (The colour was #1C806C, and is now #218D78.) This has made the white background disappear, so clearly the issue was contrast, although I'm puzzled as to how the browser determines what the appropriate amount of it is. (颜色是#1C806C,现在是#218D78。)这使得白色背景消失了,很明显问题是对比度,尽管我对浏览器如何确定适当的数量感到困惑。

Safari 14 doesn't appear to need the mask icon. Safari 14 似乎不需要蒙版图标。 It overrides the normal favicon when present, but when I remove it from the site it just uses the favicon, and that looks fine.它会在存在时覆盖正常的网站图标,但是当我从网站上删除它时,它只使用网站图标,看起来不错。 I didn't even notice the difference at first, since my normal favicon also uses only two colours.起初我什至没有注意到差异,因为我的普通图标也只使用两种颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM