SVG Scaling Text to fit container SVG Scaling Text to fit container xml xml

SVG Scaling Text to fit container


If you really don't care that the text gets ugly, here's how to fit unknown length text into a known width.

<svg width="436" height="180"    style="border:solid 6px"    xmlns="http://www.w3.org/2000/svg">    <g>        <text y="50%" textLength="436" lengthAdjust="spacingAndGlyphs">UGLY TEXT</text>    </g></svg>

enter image description here


Here is what I have come up with... Its similar to what other people have posted, but I think it resizes and scales nicely. This code will add spacing to any text roughly between 10-25 characters to make it fill the entire width of its parent. If you need longer or shorter text, just adjust the viewBox width and textLength attributes.

<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox='0 0 300 24'><text textLength='290' lengthAdjust="spacing" x='5' y="14" >    Some Unknown Text that is resizing</text></svg>