Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SVG viewbox not always scaled to width and height

Tags:

svg

scaling

I'm using several libraries to generate SVG images in-browser, which can be bounced off the server through svgexport to generate PNGs or JPEGs at user-specified resolutions. (This works as expected.)

I'd like to offer the user the option of downloading the SVG that gets fed into the conversion, with the resolution used to set the width and height attributes. When I do that, the viewbox is not scaled to the specified width and height, but is padded so that the image occupies the original size area in the upper left.

While looking for solutions, I found images in the W3C documentation that illustrate the problem. If you open these images in Chrome and use the inspector to change the width and height properties,

  • ViewBox.svg will expand to fill the width and height (linked from here)
  • PreserveAspectRatio.svg will be padded to stay in the upper left (linked from here)

This does not appear related the presence or value of thepreserveAspectRatio property, or the nesting of svg tags. My files are rendered as padded rather than scaled in Chrome/Chromium, Firefox, Safari/WebKit, Opera, Inkscape, and Gapplin.

How do I ensure that my SVG is scaled rather than padded to fill the width and height?

like image 778
ShadSterling Avatar asked Jun 27 '16 20:06

ShadSterling


Video Answer


1 Answers

The viewbox is not scaled when it's entered as viewbox rather than viewBox; svg attribute names are case sensitive.

The second link does not have a viewBox attribute, and adding a viewbox (lowercase) attribute has no effect.

like image 198
ShadSterling Avatar answered Oct 19 '22 04:10

ShadSterling