简体   繁体   English

除非容器具有固定宽度,否则 SVG 图像无法在 IE11/Edge 中正确呈现

[英]SVG image not rendering properly in IE11 / Edge unless container has fixed width

Both Edge and Internet Explorer 11 seem to scale SVG images rather differently than regular raster images. Edge 和 Internet Explorer 11 对 SVG 图像的缩放似乎与常规光栅图像不同。

Take a look at the following example:看看下面的例子:

 .container { display: flex; flex-flow: row nowrap; } .box { flex: 0 1 auto; background: red; } .fill { flex: 1 0 auto; background: green; } .box img { display: block; width: auto; height: 150px; background: blue; }
 <div class="container"> <div class="box"> <img src="http://s33.postimg.org/hbl2jy69b/logo_light.png" alt="" /> </div> <div class="fill">fill</div> </div> <div class="container"> <div class="box"> <img src="http://imgh.us/logo-light_1.svg" alt="" /> </div> <div class="fill">fill</div> </div>

Both images are identical, the first is a PNG, the other is an SVG.两个图像是相同的,第一个是 PNG,另一个是 SVG。 Both images reside in a .box which is set to flex-shrink but not flex-grow .两个图像都位于设置为flex-shrink但不是flex-grow.box中。 Next to the box is a .fill which is set to flex-grow , but not flex-shrink .盒子旁边是一个.fill ,它被设置为flex-grow ,但不是flex-shrink

So, the idea is really simple.所以,这个想法很简单。 We set each image's height to some fixed value, the width gets calculated automatically keeping the aspect ratio of the image, which respectfully sets the width of the .box container.我们将每个图像的高度设置为某个固定值,宽度自动计算保持图像的纵横比,分别设置.box容器的宽度。 .fill occupies the rest of the available space. .fill占用剩余的可用空间。

The example works just fine both in Chrome and Firefox, and two identical images are being displayed:该示例在 Chrome 和 Firefox 中都可以正常工作,并且显示了两个相同的图像:

在此处输入图片说明

In Edge however, the image is cropped:然而,在 Edge 中,图像被裁剪:

在此处输入图片说明

Where in IE it's even weirder:在 IE 中的哪里更奇怪:

在此处输入图片说明

You can view the source of the SVG here: http://imgh.us/logo-light_1.svg您可以在此处查看 SVG 的来源: http : //imgh.us/logo-light_1.svg

I've also tried a couple of different values for the preserveAspectRatio attribute of the SVG all with different results, but still not the one I'm actually looking for.我还为 SVG 的preserveAspectRatio属性尝试了几个不同的值,结果都不同,但仍然不是我真正想要的。

Am I missing something?我错过了什么吗? Is this just a matter of a proper preserveAspectRatio attribute or is it a bug?这只是一个适当的preserveAspectRatio属性的问题还是一个错误?

As you've noted, a fixed width on the image container gets this to work on IE11.正如您所指出的,图像容器上的固定宽度使其在 IE11 上工作。

This worked for me:这对我有用:

.box {
    flex: 0 1 461px;   /* changed flex-basis from auto */
    background: red;
}

If that's not an option, and you simply want PNG and SVG to match, then a positive z-index value will shift the blue background to the surface.如果这不是一个选项,并且您只是希望 PNG 和 SVG 匹配,那么正的z-index值会将蓝色背景移动到表面。

.box {
    flex: 0 1 auto;
    background: red;
    z-index: 1;        /* new */
}

Note that z-index does not need to be positioned when applied to flex items ( reference ).注意, z-index并不需要当施加到柔性物品(被定位参考)。


Another possible solution would be placing the image in a flex container, then defining the width (as opposed to the height ) of the image:另一种可能的解决方案是将图像放在 flex 容器中,然后定义图像的width (与height相反):

.box {
  flex: 0 1 auto;
  background: red;
  display: flex;      /* new */
}

.box img {
  /* display: block; */
  /* width: auto; */
  /* height: 150px; */
  background: blue;
  width: 332px;     /* new; value can be anything, PNG and SVG will match */
}

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

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