Zargham Khan
Zargham Khan

Reputation: 284

change path width to 100% of svg container

I have an svg background image but path is not 100% of svg width. I have tried change preserveaspectratio as well but did not work.

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

Upvotes: 2

Views: 2015

Answers (1)

enxaneta
enxaneta

Reputation: 33044

As I've commented you need to change the value of the viewBox attribute. In the next example the svg element has overflow:visible so that you can see that there is no overflow.

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>

If you need to add some sort of white space around the path you can do this:

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>

Upvotes: 3

Related Questions