简体   繁体   English

Safari 中的 SVG 图标模糊

[英]SVG icons in safari are blurred

I have noticed that svg icons that placed via 'img' tag aren't rendered correctly in safari.我注意到通过 'img' 标签放置的 svg 图标在 safari 中没有正确呈现。 They end up being all blurry.他们最终都变得模糊了。 I have created a simple html page and pasted the same svg icon multiple time using different methods :我创建了一个简单的 html 页面并使用不同的方法多次粘贴相同的 svg 图标:

在此处输入图片说明

I am a bit frustrated why does the 'img' tag lower the quality of the icon?我有点沮丧,为什么 'img' 标签会降低图标的质量?

Thank you in advance!先感谢您!

Edit: I have created a demo编辑:我创建了一个演示

Maybe it will be helpful for someone - Safari can't correct render IMG tag (SVG format) for retina display - so the solution is - UP size image - the result you can see here Demo也许它对某人有帮助 - Safari 无法正确渲染 IMG 标签(SVG 格式)以用于视网膜显示 - 所以解决方案是 - UP 大小的图像 - 结果你可以在这里看到Demo

Костыль для сафари (in Russian) Костыль для сафари(俄语)

#svg {
  width: 20px;
  height: 21px;

  div {
    position: relative;
    transform: scale(0.25);
    transform-origin: 0 0;
    height: 100%;
    &:before {
      content: '';
      display: block;
      position: absolute;
      width: 400%;
      height: 400%;
      background-image: url(http://svgshare.com/i/1Le.svg);
      background-size: contain;
      background-repeat: no-repeat;
    }
  }
}

.w-img {
  width: 20px;
  height: 21px;
  img {
    height: 400%;
    width: 400%;
    vertical-align: middle;
    transform: scale(0.25);
    transform-origin: 0 0;
  }
}

I'm sorry it's probably late but I had a similar problem and it was because of the blur filter, when I put the shadow on another element and kept the original one on top of it, it worked fine.对不起,可能晚了,但我遇到了类似的问题,这是因为模糊过滤器,当我将阴影放在另一个元素上并将原始元素放在上面时,效果很好。 Eg, you have to duplicate your icon element, apply shadow to it and finally place it below the actual icon.例如,您必须复制图标元素,为其应用阴影,最后将其放置在实际图标下方。

When you put SVG into an <img> tag, it basically bitmaps the vector file.当您将 SVG 放入<img>标签时,它基本上是矢量文件的位图。 So where the SVG is capable of scaling and remaining sharp at all sizes, the <img> tag treats it analogous to a gif and any scaling will compromise the quality of the image.因此,在 SVG 能够缩放并在所有尺寸下保持清晰的情况下, <img>标签将其视为类似于 gif,任何缩放都会损害图像的质量。

Using SVG as an img |使用 SVG 作为 img | CSS Tricks CSS 技巧

If I save the SVG to a file, I can use it directly in an <img> tag.如果我将 SVG 保存到文件中,我可以直接在<img>标签中使用它。

 <img src="kiwi.svg" alt="Kiwi standing on oval">

In Illustrator, our artboard was 612px ✕ 502px.在 Illustrator 中,我们的画板是 612px ✕ 502px。 That's exactly how big the image will on the page, left to itself.这正是图像在页面上的大小,留给自己。 You can change the size of it though just by selecting the img and changing its width or height, again like you could a PNG or JPG.您可以通过选择 img 并更改其宽度或高度来更改它的大小,就像您可以更改 PNG 或 JPG 一样。 Here's an example of that.这是一个例子

So in short, by using an <img> tag, you lock the size of the base image.简而言之,通过使用<img>标签,您可以锁定基本图像的大小。 This then allows the browser to determine the quality of the image, which is going to be directly proportional to how different from the original size you are trying to display the image.然后,这允许浏览器确定图像的质量,这将与您尝试显示图像的原始尺寸的差异成正比。 In this case, as can be expected, safari is handling it less elegantly than if you displayed the SVG using another means.在这种情况下,正如可以预期的那样,与使用其他方式显示 SVG 相比,safari 处理它的方式不太优雅。

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

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