Open your .svg
file with a text editor (it's just XML), and look for something like this at the top:
<svg ... width="50px" height="50px"...
Erase width and height attributes; the defaults are 100%, so it should stretch to whatever the container allows it.
Try these:
Set the missing viewbox and fill in the height and width values of the set height and height attributes in the svg tag
Then scale the picture simply by setting the height and width to the desired percent values. Good luck.
Set a fixed aspect ratio with preserveAspectRatio="X200Y200 meet
(e.g. 200px), but it's not necessary
e.g.
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="10%"
height="10%"
preserveAspectRatio="x200Y200 meet"
viewBox="0 0 350 350"
id="svg2"
version="1.1"
inkscape:version="0.48.0 r9654"
sodipodi:docname="namesvg.svg">
you can resize it by displaying svg in image tag and size image tag i.e.
<img width="200px" src="lion.svg"></img>
I have found it best to add viewBox
and preserveAspectRatio
attributes to my SVGs. The viewbox should describe the full width and height of the SVG in the form 0 0 w h
:
<svg preserveAspectRatio="xMidYMid meet" viewBox="0 0 700 550"></svg>
Use the following code:
<g transform="scale(0.1)">
...
</g>
Changing the width of the container also fixes it rather than changing the width and height of source file.
.SvgImage img{ width:80%; }
This fixes my issue of re sizing svg . you can give any % based on your requirement.
Here is an example of getting the bounds using svg.getBox()
:
https://gist.github.com/john-doherty/2ad94360771902b16f459f590b833d44
At the end you get numbers that you can plug into the svg to set the viewbox properly. Then use any css on the parent div and you're done.
// get all SVG objects in the DOM
var svgs = document.getElementsByTagName("svg");
var svg = svgs[0],
box = svg.getBBox(), // <- get the visual boundary required to view all children
viewBox = [box.x, box.y, box.width, box.height].join(" ");
// set viewable area based on value above
svg.setAttribute("viewBox", viewBox);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With