gpt4 book ai didi

css - SVG 元素之间的奇数间距

转载 作者:太空狗 更新时间:2023-10-29 13:40:12 30 4
gpt4 key购买 nike

我使用一些基本的 javascript 和字符串连接生成了 SVG 元素:http://jsfiddle.net/8d3zqLsf/3/

这些 SVG 元素之间的间距非常小(最大 1 像素)并且彼此之间的距离在可接受的范围内。

当我复制生成的 SVG 并将其呈现为正常 DOM 的一部分而不是在页面加载时生成时,它在 SVG 元素之间具有奇怪的间距。 http://jsfiddle.net/1n73a8yr/

注意:我已验证 SVG 的宽度与其内部的形状一样宽,因此额外的空间不是来自 SVG。

为什么 SVG 在页面加载时注入(inject)与作为 DOM 的一部分呈现时呈现不同?有没有一种方法可以解决这个问题而不求助于具有负像素值的 svgs 上的左 css 属性?

HTML:

<div>
<svg width="86.60254037844386" height="100"> <polygon xmlns="http://www.w3.org/2000/svg" id="0" style="fill:#6C6;" points="86.60254037844388,25.000000000000004 86.60254037844388,75 43.30127018922193,100 -7.105427357601002e-15,75 -7.105427357601002e-15,25.000000000000014 43.301270189221924,0 "></polygon></svg>
<svg width="86.60254037844386" height="100"> <polygon xmlns="http://www.w3.org/2000/svg" id="0" style="fill:#6C6;" points="86.60254037844388,25.000000000000004 86.60254037844388,75 43.30127018922193,100 -7.105427357601002e-15,75 -7.105427357601002e-15,25.000000000000014 43.301270189221924,0 "></polygon></svg>
<svg width="86.60254037844386" height="100"> <polygon xmlns="http://www.w3.org/2000/svg" id="0" style="fill:#6C6;" points="86.60254037844388,25.000000000000004 86.60254037844388,75 43.30127018922193,100 -7.105427357601002e-15,75 -7.105427357601002e-15,25.000000000000014 43.301270189221924,0 "></polygon></svg>
</div>

CSS

svg {
display:inline-block;
margin-left:0px;
margin-right:0px;
padding-left:0px;
padding-right:0px;
}

最佳答案

问题:

a series of inline-block elements formatted like you normally format HTML will have spaces in between them.

内联 block 是:

The element generates a block element box that will be flowed with surrounding content as if it were a single inline box (behaving much like a replaced element would)

那怎么办?

在这种情况下,因为是 svg,您可以在 HTML 中注释空白。

svg {
display:inline-block;
}
<svg width="86.60254037844386" height="100">
<polygon xmlns="http://www.w3.org/2000/svg" id="0" style="fill:#6C6;" points="86.60254037844388,25.000000000000004 86.60254037844388,75 43.30127018922193,100 -7.105427357601002e-15,75 -7.105427357601002e-15,25.000000000000014 43.301270189221924,0 "></polygon>
</svg><!-- --><svg width="86.60254037844386" height="100">
<polygon xmlns="http://www.w3.org/2000/svg" id="0" style="fill:#6C6;" points="86.60254037844388,25.000000000000004 86.60254037844388,75 43.30127018922193,100 -7.105427357601002e-15,75 -7.105427357601002e-15,25.000000000000014 43.301270189221924,0 "></polygon>
</svg><!-- --><svg width="86.60254037844386" height="100">
<polygon xmlns="http://www.w3.org/2000/svg" id="0" style="fill:#6C6;" points="86.60254037844388,25.000000000000004 86.60254037844388,75 43.30127018922193,100 -7.105427357601002e-15,75 -7.105427357601002e-15,25.000000000000014 43.301270189221924,0 "></polygon>
</svg>

More info 关于使用 inline-block

的空格

更新(2019)

如今(有一段时间),对于 inline-block 元素,有一种比所有 hack 更好的方法,它使用 display: flex 作为父元素。

div {
display: flex
}
<div>
<svg width="86.60254037844386" height="100">
<polygon xmlns="http://www.w3.org/2000/svg" id="0" style="fill:#6C6;" points="86.60254037844388,25.000000000000004 86.60254037844388,75 43.30127018922193,100 -7.105427357601002e-15,75 -7.105427357601002e-15,25.000000000000014 43.301270189221924,0 "></polygon>
</svg>
<svg width="86.60254037844386" height="100">
<polygon xmlns="http://www.w3.org/2000/svg" id="0" style="fill:#6C6;" points="86.60254037844388,25.000000000000004 86.60254037844388,75 43.30127018922193,100 -7.105427357601002e-15,75 -7.105427357601002e-15,25.000000000000014 43.301270189221924,0 "></polygon>
</svg>
<svg width="86.60254037844386" height="100">
<polygon xmlns="http://www.w3.org/2000/svg" id="0" style="fill:#6C6;" points="86.60254037844388,25.000000000000004 86.60254037844388,75 43.30127018922193,100 -7.105427357601002e-15,75 -7.105427357601002e-15,25.000000000000014 43.301270189221924,0 "></polygon>
</svg>
</div>

关于css - SVG 元素之间的奇数间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28034337/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com