how to resize a large svg to fit inside a div
27,835
Solution 1
I was wondering about adjusting the viewBox for this, so something like...
<div style="width:320px; height:480px;">
<svg id="svg1" width="100%" height="100%" viewBox="0 0 640 480" preserveAspectRatio="xMaxYMax"></svg>
</div>
jsfiddle http://jsfiddle.net/9zRR8/5/
Solution 2
The SVG will not scale because it does not have a viewBox defined. The viewBox tells the browser what the dimensions of the content are. Without it, there is no way to determine how much it should be scaled up or down to fit.
This is kind of a failing of svg-edit. It really should be adding one, ideally.
What you could try is loading the SVG into Inkscape or another SVG editor which should add a viewBox. Alternatively, you could add one manually, as per Ian's answer.
Related videos on Youtube
Author by
naheed
Updated on November 24, 2020Comments
-
naheed over 3 years
I have the following div inside
<body>
.<div style="width:320px; height:480px;"> <svg id="svg1"></svg> </div>
I want to fit a 640X480 svg inside this div. I have tried this:
var paper=Snap("#svg1"); Snap.load("somesvg.svg",function(f){ paper.append(f); }); paper.attr({ width:320, height:480 })
But the svg is cut off from the right size.
-
Kyle Weller over 10 yearsCan you post a jsFiddle? Also tell us about Snap.load(), it looks like it is async?
-
naheed over 10 years
-
TimSPQR over 10 yearsHere is an old post discussing the problems with loading an svg in a div with Snap. stackoverflow.com/questions/20110422/…
-
-
john ktejik over 9 yearsstrange, but I get Error: Invalid value for <svg> attribute preserveAspectRatio="xMaxYmax"
-
Ian over 9 yearsI think the case is wrong actually, so wonder if something has changed slightly with how the browsers uses case sensitivity and how its dealt with. developer.mozilla.org/en-US/docs/Web/SVG/Attribute/… has the valid values.
-
Vikash Pandey about 8 yearsCan you add some sample code.. I am getting svg element how to resize it is my biggest challenge
-
Andy Smith over 7 yearsDoesn't seem to work? If I change div width/height the size of the image that appears is unaffected.
-
Ian over 7 yearsIt depends what you are changing, the title of the question is a little ambiguous (as the question specified an inner width/height). Are you trying to make it responsive, if so try setting the papers width/height to 100%, e.g jsfiddle.net/9zRR8/543 if you mean something else it may be worth posting up a new question.
-
Justin over 7 years@johnktejik It is a long time since you asked the question but since no one answered. The value must be in camel-case, so the 'm' after 'Y' must be uppercase like 'xMaxYMax'.