xmlxhtmlsvgviewbox

SVG Scaling Text to fit container


This is likely a very simple question, but how do I get text in SVG to stretch to fit its container?

I don't care if it looks ugly from being stretched too long or high, but it needs to fits its container and be as big as possible.

Thanks


Solution

  • 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