简体   繁体   English

定义viewBox时SVG的宽度不正确

[英]Width of SVG isn't correct when viewBox is defined

I am trying to understand why my SVG icon is not rendered correctly. 我试图了解为什么我的SVG图标无法正确呈现。 The icon is a little smallest that my SVG element event if the viewBox property was used the same like width\\height. 如果将viewBox属性使用为width \\ height,则该图标比我的SVG元素事件小一点。

Thanks for any help 谢谢你的帮助

As you see the icon is a little bit smallest than the whole SVG element. 如您所见,该图标比整个SVG元素小一点。

在此处输入图片说明

My SVG code. 我的SVG代码。

 <svg width="24" height="25" viewBox="0 0 24 25" xmlns="http://www.w3.org/2000/svg"> <title>1FB31B20-CDD9-43CB-A743-1C613F5D5E0C</title> <g fill="#9FA09F" fill-rule="evenodd"> <path d="M7 13.684a6 6 0 1 0 0-12 6 6 0 0 0 0 12zm0 1a7 7 0 1 1 0-14 7 7 0 0 1 0 14z" /> <path d="M7 7.684a2 2 0 1 0 .001-4 2 2 0 0 0-.001 4zM7 8.684c3 0 3.566 2.116 3.566 2.116.24.488-.019.884-.563.884H3.997c-.55 0-.82-.418-.553-.9 0 0 .556-2.1 3.556-2.1z" /> </g> </svg> 

您的绘图似乎占据的空间比viewBox区域小得多,只需将viewBox的大小减小到所需的大小即可。

View Port 查看端口

-The viewport is the visible area of an <svg> . -视口是<svg>的可见区域。 To set viewport we can use attributes height and width with <svg> . 要设置视口,我们可以在<svg>使用heightwidth属性。

View Box 查看框

-It allow us to set our graphic and stretch to fit within container. -它允许我们设置图形并拉伸以适合容器。 viewbox having four property mix-x , min-y , width , height . viewbox具有四个属性mix-xmin-ywidthheight And min values represent from what point within the image the viewBox should start. min表示从viewBox应该从图像中的哪一点开始。

Example

If we set viewport and viewbox , width and height same. 如果我们设置viewport和viewbox ,则widthheight相同。 Then viewbox cover entire image. 然后, viewbox覆盖整个图像。

Solution

-For your problem you need to reduce size of viewbox width an height property. -对于您的问题,您需要减小viewbox width的大小和height属性。 But as your image size is small you also need to set width and `height to get required result. 但是,由于您的图片尺寸很小,因此您还需要设置width和`高度,以获得所需的结果。

JsFiddle Solution with different example JsFiddle解决方案的不同示例

If you created the icon, it was draw on the top left corner of the canvas. 如果创建了图标,则该图标绘制在画布的左上角。 The actual icon is a 14px x 15px inside a 24px x 25px viewbox. 实际的图标是24px x 25px框内的14px x 15px

在此处输入图片说明 在此处输入图片说明

If this was the case, you could either re-draw it centered full width and height of a 24px x 25px workspace canvas or if you can't re-draw it just try to align it manually as it is. 如果是这种情况,您可以将其重新绘制为以24px x 25px工作区画布的全宽和高度为中心,或者如果不能重新绘制,则尝试按原样手动对齐。

Anyways, if you wish the latter, here's a flex suggestion to align it with CSS translate (tweak values to your benefit): 无论如何,如果您希望使用后者,可以根据以下灵活建议将其与CSS translate对齐(调整值以使您受益):

 .container { display: flex; } svg { transform: translate(25%, 20%); } 
 <div class="container"> <svg width="24" height="25" viewBox="0 0 24 25" xmlns="http://www.w3.org/2000/svg"> <title>1FB31B20-CDD9-43CB-A743-1C613F5D5E0C</title> <g fill="#9FA09F" fill-rule="evenodd"> <path d="M7 13.684a6 6 0 1 0 0-12 6 6 0 0 0 0 12zm0 1a7 7 0 1 1 0-14 7 7 0 0 1 0 14z" /> <path d="M7 7.684a2 2 0 1 0 .001-4 2 2 0 0 0-.001 4zM7 8.684c3 0 3.566 2.116 3.566 2.116.24.488-.019.884-.563.884H3.997c-.55 0-.82-.418-.553-.9 0 0 .556-2.1 3.556-2.1z" /> </g> </svg> <input type="text"> </div> 

The reason why your icon appears to be offset is because it is not 24 × 25 pixels. 您的图标出现偏移的原因是因为它不是24×25像素。 The actual bounds of the vector are 14 × 14 pixels. 向量的实际边界是14×14像素。

屏幕截图

I would use an SVG editing tool to fine-tune your vector. 我将使用SVG编辑工具来微调您的向量。 The vector that you are currently using is very unstable. 您当前使用的向量非常不稳定。 I had issues opening it up as-is. 我在按原样打开它时遇到了问题。

I went ahead and tuned your vector and utilized actual circle and arc-paths. 我继续调整了向量,并利用了实际的圆弧和圆弧路径。

Here is the result. 这是结果。 The original vector is a 32 pixel SVG. 原始向量是32像素SVG。

 <svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32"> <title>1FB31B20-CDD9-43CB-A743-1C613F5D5E0C</title> <g id="person-in-circle"> <circle id="circle" cx="16" cy="16" r="15" fill="none" stroke="#9FA09F" stroke-width="2" stroke-linejoin="round" /> <g id="person" fill="#9FA09F"> <circle id="person-head" cx="16" cy="12" r="5" /> <path id="person-shoulders" d="M 24 24 a8,8 0 1,0 -16,0" /> </g> </g> </svg> 

Here is a 16 pixel SVG (Scaled by half) 这是16像素SVG(按比例缩放)

 <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16"> <title>1FB31B20-CDD9-43CB-A743-1C613F5D5E0C</title> <g id="person-in-circle"> <circle id="circle" cx="8" cy="8" r="7.5" fill="none" stroke="#9FA09F" stroke-width="1" stroke-linejoin="round" /> <g id="person" fill="#9FA09F"> <circle id="person-head" cx="8" cy="6" r="2.5" /> <path id="person-shoulders" d="M 12 12 a4,4 0 1,0 -8,0" /> </g> </g> </svg> 

If you want to find out more about how arc-paths work, check out this answer: 如果您想了解有关弧形路径如何工作的更多信息,请查看以下答案:

Circle drawing with SVG's arc path 带有SVG弧线路径的圆图

 <path d=" M cx cy m -r, 0 ar,r 0 1,0 (r * 2),0 ar,r 0 1,0 -(r * 2),0 " /> 

There are two problems here: 这里有两个问题:

  1. As others have pointed out, your viewBox is wrong. 正如其他人指出的那样,您的viewBox是错误的。 Change it to "0 0 14 14" to fix that. 将其更改为"0 0 14 14"可以解决该问题。

  2. Secondly, by default, <svg> elements are display: inline-block , just like other images. 其次,默认情况下, display: inline-block <svg>元素display: inline-block ,就像其他图像一样。 So if they are in a line with other text (inluding <input> elements) they will be positioned so that they sit of the text baseline. 因此,如果它们与其他文本(包括<input>元素)排成一行,则它们的位置将使其位于文本基线的上方。

There are various way to solve that, but generally you would just change the SVG to display: block then use the typical ways that you would align vertically to other things on the line. 有多种方法可以解决此问题,但是通常您只需将SVG更改为display: block然后使用典型的方法将SVG与行上的其他东西垂直对齐。 Eg. 例如。 with float: left and margin . 带有float: leftmargin

 svg { display:block; float: left; } input { margin-top: 2px; margin-left: 5px; } 
 <svg width="24" height="24" viewBox="0 0 14 14" xmlns="http://www.w3.org/2000/svg"> <title>1FB31B20-CDD9-43CB-A743-1C613F5D5E0C</title> <g fill="#9FA09F" fill-rule="evenodd"> <path d="M7 13.684a6 6 0 1 0 0-12 6 6 0 0 0 0 12zm0 1a7 7 0 1 1 0-14 7 7 0 0 1 0 14z" /> <path d="M7 7.684a2 2 0 1 0 .001-4 2 2 0 0 0-.001 4zM7 8.684c3 0 3.566 2.116 3.566 2.116.24.488-.019.884-.563.884H3.997c-.55 0-.82-.418-.553-.9 0 0 .556-2.1 3.556-2.1z" /> </g> </svg> <input type="text"/> 

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

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