我有一个 svg 背景图像,但路径不是 100% 的 svg 宽度。我也尝试过更改preserveaspectratio,但没有成功。
<svg x="0" y="0" viewBox="0 0 2227 1601" >
<path d="M2166.88,40.32V1441.8l-323.83-195.25a347.4,347.4,0,0,0-352.44-3.72l-5.15,3-397.67,228.46a347.4,347.4,0,0,1-360.61-8.79L61,1038.23V40.32Z"/></svg>
最佳答案
正如我所评论的,您需要更改 viewBox 属性的值。 在下一个示例中,svg 元素具有 Overflow:visible,以便您可以看到没有溢出。
let bb = thePath.getBBox();
theSVG.setAttributeNS(null,"viewBox", `${bb.x} ${bb.y} ${bb.width} ${bb.height}`)
svg{border:solid; width:200px; overflow:visible}
<svg id="theSVG" x="0" y="0" viewBox="0 0 2227 1601" >
<path id="thePath" d="M2166.88,40.32V1441.8l-323.83-195.25a347.4,347.4,0,0,0-352.44-3.72l-5.15,3-397.67,228.46a347.4,347.4,0,0,1-360.61-8.79L61,1038.23V40.32Z"/>
</svg>
如果您需要在路径周围添加某种空白,您可以这样做:
theSVG.setAttributeNS(null,"viewBox", `${bb.x - padding} ${bb.y - padding} ${bb.width + 2* padding} ${bb.height + 2 * padding}`)
let bb = thePath.getBBox();
let padding = 30;
theSVG.setAttributeNS(null,"viewBox", `${bb.x - padding} ${bb.y - padding} ${bb.width + 2* padding} ${bb.height + 2 * padding}`)
svg{border:solid; width:200px; overflow:visible}
<svg id="theSVG" x="0" y="0" viewBox="0 0 2227 1601" >
<path id="thePath" d="M2166.88,40.32V1441.8l-323.83-195.25a347.4,347.4,0,0,0-352.44-3.72l-5.15,3-397.67,228.46a347.4,347.4,0,0,1-360.61-8.79L61,1038.23V40.32Z"/>
</svg>
关于html - 将路径宽度更改为 svg 容器的 100%,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62693472/