简体   繁体   English

如何防止SVG图像周围的CSS背景颜色“渗出”?

[英]How can I prevent CSS background-colour “bleed” around my SVG images?

I have an SVG file which I display white normally, or red when the user hovers over it. 我有一个SVG文件,该文件通常显示为白色,或者当用户将鼠标悬停在该文件上时显示为红色。 This is made by having transparent areas of the image and using the following CSS & HTML: 这是通过使图像具有透明区域并使用以下CSS和HTML来实现的:

a>img{background:#fff;width:32px;height:32px}
a:hover>img{background:#900}

<a href="blah.html"><img src="blah.svg"></a>

The problem is that I sporadically get very thin borders of white (or red) around the image. 问题是我偶尔会在图像周围得到非常细的白色(或红色)边框。 Different browsers/zoom levels/devices show different borders - sometimes none, sometimes some, sometimes all of them. 不同的浏览器/缩放级别/设备显示不同的边界-有时没有,有时有些,有时全部。

This would appear to suggest that the SVG file isn't quite "filling" the img tag. 这似乎表明SVG文件没有完全“填充” img标签。

I might assume that the problem is that the SVG isn't quite square, so it's not stretching perfectly to 32x32 pixels, but sometimes I get borders on all four sides, so it can't be that simple. 我可能会认为问题在于SVG不太方形,因此无法完美地拉伸到32x32像素,但是有时我在所有四个侧面上都形成边框,所以不可能那么简单。 (And indeed the SVG file has a viewbox of "0 0 195 195" and doesn't appear to have any points outside of that range. (实际上,SVG文件的视图框为“ 0 0 195 195”,并且似乎没有超出该范围的任何点。

It's worth noting that changing the dimensions to 39x39 pixels (a perfect divisor of 195) doesn't fix the issue, not that I expected it to, due to the vector nature of SVG. 值得注意的是,由于SVG的向量性质,将尺寸更改为39x39像素(195的完美除数)不能解决问题,并非我期望的那样。

Fiddle: http://jsfiddle.net/3wtazst8/1/ 小提琴: http : //jsfiddle.net/3wtazst8/1/

Any suggestions? 有什么建议么?

Thanks 谢谢

I know this is over a year old, but I was having the same issue and considered Paul LeBeau's advice regarding extending outside the viewBox. 我知道这已经一年多了,但是我遇到了同样的问题,并考虑了Paul LeBeau关于扩展viewBox的建议。 Below is an example showing the issue and the fix. 下面是显示问题和修复程序的示例。

Icons with background colors showing issue and showing fix 具有背景颜色的图标显示问题并显示修复

The badge icon does not show the bleed, while the medal icon does. 徽章图标不显示出血,而勋章图标显示。

Side-by-side image of icons within viewBox and extended beyond viewBox viewBox中图标的并排图像,并扩展到viewBox之外

Viewing the left SVG in Illustrator, the background of the image goes to the very edge of the viewBox. 在Illustrator中查看左SVG,图像的背景将移到viewBox的最边缘。

Viewing the right SVG in Illustrator, the background of the image extends beyond the edge of the viewBox. 在Illustrator中查看正确的SVG,图像的背景超出了viewBox的边缘。

Fixing your SVG so the background extends beyond viewBox will fix your issue. 修复SVG,使背景超出viewBox的范围,即可解决您的问题。

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

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