简体   繁体   English

缩放没有宽高比的SVG图像

[英]Scale SVG image without aspect ratio

Is it possible to fit the svg image to its full size without maintaining the aspect ratio? 是否可以在不保持纵横比的情况下将svg图像调整为完整尺寸? In the below example, 在以下示例中,

<img style="position: absolute; top: 12px; left: 31px; z-index: 1; width: 196px; height: 265px; " src="/media/images/logo.svg" />

在此处输入图片说明

The svg image is centered adding some transparent spaces along its sides. svg图像居中,并在其侧面添加了一些透明空间。 If I increase the size of the image, the svg image increases proportionally but does not take the entire space, instead the transparent space increases in size. 如果我增加图像的大小,则svg图像会成比例地增加,但不会占用整个空间,而是透明空间的大小会增加。 Is there a way to allow the svg image to occupy the entire space? 有没有办法让svg图像占据整个空间?

Thanks and Regards, 谢谢并恭祝安康,

Neha 内哈

I solved the same problem with help of this article . 我在本文的帮助下解决了相同的问题。 The important part for you would be: 对您来说重要的部分是:

In order to prevent the uniform scaling of our SVG graphic we need to add the preserveAspectRatio attribute with the value of "none". 为了防止SVG图形的统一缩放,我们需要添加值为“ none”的preserveAspectRatio属性。

So open your SVG file in text editor and add attribute preserveAspectRatio="none" to <svg> tag. 因此,在文本编辑器中打开您的SVG文件,然后向<svg>标签添加属性prepareAspectRatio =“ none” Like: 喜欢:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="none">

Is it possible to fit the svg image to its full size without maintaining the aspect ratio? 是否可以在不保持纵横比的情况下将svg图像调整为完整尺寸?

Yes, If you set required height and width , it should do resize. 是的,如果您设置了所需的heightwidth ,则应该调整大小。 Unless you need aspect ratio, you can leave one of the parameters to auto . 除非需要宽高比,否则可以将其中一个参数保留为auto

I could not reproduce your issue. 我无法重现您的问题。 You might be resizing the content div instead of img . 您可能正在调整内容div大小,而不是img大小。 I've attached a sample snippet, can you reproduce the issue in this below snippet? 我已经附上了一个示例代码段,您可以在下面的代码段中重现该问题吗?

Here, I've set the width & height property in-line & in CSS to do the resize. 在这里,我在CSS设置了widthheight属性以进行调整大小。

 .modified{ height:100px; width:200px; } 
 <img width="500px" height="300px" src="http://placeholder.pics/svg/150x250/DEDEDE/000000"/> <img class="modified" src="http://placeholder.pics/svg/150x250/DEDEDE/000000"/> 

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

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