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

时间:2019-03-15 14:05:41

标签: html css svg

我试图通过更改height和width参数来增加SVG图像的大小,但是它无法正常工作,而是更改了画布的大小并显示了导航栏。此外,还有高度和宽度参数的两个副本。我不知道要更改哪个。

<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>

2 个答案:

答案 0 :(得分:3)

要在不更改svg画布尺寸的情况下调整svg的形状大小,请添加viewBox

我添加了一个红色框以显示svg画布的边框。

style="border:1px solid red;"

在下面的示例中,svg画布width ="85"height ="75"的大小等于
 viewBox = "0 0 85 75"

因此,该矩形按原样显示,而不更改其大小。

<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>	

  • 在不更改矩形大小的情况下增加矩形的大小 您需要减小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> ,您需要增加svg的大小

viewBox = "0 0 170 150"

希望对您有帮助。

答案 1 :(得分:1)

从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>

或告诉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>

相关问题