Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stretch <svg> inside an <embed> to fit window size

I am trying to stretch an svg document inside an DOM in order to fit the window size.

like so:

<div id="y">
    <div id="button"> click to zoom</div>
    <embed id="x" src="s17.svg" >
    <script>
        var btn= document.getElementById("button");
        btn.addEventListener('click',function(){
        var z= document.getElementsByTagName("embed")[0];
        var y = z.getSVGDocument();
        y.lastChild.setAttribute("viewBox","0 0 "+window.innerWidth+" "+window.innerHeight);
                                               },false);

    </script>
</div>

css:

#x{
    height:100%;
    width:100%;
    overflow:hidden;
}
#y{
    position:absolute;
    top:0;
    bottom:0;
    left:0;
    right:0;
    overflow:hidden;
}

This isn't working... What am I doing wrong?

like image 630
fogy Avatar asked May 26 '11 22:05

fogy


People also ask

How do I make SVG fit my screen?

We use viewBox to make the SVG image scalable. viewBox = “0 0 100 100”: defines a coordinate system having x=0, y=0, width=100 units and height=100 units. Thus, the SVG containing a rectangle having width=50px and height=50px will fill up the height and width of the SVG image, and all the dimensions get scaled equally.

Can SVG be stretched?

An SVG image with fixed dimensions will be treated just like a raster image of the same size. Note: If you are trying to stretch your SVG to a different aspect ratio with CSS—for example in order to stretch it over the page background—make sure your SVG includes preserveAspectRatio="none" .

How do I make SVG fit inside a div?

“In the SVG declaration after the doctype, simply remove the width and height attributes. This forces the browser to always fill the containing DIV or box with your SVG.”

How do I fix SVG size?

❓ How can I resize a SVG image? First, you need to add a SVG image file: drag & drop your SVG image file or click inside the white area to choose a file. Then adjust resize settings, and click the "Resize" button. After the process completes, you can download your result file.


1 Answers

All browsers should be able to handle this just fine:

  • add a viewBox to the svg element (s17.svg in your example) without using script if possible
  • remove the width and height attributes on the svg element if they are specified
  • add an attribute preserveAspectRatio="none" to the svg element to make it stretch even if the css-viewport aspect ratio doesn't match the viewBox aspect ratio.
  • set the width/height of the embed/iframe/object to whatever you want and the svg will automatically stretch to fit

If you don't want stretching then you can also do preserveAspectRatio="xMidYMid slice" (fill whole viewport, slicing away parts if necessary) or preserveAspectRatio="xMidYMid meet" (this is the default, center the svg in the viewport and maintain the aspect ratio).

like image 84
Erik Dahlström Avatar answered Oct 21 '22 03:10

Erik Dahlström