简体   繁体   English

如何重叠两个SVG图像?

[英]How to overlap two SVG images?

So I have two SVG images that I've created in Photoshop. 所以我有两个我在Photoshop中创建的SVG图像。 The images have all the correct dimensions to align next to one another however, when I export them out they don't overlap. 图像具有彼此相邻的所有正确尺寸,但是当我将它们导出时它们不重叠。 Below are the two images (sorry for the sizes) : 以下是两张图片(对不起尺码):

在此输入图像描述

在此输入图像描述

And I've exported them out as SVGs then placed the code within my index.html page. 我已将它们作为SVG导出,然后将代码放在我的index.html页面中。 That looks like : 看起来像:

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" preserveAspectRatio="xMidYMid" width="600" height="862" viewBox="0 0 600 862">
  <image xlink:href="data:img/png;base64,iVBORw0KGgoAAAANSUhEUgAABQIA ..." />
</svg>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" preserveAspectRatio="xMidYMid" width="600" height="862" viewBox="0 0 600 862">
  <image xlink:href="data:img/png;base64,iVBORw0KGgoAAAANSUhEUgAAA8EAAAT7CAMAAACJ ..." />
</svg>

Sorry, I had to create a gist as my images are base64 encoded so they're huge : 对不起,我不得不创建一个要点,因为我的图像是base64编码所以它们很大:

full code 完整代码

Question: The background is transparent in Photoshop. 问题:Photoshop中的背景是透明的。 But they overlap one another to the point one hides the other. 但是它们彼此重叠到一个隐藏另一个的点。 How can I get my two SVG images to overlap one another but both be visible? 如何让我的两个SVG图像相互重叠但两个都可见?

The problem with your approach is that both shapes are in seperate svg elements . 你的方法的问题是两个形状都是单独的svg元素 So they can't overlap with the default svg positioning. 所以它们不能与默认的svg定位重叠。 You could make them overlay with absolute, relative or fixed positioning but that would be overkill and complicated for such simple shapes. 您可以使用绝对,相对或固定定位进行叠加,但对于这种简单的形状而言,这将是过度和复杂的。 Another approach would be to export both of them in the same file BUT : 另一种方法是将它们都导出到同一个文件中但是

For such a simple shape, you can use an inline SVG with 2 polygon elements . 对于这种简单的形状,您可以使用带有2个多边形元素内联SVG Simple, much shorter and "human readable" : 简单,短得多,“人类可读”:

 svg{width:30%; height:auto;} 
 <svg viewbox="0 0 50 60"> <polygon points="0 0 50 0 50 5 0 50" fill="#C000FF"/> <polygon points="0 50 50 5 50 60 0 60" fill="#803698"/> </svg> 

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

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