简体   繁体   English

在HTML中调整SVG的大小而无需调整画布的大小

[英]Resizing SVG in HTML without resizing canvas

I am trying to increase the size of an SVG image by changing the height and width parameters but it doesn't work instead the size of the canvas changes and navigation bars appear. 我正在尝试通过更改高度和宽度参数来增加SVG图像的大小,但是它无法正常工作,而是更改了画布的大小并显示了导航栏。 Also there are two copies of height and width parameters. 此外,还有高度和宽度参数的两个副本。 I don't know which ones to change. 我不知道要更改哪个。

 <svg xmlns:.... width="85.75438690185547" height="75.7368392944336" xml:space="preserve" inkscape:version="0.48.4 r9939" sodipodi:docname="output.svg" style=""><rect id="backgroundrect" width="100%" height="100%" x="0" y="0" fill="none" stroke="none"/> ... </svg> 

To resize the shapes of svg without changing the dimensions of the svg canvas, add the viewBox 要在不更改svg画布尺寸的情况下调整svg形状的大小,请添加viewBox

I added a red frame to show the borders of the svg canvas. 我添加了一个红色框以显示svg画布的边框。

style="border:1px solid red;"

In the example below, the size of the svg canvas width ="85" and height ="75" is equal to the size of the 在下面的示例中, svg画布的width ="85"height ="75"的大小等于
viewBox = "0 0 85 75"

Therefore, the rectangle is displayed as is, without changing its size. 因此,该矩形按原样显示,而不更改其大小。

 <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="85" height="75" viewBox="0 0 85 75" style="border:1px solid red;" > <rect x="0" y="0" width="42" height="35" fill="purple" /> </svg> 

  • To increase the size of the rectangle without changing the size of the canvas svg you need to reduce the size of the viewBox="0 0 42 35" 要增加矩形的大小而不更改画布svg的大小,您需要减小viewBox="0 0 42 35"的大小。

 <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="85" height="75" viewBox="0 0 42 35" style="border:1px solid red;" > <rect x="0" y="0" width="42" height="35" fill="purple" /> </svg> 

  • To reduce the size of the rectangle without changing the size of the canvas svg you need to increase the size of viewBox = "0 0 170 150" 要在不更改画布svg大小的情况下减小矩形的大小,您需要增加viewBox = "0 0 170 150"的大小。

 <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="85" height="75" viewBox="0 0 170 150" style="border:1px solid red;" > <rect x="0" y="0" width="42" height="35" fill="purple" /> </svg> 

I hope this helps you. 我希望这可以帮助你。

Remove the width and height attributes from the svg and then size it in css. 从svg中删除width和height属性,然后在CSS中调整其大小。 EG: 例如:

 #someIcon { width: 100px; transition: all 0.5s ease; } #someIcon:hover { width: 200px; } #someIcon:hover .disc { fill: red; } 
 <svg id="someIcon" viewBox="0 0 84 98" fill="none" xmlns="http://www.w3.org/2000/svg"> <path class="disc" fill-rule="evenodd" clip-rule="evenodd" d="M42 70C57.464 70 70 57.464 70 42C70 26.536 57.464 14 42 14C26.536 14 14 26.536 14 42C14 57.464 26.536 70 42 70Z" fill="#009688"/> <path class="plus" fill-rule="evenodd" clip-rule="evenodd" d="M49 41H43V35H41V41H35V43H41V49H43V43H49V41Z" fill="white"/> </svg> 

OR tell the SVG to be 100% and put in a container which you can control the size of. 或告诉SVG为100%,并放入一个您可以控制尺寸的容器中。 EG: 例如:

 #iconwrapper { border: 1px solid red; display: inline-block; width: 25%; transition: all 0.5s ease; } #iconwrapper:hover { width: 50%; } #iconwrapper:hover .disc { fill: pink; } 
 <div id="iconwrapper"> <svg id="someIcon" width="100%" height="100%" viewBox="0 0 84 98" fill="none" xmlns="http://www.w3.org/2000/svg"> <path class="disc" fill-rule="evenodd" clip-rule="evenodd" d="M42 70C57.464 70 70 57.464 70 42C70 26.536 57.464 14 42 14C26.536 14 14 26.536 14 42C14 57.464 26.536 70 42 70Z" fill="blue"/> <path class="plus" fill-rule="evenodd" clip-rule="evenodd" d="M49 41H43V35H41V41H35V43H41V49H43V43H49V41Z" fill="white"/> </svg> </div> 

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

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