Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I <embed> svg and scale it like an img

Tags:

svg

I just like images I want to separate the picture from the page. I can take a 1024x768 image and do this

<img src="1024x768.jpg" width="400" height="300"/>

or I can do this

<img src="1024x768.jpg" style="width:400px; height:300px"/>

and the image will be scaled not cropped. How do I do the same with svg? Note: I don't want to embed the svg directly into the page. That would be as stupid as using dataURLs for all my images. My artists edit images, other people edit html so I need those to be separate.

<embed src="somefile.svg" width="400" height="300"></embed>

Does not scale the svg, it just crops. CSS width/height doesn't work either. This there a way to do this similar to images?

like image 910
gman Avatar asked Jan 11 '12 09:01

gman


People also ask

Can SVG images be scaled?

Once you add a viewBox to your <svg> (and editors like Inkscape and Illustrator will add it by default), you can use that SVG file as an image, or as inline SVG code, and it will scale perfectly to fit within whatever size you give it.

How do you scale SVG?

You should set the viewBox attribute first, and then make all of your dimensions within your svg relative to that. When you assign height and width (whether in your svg tag or in your css) you are adjusting the size of the svg bounding box, not the size of the elements within it.

How do I use SVG as an image?

SVG images can be written directly into the HTML document using the <svg> </svg> tag. To do this, open the SVG image in VS code or your preferred IDE, copy the code, and paste it inside the <body> element in your HTML document. If you did everything correctly, your webpage should look exactly like the demo below.

How do I scale SVG to fit a div?

Set the CSS attribute position:absolute on your <svg> element to cause it to sit inside and fill your div. (If necessary, also apply left:0; top:0; width:100%; height:100% .)


1 Answers

okay, apparently you can just use svg in an img tag

<img src="somefile.svg" width="200"/>

And it works in IE9+, FF4+, Chrome4+, Safari4+, Opera 9.

like image 181
gman Avatar answered Oct 01 '22 12:10

gman