简体   繁体   English

SVG元素有太多额外空间

[英]SVG element has too much extra space

I'm using Bootstrap 4, and I have a simple svg element in my Web page: 我正在使用Bootstrap 4,并且我的网页中有一个简单的svg元素:

<svg>
    <polygon points="0,0 20,5 0,10" style="fill: black; stroke: black;" />
    <polygon points="20,0 40,5 20,10" style="fill: black; stroke: black;" />
</svg>

And to show how much extra space the element is using, I created a border around the element: 为了显示元素正在使用多少额外空间,我在元素周围创建了一个边框:

<style>
    svg {
        border: 1px solid black;
    }
</style>

The graphic is very simple, and displays mostly as it should, like this: 该图形非常简单,并且大多数情况下都应显示,如下所示:

svg屏幕截图

But I don't understand why there is so much extra whitespace on the right and bottom. 但是我不明白为什么在右侧和底部有这么多额外的空格。 I've tried everything I can think of to just make the container fit to the actual content: adding viewBox="0 0 20 40" to the svg tag just blows up the graphic so that it's enormous, and viewPort doesn't do anything. 我已经尝试了所有可以使容器适合实际内容的事情:向svg标签添加viewBox="0 0 20 40"只会炸毁图形,以至于图像很大,而viewPort却不执行任何操作。 I've tried a bunch of other CSS "tricks" and putting the graphic inside its own div element, all of which had no effect on the appearance of the graphic. 我尝试了许多其他CSS“技巧”,并将图形放入其自己的div元素中,所有这些都对图形的外观没有影响。

I don't remember experiencing this issue before. 我不记得以前遇到过这个问题。 Does Bootstrap add extra styling to svg graphics that I have to override, or is this normal? Bootstrap是否向我必须覆盖的svg图形添加了额外的样式,或者这是正常现象吗? Appreciate any help on how to fix it. 感谢有关如何修复它的任何帮助。

When an inline replaced element has no width nor height explicitely set, it will default to 300px * 150px. 如果没有明确设置内联替换元素的宽度或高度,则默认为300px * 150px。 (Actual rules are specified here ) 此处指定了实际规则)

Are concerned by this rule, <iframe> <video> <canvas> and <svg> . 受此规则<iframe> <video> <canvas>和<svg>约束

To avoid that, set a width or height on your <svg> (eitehr through its attributes, or through CSS. 为避免这种情况,请在<svg>上设置宽度或高度(通过属性或CSS来设置宽度或高度)。

 svg { border: 1px solid; width: 150px; height: 150px; } 
 <svg viewBox="0 0 40 40"> <polygon points="0,0 20,5 0,10" style="fill: black; stroke: black;" /> <polygon points="20,0 40,5 20,10" style="fill: black; stroke: black;" /> </svg> 

As Kaiido mentions you need to declare a viewBox attribute for the svg element. 正如Kaiido提到的那样,您需要为svg元素声明一个viewBox属性。 To calculate the size of the viewBox I use to wrap everything in a <g> element and use the method getBBox() for that <g> element. 为了计算视框的大小我使用包裹在一切<g>元素,并使用方法getBBox()对于<g>元素。

In this case the method returns the following bounding box: {x:0,y:0,height:40,width:10} . 在这种情况下,该方法返回以下边界框: {x:0,y:0,height:40,width:10}

The viewbox you need is: viewBox="0 0 40 10" . 您需要的视图框是: viewBox="0 0 40 10"

Since your polygons have a stroke you need some extra space so I used viewBox="-1 -1 42 12" 由于您的多边形有笔触,因此您需要一些额外的空间,因此我使用了viewBox="-1 -1 42 12"

You don't need to keep the <g id="test"> wrap. 您无需保留<g id="test">换行。

 svg{border:1px solid} 
 <svg viewBox="-1 -1 42 12"> <g id="test"> <polygon points="0,0 20,5 0,10" style="fill: black; stroke: black;" /> <polygon points="20,0 40,5 20,10" style="fill: black; stroke: black;" /> </g> </svg> 

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

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