Search code examples
svg

How do you render SVG in a different size in HTML?


Given this svg:

<svg width="640" height="480" xmlns="http://www.w3.org/2000/svg">
 <!-- Created with SVG-edit - http://svg-edit.googlecode.com/ -->
 <g>
  <title>Layer 1</title>
  <ellipse stroke="#000000" ry="210.999999" rx="259.000004" id="svg_1" cy="237.727263" cx="324.999989" stroke-width="5" fill="#FF0000"/>
 </g>
</svg>

How do I scale it to user 320x240 without having to recompute the numbers inside? Is this even possible?


Solution

  • Add viewBox="0 0 640 480" and set width="320" height="240" to the <svg> element to scale the SVG. you can go crazy on the width and height and control the aspect ratio with the preserveAspectRatio attribute.