How to make a <svg> element expand or contract to its parent container?

140,774

Solution 1

The viewBox isn't the height of the container, it's the size of your drawing. Define your viewBox to be 100 units in width, then define your rect to be 10 units. After that, however large you scale the SVG, the rect will be 10% the width of the image.

Solution 2

Suppose I have an SVG which looks like this: pic1

And I want to put it in a div and make it fill the div responsively. My way of doing it is as follows:

First I open the SVG file in an application like inkscape. In File->Document Properties I set the width of the document to 800px and and the height to 600px (you can choose other sizes). Then I fit the SVG into this document.

pic2

Then I save this file as a new SVG file and get the path data from this file. Now in HTML the code that does the magic is as follows:

<div id="containerId">    
    <svg
    id="svgId" 
    xmlns:svg="http://www.w3.org/2000/svg"
    xmlns="http://www.w3.org/2000/svg"
    version="1.1"
    x="0"
    y="0"
    width="100%"
    height="100%"
    viewBox="0 0 800 600"
    preserveAspectRatio="none">
       <path d="m0 0v600h800v-600h-75.07031l-431 597.9707-292.445315-223.99609 269.548825-373.97461h-271.0332z" fill="#f00"/>
    </svg>
</div>

Note that width and height of SVG are both set to 100%, since we want it to fill the container vertically and horizontally ,but width and height of the viewBox are the same as the width and height of the document in inkscape which is 800px X 600px. The next thing you need to do is set the preserveAspectRatio to "none". If you need to have more information on this attribute here's a good link. And that's all there is to it.

One more thing is that this code works on almost all the major browsers even the old ones but on some versions of android and ios you need to use some javascrip/jQuery code to keep it consistent. I use the following in document ready and resize functions:

$('#svgId').css({
    'width': $('#containerId').width() + 'px',
    'height': $('#containerId').height() + 'px'
});

Hope it helps!

Solution 3

What's worked for me recently is to remove all height="" and width="" attributes from the <svg> tag and all child tags. Then you can use scaling using a percentage of the parent container's height or width.

Before:

<svg width="3212" height="3212" viewBox="0 0 3212 3212" fill="none" xmlns="http://www.w3.org/2000/svg">
   circle cx="1606" cy="1606" r="1387" stroke="black" stroke-width="438"/>
</svg>

After:

<svg viewBox="0 0 3212 3212" fill="none" xmlns="http://www.w3.org/2000/svg">
   circle cx="1606" cy="1606" r="1387" stroke="black" stroke-width="438"/>
</svg>

Solution 4

@robertc has it right, but you also need to notice that svg, #container causes the svg to be scaled exponentially for anything but 100% (once for #container and once for svg).

In other words, if I applied 50% h/w to both elements, it's actually 50% of 50%, or .5 * .5, which equals .25, or 25% scale.

One selector works fine when used as @robertc suggests.

svg {
  width:50%;
  height:50%;
}

Solution 5

For your iphone You could use in your head balise :

"width=device-width"
Share:
140,774
user782860
Author by

user782860

Updated on January 29, 2021

Comments

  • user782860
    user782860 over 3 years

    The goal is to have the <svg> element expand to the size of its parent container, in this case a <div>, no matter how big or small that container may be.

    The code:

    <style>
        svg, #container{
            height: 100%;
            width: 100%;
        }
    </style>
    
    <div id="container">
        <svg xmlns="http://www.w3.org/2000/svg" version="1.1" >
             <rect x="0" y="0" width="100" height="100" />
        </svg>
    </div>
    

    The most common solution to this problem seems to be setting the viewBox attribute on the <svg> element.

    viewBox="0 0 widthOfContainer heightOfContainer"
    

    However, this does not seem to work in cases where elements within the <svg> element have predefined widths and/or heights. For example, the <rect> element, in the above code, has its width and height explicitly set.

    So the obvious solution is to use % widths and % heights on those elements as well. But does this even have to be done? Especially, since <img src=test.svg > works fine and expands/contracts without any problems with explicitly set <rect> heights and widths.

    If elements like <rect>, and other elements like it, have to have their widths and heights defined in percentages, is there a way in Inkscape to set it so that all elements with the <svg> document use percentage widths, heights, etc.. instead of fixed dimensions?

  • Adrian Heine
    Adrian Heine over 12 years
    But the question is: How to scale the SVG?
  • robertc
    robertc over 12 years
    @AdrianLang The scaling happens automatically when you set a size on the SVG object, as is already done in the code in the question. Here's a fixed version of the example.
  • Adrian Heine
    Adrian Heine over 12 years
    You’re right, great, that actually works for me. I guess my problem was the height and width properties in my svg root element.
  • Mister Crimson
    Mister Crimson over 10 years
    I made some minor tweaks to that fiddle to make that resizing be forced horizontally and vertically without preserving the aspect ratio, and made it work in IE, while the fiddle @robertc submitted wasn't. Hope it'll help someone down the road!
  • Etheryte
    Etheryte over 10 years
    That's because your selector svg, #container matches both the svg dom element and the #container dom element. I believe you were instead looking for svg #container, but the specification of a parent isn't necessary if you're using an ID anyway.
  • Justin Putney
    Justin Putney over 10 years
    Isn't that exactly what I said, "once for #container and once for svg"?
  • Etheryte
    Etheryte over 10 years
    Oh pardon, you're right, but your answer is a bit oddly worded leading to the misunderstanding. It sounds as if you're talking about a potential gotcha at first.
  • patricksurry
    patricksurry about 9 years
    By default your svg will scale as large as possible so that it's completely visible but preserves it's aspect ratio (so a square viewBox won't completely fill a rectangular parent). If you really want to force it to completely cover the parent container, add preserveAspectRatio="none" to the SVG element.
  • adelphus
    adelphus almost 9 years
    +1 for the preserveAspectRatio note. After hunting high and low to get an SVG to actually stretch (not scale) to a div, this was the correct answer.
  • Prasad Parab
    Prasad Parab about 8 years
    @Gazoris, Thanks for this wonderful answer. have question on the same. What if my svg is nested. for e.g., <svg> <svg id="1"></svg> <svg id="2"></svg> </svg> i want to display svg 1 and 2 conditionally and it should take width and height of container. <div id="container" style="width:200px;height:200px;"></div> can you please help me out on this.
  • Michael
    Michael almost 7 years
    Can you give a working example? I can't get this to do anything.
  • Rolf
    Rolf over 6 years
    This is useful information. The viewbox unit is a relative unit. In my case I had a square viewbox of 0 0 6.82666 6.82666 so i set the width and height attribute to 6.82666, then 100% on the <svg> element, through CSS. It seems to work.
  • DanMad
    DanMad over 6 years
    @Reza Baradaran: Genius.
  • mikemaccana
    mikemaccana over 3 years
    This worked for me too. I've edited this question to add an example.