So, I have an SVG file in HTML, and one of the things I've heard about the format is that it doesn't get all pixelated when you zoom in on it.
I know with a jpeg or whatever I could have it stored as a 50 by 50 icon, then actually display it as a (rather pixelated) 100 by 100 thumbnail (or 10 by 10), by manually setting the height and width in the image_src tag.
However, SVG files seem to be used with object/embed tags, and changing the height or width of THOSE just results in more space being allocated for the picture.
IS there any way to specify that you want an SVG image displayed smaller or larger than it actually is stored in the file system?
I have found it best to add
viewBox
andpreserveAspectRatio
attributes to my SVGs. The viewbox should describe the full width and height of the SVG in the form0 0 w h
:Here is an example of getting the bounds using
svg.getBox()
: https://gist.github.com/john-doherty/2ad94360771902b16f459f590b833d44At 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.
Changing the width of the container also fixes it rather than changing the width and height of source file.
This fixes my issue of re sizing svg . you can give any % based on your requirement.
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 necessarye.g.
Open your
.svg
file with a text editor (it's just XML), and look for something like this at the top:Erase width and height attributes; the defaults are 100%, so it should stretch to whatever the container allows it.
SVG graphics, like other creative works, are protected under copyright law in most countries. Depending on jurisdiction, license of the work or whether or not you are the copyright holder you may not be able to modify the SVG without violating copyright law, believe it or not.
But laws are tricky topics and sometimes you just want to get shit done. Therefore you may adjust the scale of the graphic without modifying the work itself using the
img
tag withwidth
attribute within your HTML.Using an external HTTP request to specify the size:
Specifying size in markup using a Data URI:
SVGs can be Optimized for Data URIs to create SVG Favicon images suitable for any size: